import { useTheme } from "@emotion/react"; import { Box, Stack, Typography } from "@mui/material"; import TextInput from "../../Components/Inputs/TextInput"; 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"; import { getAppSettings, updateAppSettings } from "../../Features/Settings/settingsSlice"; import { useState, useEffect } from "react"; import Select from "../../Components/Inputs/Select"; import { advancedSettingsValidation } from "../../Validation/validation"; import { buildErrors, hasValidationErrors } from "../../Validation/error"; const AdvancedSettings = ({ isAdmin }) => { const navigate = useNavigate(); useEffect(() => { if (!isAdmin) { navigate("/"); } }, [navigate, isAdmin]); const [errors, setErrors] = useState({}); const theme = useTheme(); const { authToken } = useSelector((state) => state.auth); const dispatch = useDispatch(); const settings = useSelector((state) => state.settings); const [localSettings, setLocalSettings] = useState({ apiBaseUrl: "", logLevel: "debug", systemEmailHost: "", systemEmailPort: "", systemEmailAddress: "", systemEmailPassword: "", jwtTTLNum: 99, jwtTTLUnits: "days", jwtTTL: "99d", dbType: "", redisHost: "", redisPort: "", pagespeedApiKey: "", }); const parseJWTTTL = (data) => { if (data.jwtTTL) { const len = data.jwtTTL.length; data.jwtTTLNum = data.jwtTTL.substring(0, len - 1); data.jwtTTLUnits = unitItems.filter( (itm) => itm._id == data.jwtTTL.substring(len - 1) )[0].name; } }; useEffect(() => { const getSettings = async () => { const action = await dispatch(getAppSettings({ authToken })); if (action.payload.success) { parseJWTTTL(action.payload.data); setLocalSettings(action.payload.data); } else { createToast({ body: "Failed to get settings" }); } }; getSettings(); }, [authToken, dispatch]); 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 unitItemLookup = { days: "d", hours: "h", }; const unitItems = Object.keys(unitItemLookup).map((key) => ({ _id: unitItemLookup[key], name: key, })); const handleLogLevel = (e) => { const id = e.target.value; const newLogLevel = logItems.find((item) => item._id === id).name; setLocalSettings({ ...localSettings, logLevel: newLogLevel }); }; const handleJWTTTLUnits = (e) => { const id = e.target.value; const newUnits = unitItems.find((item) => item._id === id).name; setLocalSettings({ ...localSettings, jwtTTLUnits: newUnits }); }; const handleBlur = (event) => { const { value, id } = event.target; const { error } = advancedSettingsValidation.validate( { [id]: value }, { abortEarly: false, } ); setErrors((prev) => { return buildErrors(prev, id, error); }); }; const handleChange = (event) => { const { value, id } = event.target; setLocalSettings({ ...localSettings, [id]: value }); }; const handleSave = async () => { localSettings.jwtTTL = localSettings.jwtTTLNum + unitItemLookup[localSettings.jwtTTLUnits]; if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors)) { return; } const action = await dispatch( updateAppSettings({ settings: localSettings, authToken }) ); let body = ""; if (action.payload.success) { parseJWTTTL(action.payload.data); setLocalSettings(action.payload.data); body = "Settings saved successfully"; } else { body = "Failed to save settings"; } createToast({ body }); }; return ( Client settings Modify client settings here. About BlueWave Uptime v1.0.0 Developed by Bluewave Labs. Save ); }; AdvancedSettings.propTypes = { isAdmin: PropTypes.bool, }; export default AdvancedSettings;