Files
Checkmate/Client/src/Pages/Settings/index.jsx
T

290 lines
9.1 KiB
React
Raw Normal View History

2024-07-20 20:48:59 -04:00
import { useTheme } from "@emotion/react";
2024-09-16 12:13:29 +08:00
import { Box, Stack, Typography } from "@mui/material";
2024-07-20 20:48:59 -04:00
import Field from "../../Components/Inputs/Field";
import Link from "../../Components/Link";
2024-07-24 14:09:24 -04:00
import Select from "../../Components/Inputs/Select";
import { logger } from "../../Utils/Logger";
2024-07-24 14:09:24 -04:00
import "./index.css";
2024-08-30 10:04:47 -07:00
import { useDispatch, useSelector } from "react-redux";
import { createToast } from "../../Utils/toastUtils";
import {
deleteMonitorChecksByTeamId,
addDemoMonitors,
deleteAllMonitors,
} from "../../Features/UptimeMonitors/uptimeMonitorsSlice";
2024-09-16 12:13:29 +08:00
import { update } from "../../Features/Auth/authSlice";
import PropTypes from "prop-types";
2024-08-31 18:10:50 -04:00
import LoadingButton from "@mui/lab/LoadingButton";
import { setTimezone } from "../../Features/UI/uiSlice";
import timezones from "../../Utils/timezones.json";
2024-09-16 12:13:29 +08:00
import { useState } from "react";
import { ConfigBox } from "./styled";
import { networkService } from "../../main";
import { settingsValidation } from "../../Validation/validation";
const SECONDS_PER_DAY = 86400;
const Settings = ({ isAdmin }) => {
2024-07-20 20:48:59 -04:00
const theme = useTheme();
2024-08-30 10:04:47 -07:00
const { user, authToken } = useSelector((state) => state.auth);
2024-09-16 12:13:29 +08:00
const { checkTTL } = user;
2024-08-30 10:04:47 -07:00
const { isLoading } = useSelector((state) => state.uptimeMonitors);
2024-09-16 12:13:29 +08:00
const { isLoading: authIsLoading } = useSelector((state) => state.auth);
const { timezone } = useSelector((state) => state.ui);
2024-09-16 12:13:29 +08:00
const [checksIsLoading, setChecksIsLoading] = useState(false);
const [form, setForm] = useState({
ttl: checkTTL ? (checkTTL / SECONDS_PER_DAY).toString() : 0,
2024-09-16 12:13:29 +08:00
});
const [errors, setErrors] = useState({});
2024-08-30 10:04:47 -07:00
const dispatch = useDispatch();
2024-09-16 12:13:29 +08:00
const handleChange = (event) => {
const { value, id } = event.target;
const { error } = settingsValidation.validate(
{ [id]: value },
{
abortEarly: false,
}
);
if (!error || error.details.length === 0) {
setErrors({});
} else {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
console.log(newErrors);
logger.error("Validation errors:", error.details);
}
let inputValue = value;
id === "ttl" && (inputValue = value.replace(/[^0-9]/g, ""));
setForm((prev) => ({
...prev,
[id]: inputValue,
}));
};
2024-09-03 10:37:26 -07:00
// TODO Handle saving
2024-09-16 12:13:29 +08:00
const handleSave = async () => {
try {
setChecksIsLoading(true);
await networkService.updateChecksTTL({
authToken: authToken,
ttl: form.ttl,
});
2024-09-16 12:13:29 +08:00
const updatedUser = { ...user, checkTTL: form.ttl };
const action = await dispatch(
update({ authToken, localData: updatedUser })
);
if (action.payload.success) {
createToast({
body: "Settings saved successfully",
});
} else {
if (action.payload) {
// dispatch errors
createToast({
body: action.payload.msg,
});
} else {
// unknown errors
createToast({
body: "Unknown error.",
});
}
}
} catch (error) {
console.log(error);
createToast({ body: "Failed to save settings" });
} finally {
setChecksIsLoading(false);
}
};
2024-09-03 10:37:26 -07:00
2024-08-30 10:04:47 -07:00
const handleClearStats = async () => {
try {
const action = await dispatch(
deleteMonitorChecksByTeamId({ teamId: user.teamId, authToken })
);
if (deleteMonitorChecksByTeamId.fulfilled.match(action)) {
createToast({ body: "Stats cleared successfully" });
} else {
createToast({ body: "Failed to clear stats" });
}
} catch (error) {
logger.error(error);
createToast({ body: "Failed to clear stats" });
}
};
2024-07-20 20:48:59 -04:00
const handleInsertDemoMonitors = async () => {
try {
const action = await dispatch(addDemoMonitors({ authToken }));
if (addDemoMonitors.fulfilled.match(action)) {
createToast({ body: "Successfully added demo monitors" });
} else {
createToast({ body: "Failed to add demo monitors" });
}
} catch (error) {
logger.error(error);
createToast({ Body: "Failed to add demo monitors" });
}
};
const handleDeleteAllMonitors = async () => {
try {
const action = await dispatch(deleteAllMonitors({ authToken }));
if (deleteAllMonitors.fulfilled.match(action)) {
createToast({ body: "Successfully deleted all monitors" });
} else {
createToast({ body: "Failed to add demo monitors" });
}
} catch (error) {
logger.error(error);
createToast({ Body: "Failed to delete all monitors" });
}
};
2024-07-20 20:48:59 -04:00
return (
<Box
className="settings"
style={{
paddingBottom: 0,
}}
>
2024-08-28 10:28:02 -04:00
<Stack
component="form"
gap={theme.spacing(12)}
noValidate
spellCheck="false"
>
<ConfigBox>
2024-07-22 13:48:26 -04:00
<Box>
<Typography component="h1">General Settings</Typography>
2024-08-28 10:28:02 -04:00
<Typography sx={{ mt: theme.spacing(2), mb: theme.spacing(2) }}>
2024-07-22 13:48:26 -04:00
<Typography component="span">Display timezone</Typography>- The
timezone of the dashboard you publicly display.
</Typography>
</Box>
2024-08-28 18:15:48 -04:00
<Stack gap={theme.spacing(20)}>
2024-07-24 14:09:24 -04:00
<Select
2024-07-22 13:48:26 -04:00
id="display-timezone"
label="Display timezone"
value={timezone}
onChange={(e) => {
dispatch(setTimezone({ timezone: e.target.value }));
}}
items={timezones}
2024-07-22 13:48:26 -04:00
/>
2024-07-20 20:48:59 -04:00
</Stack>
2024-08-28 10:28:02 -04:00
</ConfigBox>
2024-09-16 12:44:27 +08:00
{isAdmin && (
<ConfigBox>
2024-07-20 20:48:59 -04:00
<Box>
<Typography component="h1">History and monitoring</Typography>
<Typography sx={{ mt: theme.spacing(2) }}>
Define here for how long you want to keep the data. You can also
remove all past data.
</Typography>
2024-07-20 20:48:59 -04:00
</Box>
<Stack gap={theme.spacing(20)}>
<Field
2024-09-16 12:13:29 +08:00
id="ttl"
label="The days you want to keep monitoring history."
optionalLabel="0 for infinite"
2024-09-16 12:13:29 +08:00
value={form.ttl}
onChange={handleChange}
error={errors.ttl}
/>
<Box>
<Typography>Clear all stats. This is irreversible.</Typography>
2024-08-31 18:10:50 -04:00
<LoadingButton
variant="contained"
color="error"
2024-09-16 12:13:29 +08:00
loading={isLoading || authIsLoading || checksIsLoading}
onClick={handleClearStats}
sx={{ mt: theme.spacing(4) }}
2024-08-31 18:10:50 -04:00
>
Clear all stats
</LoadingButton>
</Box>
</Stack>
</ConfigBox>
)}
{isAdmin && (
<ConfigBox>
<Box>
<Typography component="h1">Demo Monitors</Typography>
<Typography sx={{ mt: theme.spacing(2) }}>
Here you can add and remove demo monitors
</Typography>
</Box>
<Stack gap={theme.spacing(20)}>
<Box>
<Typography>Add demo monitors</Typography>
<LoadingButton
variant="contained"
color="primary"
2024-09-16 12:13:29 +08:00
loading={isLoading || authIsLoading || checksIsLoading}
onClick={handleInsertDemoMonitors}
sx={{ mt: theme.spacing(4) }}
>
Add demo monitors
</LoadingButton>
</Box>
<Box>
<Typography>Remove all monitors</Typography>
<LoadingButton
variant="contained"
color="error"
2024-09-16 12:13:29 +08:00
loading={isLoading || authIsLoading || checksIsLoading}
onClick={handleDeleteAllMonitors}
sx={{ mt: theme.spacing(4) }}
>
Remove all monitors
</LoadingButton>
</Box>
</Stack>
</ConfigBox>
)}
2024-08-28 10:28:02 -04:00
<ConfigBox>
2024-07-22 13:48:26 -04:00
<Box>
<Typography component="h1">About</Typography>
</Box>
<Box>
<Typography component="h2">BlueWave Uptime v1.0.0</Typography>
2024-07-22 13:48:26 -04:00
<Typography
2024-08-28 10:28:02 -04:00
sx={{ mt: theme.spacing(2), mb: theme.spacing(6), opacity: 0.6 }}
2024-07-22 13:48:26 -04:00
>
Developed by Bluewave Labs.
</Typography>
<Link
level="secondary"
url="https://github.com/bluewave-labs"
label="https://github.com/bluewave-labs"
/>
</Box>
2024-08-28 10:28:02 -04:00
</ConfigBox>
2024-07-22 13:48:26 -04:00
<Stack direction="row" justifyContent="flex-end">
2024-09-03 10:37:26 -07:00
<LoadingButton
2024-09-16 12:13:29 +08:00
loading={isLoading || authIsLoading || checksIsLoading}
disabled={Object.keys(errors).length > 0}
2024-08-31 17:37:55 -04:00
variant="contained"
color="primary"
2024-08-28 10:28:02 -04:00
sx={{ px: theme.spacing(12), mt: theme.spacing(20) }}
onClick={handleSave}
2024-08-31 17:37:55 -04:00
>
Save
2024-09-03 10:37:26 -07:00
</LoadingButton>
2024-07-20 20:48:59 -04:00
</Stack>
2024-08-28 10:28:02 -04:00
</Stack>
2024-07-20 20:48:59 -04:00
</Box>
);
2024-06-18 21:01:41 -04:00
};
Settings.propTypes = {
isAdmin: PropTypes.bool,
};
export default Settings;