Revert "FE - Advanced Settings Page Validation and Error Handling"
This commit is contained in:
1 parent
c9864d9075
commit
238db5bc8f
4 files changed
+32
-92
No files matched your search
@@ -12,11 +12,10 @@ 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("/");
|
||||
@@ -40,12 +39,11 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
redisPort: "",
|
||||
pagespeedApiKey: "",
|
||||
});
|
||||
const [errors, setErrors] = useState({});
|
||||
|
||||
useEffect(() => {
|
||||
const getSettings = async () => {
|
||||
const action = await dispatch(getAppSettings({ authToken }));
|
||||
if (action.payload?.success) {
|
||||
if (action.payload.success) {
|
||||
setLocalSettings(action.payload.data);
|
||||
} else {
|
||||
createToast({ body: "Failed to get settings" });
|
||||
@@ -77,20 +75,9 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
const handleChange = (event) => {
|
||||
const { value, id } = event.target;
|
||||
setLocalSettings({ ...localSettings, [id]: value });
|
||||
const { error } = advancedSettingsValidation.validate(
|
||||
{ [id]: value },
|
||||
{
|
||||
abortEarly: false,
|
||||
}
|
||||
);
|
||||
setErrors((prev) => {
|
||||
return buildErrors(prev, id, error);
|
||||
});
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
if (hasValidationErrors(localSettings, advancedSettingsValidation, setErrors))
|
||||
return;
|
||||
const action = await dispatch(
|
||||
updateAppSettings({ settings: localSettings, authToken })
|
||||
);
|
||||
@@ -131,7 +118,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
label="API URL Host"
|
||||
value={localSettings.apiBaseUrl}
|
||||
onChange={handleChange}
|
||||
error={errors.apiBaseUrl}
|
||||
/>
|
||||
<Select
|
||||
id="logLevel"
|
||||
@@ -140,7 +126,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
items={logItems}
|
||||
value={logItemLookup[localSettings.logLevel]}
|
||||
onChange={handleLogLevel}
|
||||
error={errors.logLevel}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -148,8 +133,8 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
<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.
|
||||
Set your host email settings here. These settings are used for sending
|
||||
system emails.
|
||||
</Typography>
|
||||
</Box>
|
||||
<Stack gap={theme.spacing(20)}>
|
||||
@@ -160,7 +145,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailHost"
|
||||
value={localSettings.systemEmailHost}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailHost}
|
||||
/>
|
||||
<Field
|
||||
type="number"
|
||||
@@ -169,7 +153,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailPort"
|
||||
value={localSettings.systemEmailPort.toString()}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailPort}
|
||||
/>
|
||||
<Field
|
||||
type="email"
|
||||
@@ -178,7 +161,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailAddress"
|
||||
value={localSettings.systemEmailAddress}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailAddress}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -187,7 +169,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="systemEmailPassword"
|
||||
value={localSettings.systemEmailPassword}
|
||||
onChange={handleChange}
|
||||
error={errors.systemEmailPassword}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -206,7 +187,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="jwtTTL"
|
||||
value={localSettings.jwtTTL}
|
||||
onChange={handleChange}
|
||||
error={errors.jwtTTL}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -215,7 +195,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="dbType"
|
||||
value={localSettings.dbType}
|
||||
onChange={handleChange}
|
||||
error={errors.dbType}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -224,7 +203,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="redisHost"
|
||||
value={localSettings.redisHost}
|
||||
onChange={handleChange}
|
||||
error={errors.redisHost}
|
||||
/>
|
||||
<Field
|
||||
type="number"
|
||||
@@ -233,7 +211,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="redisPort"
|
||||
value={localSettings.redisPort.toString()}
|
||||
onChange={handleChange}
|
||||
error={errors.redisPort}
|
||||
/>
|
||||
<Field
|
||||
type="text"
|
||||
@@ -242,7 +219,6 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
name="pagespeedApiKey"
|
||||
value={localSettings.pagespeedApiKey}
|
||||
onChange={handleChange}
|
||||
error={errors.pagespeedApiKey}
|
||||
/>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -252,9 +228,7 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
</Box>
|
||||
<Box>
|
||||
<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.
|
||||
</Typography>
|
||||
<Link
|
||||
@@ -264,7 +238,10 @@ const AdvancedSettings = ({ isAdmin }) => {
|
||||
/>
|
||||
</Box>
|
||||
</ConfigBox>
|
||||
<Stack direction="row" justifyContent="flex-end">
|
||||
<Stack
|
||||
direction="row"
|
||||
justifyContent="flex-end"
|
||||
>
|
||||
<LoadingButton
|
||||
loading={settings.isLoading || settings.authIsLoading}
|
||||
variant="contained"
|
||||
|
||||
@@ -28,7 +28,6 @@ import {
|
||||
MS_PER_WEEK,
|
||||
} from "../../../Utils/timeUtils";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { buildErrors, hasValidationErrors } from "../../../Validation/error";
|
||||
|
||||
const getDurationAndUnit = (durationInMs) => {
|
||||
if (durationInMs % MS_PER_DAY === 0) {
|
||||
@@ -177,6 +176,16 @@ const CreateMaintenance = () => {
|
||||
fetchMonitors();
|
||||
}, [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) => {
|
||||
setSearch(value);
|
||||
};
|
||||
@@ -215,8 +224,20 @@ const CreateMaintenance = () => {
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if(hasValidationErrors(form, maintenanceWindowValidation, setErrors))
|
||||
const { error } = maintenanceWindowValidation.validate(form, {
|
||||
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;
|
||||
}
|
||||
// Build timestamp for maintenance window from startDate and startTime
|
||||
const start = dayjs(form.startDate)
|
||||
.set("hour", form.startTime.hour())
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
const buildErrors = (prev, id, error) => {
|
||||
const updatedErrors = { ...prev };
|
||||
if (error) {
|
||||
updatedErrors[id] = error.details[0].message?? "Validation error";
|
||||
} 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?? "Validation error";
|
||||
});
|
||||
setErrors(newErrors);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
export { buildErrors, hasValidationErrors };
|
||||
@@ -128,43 +128,10 @@ const maintenanceWindowValidation = joi.object({
|
||||
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().valid("debug", "none", "error", "warn").allow(""),
|
||||
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 {
|
||||
credentials,
|
||||
imageValidation,
|
||||
monitorValidation,
|
||||
settingsValidation,
|
||||
maintenanceWindowValidation,
|
||||
advancedSettingsValidation,
|
||||
};
|
||||
Reference in new issue
Block a user