- Extracted functions to error.js
- Add validation to advanced settings page
This commit is contained in:
1 parent
44cee3cefb
commit
8c57f3990b
4 files changed
+92
-32
No files matched your search
@@ -12,10 +12,11 @@ import { useNavigate } from "react-router";
|
|||||||
import { getAppSettings, updateAppSettings } from "../../Features/Settings/settingsSlice";
|
import { getAppSettings, updateAppSettings } from "../../Features/Settings/settingsSlice";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import Select from "../../Components/Inputs/Select";
|
import Select from "../../Components/Inputs/Select";
|
||||||
|
import { advancedSettingsValidation } from "../../Validation/validation";
|
||||||
|
import { buildErrors, hasValidationErrors } from "../../Validation/error";
|
||||||
|
|
||||||
const AdvancedSettings = ({ isAdmin }) => {
|
const AdvancedSettings = ({ isAdmin }) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
navigate("/");
|
navigate("/");
|
||||||
@@ -39,11 +40,12 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
redisPort: "",
|
redisPort: "",
|
||||||
pagespeedApiKey: "",
|
pagespeedApiKey: "",
|
||||||
});
|
});
|
||||||
|
const [errors, setErrors] = useState({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const getSettings = async () => {
|
const getSettings = async () => {
|
||||||
const action = await dispatch(getAppSettings({ authToken }));
|
const action = await dispatch(getAppSettings({ authToken }));
|
||||||
if (action.payload.success) {
|
if (action.payload?.success) {
|
||||||
setLocalSettings(action.payload.data);
|
setLocalSettings(action.payload.data);
|
||||||
} else {
|
} else {
|
||||||
createToast({ body: "Failed to get settings" });
|
createToast({ body: "Failed to get settings" });
|
||||||
@@ -75,9 +77,20 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
setLocalSettings({ ...localSettings, [id]: value });
|
setLocalSettings({ ...localSettings, [id]: value });
|
||||||
|
const { error } = advancedSettingsValidation.validate(
|
||||||
|
{ [id]: value },
|
||||||
|
{
|
||||||
|
abortEarly: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
setErrors((prev) => {
|
||||||
|
return buildErrors(prev, id, error);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
|
if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors))
|
||||||
|
return;
|
||||||
const action = await dispatch(
|
const action = await dispatch(
|
||||||
updateAppSettings({ settings: localSettings, authToken })
|
updateAppSettings({ settings: localSettings, authToken })
|
||||||
);
|
);
|
||||||
@@ -118,6 +131,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
label="API URL Host"
|
label="API URL Host"
|
||||||
value={localSettings.apiBaseUrl}
|
value={localSettings.apiBaseUrl}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.apiBaseUrl}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
id="logLevel"
|
id="logLevel"
|
||||||
@@ -126,6 +140,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
items={logItems}
|
items={logItems}
|
||||||
value={logItemLookup[localSettings.logLevel]}
|
value={logItemLookup[localSettings.logLevel]}
|
||||||
onChange={handleLogLevel}
|
onChange={handleLogLevel}
|
||||||
|
error={errors.logLevel}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
@@ -133,8 +148,8 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
<Box>
|
<Box>
|
||||||
<Typography component="h1">Email settings</Typography>
|
<Typography component="h1">Email settings</Typography>
|
||||||
<Typography sx={{ mt: theme.spacing(2) }}>
|
<Typography sx={{ mt: theme.spacing(2) }}>
|
||||||
Set your host email settings here. These settings are used for sending
|
Set your host email settings here. These settings are used for
|
||||||
system emails.
|
sending system emails.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<Stack gap={theme.spacing(20)}>
|
<Stack gap={theme.spacing(20)}>
|
||||||
@@ -145,6 +160,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="systemEmailHost"
|
name="systemEmailHost"
|
||||||
value={localSettings.systemEmailHost}
|
value={localSettings.systemEmailHost}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.systemEmailHost}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="number"
|
type="number"
|
||||||
@@ -153,6 +169,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="systemEmailPort"
|
name="systemEmailPort"
|
||||||
value={localSettings.systemEmailPort.toString()}
|
value={localSettings.systemEmailPort.toString()}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.systemEmailPort}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="email"
|
type="email"
|
||||||
@@ -161,6 +178,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="systemEmailAddress"
|
name="systemEmailAddress"
|
||||||
value={localSettings.systemEmailAddress}
|
value={localSettings.systemEmailAddress}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.systemEmailAddress}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
@@ -169,6 +187,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="systemEmailPassword"
|
name="systemEmailPassword"
|
||||||
value={localSettings.systemEmailPassword}
|
value={localSettings.systemEmailPassword}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.systemEmailPassword}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
@@ -187,6 +206,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="jwtTTL"
|
name="jwtTTL"
|
||||||
value={localSettings.jwtTTL}
|
value={localSettings.jwtTTL}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.jwtTTL}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
@@ -195,6 +215,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="dbType"
|
name="dbType"
|
||||||
value={localSettings.dbType}
|
value={localSettings.dbType}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.dbType}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
@@ -203,6 +224,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="redisHost"
|
name="redisHost"
|
||||||
value={localSettings.redisHost}
|
value={localSettings.redisHost}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.redisHost}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="number"
|
type="number"
|
||||||
@@ -211,6 +233,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="redisPort"
|
name="redisPort"
|
||||||
value={localSettings.redisPort.toString()}
|
value={localSettings.redisPort.toString()}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.redisPort}
|
||||||
/>
|
/>
|
||||||
<Field
|
<Field
|
||||||
type="text"
|
type="text"
|
||||||
@@ -219,6 +242,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
name="pagespeedApiKey"
|
name="pagespeedApiKey"
|
||||||
value={localSettings.pagespeedApiKey}
|
value={localSettings.pagespeedApiKey}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
|
error={errors.pagespeedApiKey}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
@@ -228,7 +252,9 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="h2">BlueWave Uptime v1.0.0</Typography>
|
<Typography component="h2">BlueWave Uptime v1.0.0</Typography>
|
||||||
<Typography sx={{ mt: theme.spacing(2), mb: theme.spacing(6), opacity: 0.6 }}>
|
<Typography
|
||||||
|
sx={{ mt: theme.spacing(2), mb: theme.spacing(6), opacity: 0.6 }}
|
||||||
|
>
|
||||||
Developed by Bluewave Labs.
|
Developed by Bluewave Labs.
|
||||||
</Typography>
|
</Typography>
|
||||||
<Link
|
<Link
|
||||||
@@ -238,10 +264,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
|||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</ConfigBox>
|
</ConfigBox>
|
||||||
<Stack
|
<Stack direction="row" justifyContent="flex-end">
|
||||||
direction="row"
|
|
||||||
justifyContent="flex-end"
|
|
||||||
>
|
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
loading={settings.isLoading || settings.authIsLoading}
|
loading={settings.isLoading || settings.authIsLoading}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import {
|
|||||||
MS_PER_WEEK,
|
MS_PER_WEEK,
|
||||||
} from "../../../Utils/timeUtils";
|
} from "../../../Utils/timeUtils";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
|
import { buildErrors, hasValidationErrors } from "../../../Validation/error";
|
||||||
|
|
||||||
const getDurationAndUnit = (durationInMs) => {
|
const getDurationAndUnit = (durationInMs) => {
|
||||||
if (durationInMs % MS_PER_DAY === 0) {
|
if (durationInMs % MS_PER_DAY === 0) {
|
||||||
@@ -176,16 +177,6 @@ const CreateMaintenance = () => {
|
|||||||
fetchMonitors();
|
fetchMonitors();
|
||||||
}, [authToken, user]);
|
}, [authToken, user]);
|
||||||
|
|
||||||
const buildErrors = (prev, id, error) => {
|
|
||||||
const updatedErrors = { ...prev };
|
|
||||||
if (error) {
|
|
||||||
updatedErrors[id] = error.details[0].message;
|
|
||||||
} else {
|
|
||||||
delete updatedErrors[id];
|
|
||||||
}
|
|
||||||
return updatedErrors;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSearch = (value) => {
|
const handleSearch = (value) => {
|
||||||
setSearch(value);
|
setSearch(value);
|
||||||
};
|
};
|
||||||
@@ -224,20 +215,8 @@ const CreateMaintenance = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
const { error } = maintenanceWindowValidation.validate(form, {
|
if(hasValidationErrors(form, maintenanceWindowValidation, setErrors))
|
||||||
abortEarly: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
// If errors, return early
|
|
||||||
if (error) {
|
|
||||||
const newErrors = {};
|
|
||||||
error.details.forEach((err) => {
|
|
||||||
newErrors[err.path[0]] = err.message;
|
|
||||||
});
|
|
||||||
setErrors(newErrors);
|
|
||||||
logger.error(error);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
// Build timestamp for maintenance window from startDate and startTime
|
// Build timestamp for maintenance window from startDate and startTime
|
||||||
const start = dayjs(form.startDate)
|
const start = dayjs(form.startDate)
|
||||||
.set("hour", form.startTime.hour())
|
.set("hour", form.startTime.hour())
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
const buildErrors = (prev, id, error) => {
|
||||||
|
const updatedErrors = { ...prev };
|
||||||
|
if (error) {
|
||||||
|
updatedErrors[id] = error.details[0].message;
|
||||||
|
} else {
|
||||||
|
delete updatedErrors[id];
|
||||||
|
}
|
||||||
|
return updatedErrors;
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasValidationErrors = (form, validation, setErrors) => {
|
||||||
|
const { error } = validation.validate(form, {
|
||||||
|
abortEarly: false,
|
||||||
|
});
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
export { buildErrors, hasValidationErrors };
|
||||||
@@ -128,10 +128,43 @@ const maintenanceWindowValidation = joi.object({
|
|||||||
monitors: joi.array().min(1),
|
monitors: joi.array().min(1),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const advancedSettingsValidation = joi.object({
|
||||||
|
apiBaseUrl: joi.string().uri({ allowRelative: true }).trim().messages({
|
||||||
|
"string.empty": "API base url is required.",
|
||||||
|
"string.uri": "The URL you provided is not valid.",
|
||||||
|
}),
|
||||||
|
logLevel: joi.string(),
|
||||||
|
systemEmailHost: joi.string().allow(''),
|
||||||
|
systemEmailPort: joi.string().allow(''),
|
||||||
|
systemEmailAddress: joi
|
||||||
|
.string().allow(''),
|
||||||
|
systemEmailPassword: joi.string().allow(''),
|
||||||
|
jwtTTL: joi
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.messages({
|
||||||
|
"string.empty": "JWT TTL is required."
|
||||||
|
}),
|
||||||
|
dbType: joi.string().trim().messages({
|
||||||
|
"string.empty": "DB type is required",
|
||||||
|
}),
|
||||||
|
redisHost: joi.string().trim().messages({
|
||||||
|
"string.empty": "Redis host is required",
|
||||||
|
}),
|
||||||
|
redisPort: joi.string().allow('').custom((value, helpers) => {
|
||||||
|
if(value && isNaN(parseInt(value))){
|
||||||
|
return helpers.message("Redit port must be a number")
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}),
|
||||||
|
pagespeedApiKey: joi.string().allow('')
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
credentials,
|
credentials,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
monitorValidation,
|
monitorValidation,
|
||||||
settingsValidation,
|
settingsValidation,
|
||||||
maintenanceWindowValidation,
|
maintenanceWindowValidation,
|
||||||
|
advancedSettingsValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user