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

265 lines
7.6 KiB
React
Raw Normal View History

2024-09-27 10:55:37 +08:00
import { useTheme } from "@emotion/react";
2024-09-27 12:22:50 +08:00
import { Box, Stack, Typography } from "@mui/material";
2024-09-27 10:55:37 +08:00
import Field from "../../Components/Inputs/Field";
import Link from "../../Components/Link";
import "./index.css";
import { useDispatch, useSelector } from "react-redux";
import { createToast } from "../../Utils/toastUtils";
import PropTypes from "prop-types";
import LoadingButton from "@mui/lab/LoadingButton";
import { ConfigBox } from "../Settings/styled";
import { useNavigate } from "react-router";
2024-09-30 11:14:53 +08:00
import {
getAppSettings,
updateAppSettings,
} from "../../Features/Settings/settingsSlice";
2024-09-27 13:38:57 +08:00
import { useState, useEffect } from "react";
2024-09-27 10:55:37 +08:00
import Select from "../../Components/Inputs/Select";
2024-09-27 12:13:39 +08:00
2024-09-27 10:55:37 +08:00
const AdvancedSettings = ({ isAdmin }) => {
2024-09-27 12:22:50 +08:00
const navigate = useNavigate();
2024-09-27 13:38:57 +08:00
useEffect(() => {
if (!isAdmin) {
navigate("/");
}
}, [navigate, isAdmin]);
2024-09-27 12:22:50 +08:00
2024-09-27 10:55:37 +08:00
const theme = useTheme();
2024-09-27 12:22:50 +08:00
const { authToken } = useSelector((state) => state.auth);
2024-09-27 12:13:39 +08:00
const dispatch = useDispatch();
2024-09-27 10:55:37 +08:00
const settings = useSelector((state) => state.settings);
2024-09-30 11:14:53 +08:00
const [localSettings, setLocalSettings] = useState({
apiBaseUrl: "",
logLevel: "debug",
systemEmailHost: "",
systemEmailPort: "",
systemEmailAddress: "",
systemEmailPassword: "",
jwtTTL: "",
dbType: "",
redisHost: "",
redisPort: "",
pagespeedApiKey: "",
});
useEffect(() => {
const getSettings = async () => {
const action = await dispatch(getAppSettings({ authToken }));
if (action.payload.success) {
setLocalSettings(action.payload.data);
} else {
createToast({ body: "Failed to get settings" });
}
};
getSettings();
}, [authToken, dispatch]);
2024-09-27 10:55:37 +08:00
const logItems = [
{ _id: 1, name: "none" },
{ _id: 2, name: "debug" },
{ _id: 3, name: "error" },
{ _id: 4, name: "warn" },
];
const logItemLookup = {
none: 1,
debug: 2,
error: 3,
warn: 4,
};
const handleLogLevel = (e) => {
const id = e.target.value;
const newLogLevel = logItems.find((item) => item._id === id).name;
setLocalSettings({ ...localSettings, logLevel: newLogLevel });
};
const handleChange = (event) => {
const { value, id } = event.target;
setLocalSettings({ ...localSettings, [id]: value });
};
2024-09-27 12:13:39 +08:00
const handleSave = async () => {
const action = await dispatch(
updateAppSettings({ settings: localSettings, authToken })
);
let body = "";
if (action.payload.success) {
2024-09-30 11:14:53 +08:00
console.log(action.payload.data);
setLocalSettings(action.payload.data);
2024-09-27 12:13:39 +08:00
body = "Settings saved successfully";
} else {
body = "Failed to save settings";
}
createToast({ body });
};
2024-09-27 10:55:37 +08:00
return (
<Box
className="settings"
style={{
paddingBottom: 0,
}}
>
<Stack
component="form"
gap={theme.spacing(12)}
noValidate
spellCheck="false"
>
<ConfigBox>
<Box>
<Typography component="h1">Client Settings</Typography>
<Typography sx={{ mt: theme.spacing(2) }}>
Modify client settings here
</Typography>
</Box>
<Stack gap={theme.spacing(20)}>
<Field
2024-09-27 12:13:39 +08:00
id="apiBaseUrl"
label="API URL Host"
2024-09-27 12:13:39 +08:00
value={localSettings.apiBaseUrl}
2024-09-27 10:55:37 +08:00
onChange={handleChange}
/>
<Select
id="logLevel"
label="logLevel"
name="logLevel"
items={logItems}
value={logItemLookup[localSettings.logLevel]}
onChange={handleLogLevel}
/>
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
<Typography component="h1">Email Settings</Typography>
<Typography sx={{ mt: theme.spacing(2) }}>
Set your host email settings here. These settings are used for
sending system emails
</Typography>
</Box>
<Stack gap={theme.spacing(20)}>
<Field
type="text"
id="systemEmailHost"
label="Email Host"
name="systemEmailHost"
value={localSettings.systemEmailHost}
onChange={handleChange}
/>
<Field
type="number"
id="systemEmailPort"
label="System Email Address"
name="systemEmailPort"
value={localSettings.systemEmailPort.toString()}
onChange={handleChange}
/>
<Field
type="email"
id="systemEmailAddress"
label="System Email Address"
name="systemEmailAddress"
value={localSettings.systemEmailAddress}
onChange={handleChange}
/>
<Field
type="text"
id="systemEmailPassword"
label="System Email Password"
name="systemEmailPassword"
value={localSettings.systemEmailPassword}
onChange={handleChange}
/>
</Stack>
</ConfigBox>
2024-09-27 12:22:50 +08:00
<ConfigBox>
<Box>
<Typography component="h1">Server Settings</Typography>
<Typography sx={{ mt: theme.spacing(2) }}>
Modify server settings here
</Typography>
</Box>
<Stack gap={theme.spacing(20)}>
<Field
type="text"
id="jwtTTL"
label="JWT Time To Live"
name="jwtTTL"
value={localSettings.jwtTTL}
onChange={handleChange}
/>
<Field
type="text"
id="dbType"
label="Database Type"
name="dbType"
value={localSettings.dbType}
onChange={handleChange}
/>
<Field
type="text"
id="redisHost"
label="Redis Host"
name="redisHost"
value={localSettings.redisHost}
onChange={handleChange}
/>
<Field
type="number"
id="redisPort"
label="Redis Port"
name="redisPort"
value={localSettings.redisPort.toString()}
onChange={handleChange}
/>
<Field
type="text"
id="pagespeedApiKey"
label="PageSpeed API Key"
name="pagespeedApiKey"
value={localSettings.pagespeedApiKey}
onChange={handleChange}
/>
</Stack>
</ConfigBox>
2024-09-27 10:55:37 +08:00
<ConfigBox>
<Box>
<Typography component="h1">About</Typography>
</Box>
<Box>
<Typography component="h2">BlueWave Uptime v1.0.0</Typography>
<Typography
sx={{ mt: theme.spacing(2), mb: theme.spacing(6), opacity: 0.6 }}
>
Developed by Bluewave Labs.
</Typography>
<Link
level="secondary"
url="https://github.com/bluewave-labs"
label="https://github.com/bluewave-labs"
/>
</Box>
</ConfigBox>
2024-09-27 12:13:39 +08:00
<Stack direction="row" justifyContent="flex-end">
<LoadingButton
loading={settings.isLoading || settings.authIsLoading}
variant="contained"
color="primary"
sx={{ px: theme.spacing(12), mt: theme.spacing(20) }}
onClick={handleSave}
>
Save
</LoadingButton>
</Stack>
2024-09-27 10:55:37 +08:00
</Stack>
</Box>
);
};
AdvancedSettings.propTypes = {
isAdmin: PropTypes.bool,
};
export default AdvancedSettings;