[PageSpeed]: Renamed to isCreate to follow convention, removed redundant loading states, Added validation on update submit

This commit is contained in:
Jesulayomy committed 2025-07-10 15:21:27 -04:00
1 parent a71756a6ec
commit eaa85bb9c4
1 file changed
+50 -43
+50 -43
View File
@@ -35,37 +35,33 @@ import {
const PageSpeedSetup = () => { const PageSpeedSetup = () => {
const { monitorId } = useParams(); const { monitorId } = useParams();
const isConfigure = !!monitorId; const isCreate = typeof monitorId === "undefined";
const CRUMBS = [ const CRUMBS = [
{ name: "pagespeed", path: "/pagespeed" }, { name: "pagespeed", path: "/pagespeed" },
...(isConfigure ...(isCreate
? [ ? [{ name: "create", path: `/pagespeed/create` }]
: [
{ name: "details", path: `/pagespeed/${monitorId}` }, { name: "details", path: `/pagespeed/${monitorId}` },
{ name: "configure", path: `/pagespeed/configure/${monitorId}` }, { name: "configure", path: `/pagespeed/configure/${monitorId}` },
] ]),
: [{ name: "create", path: `/pagespeed/create` }]),
]; ];
// States // States
const [monitor, setMonitor] = useState( const [monitor, setMonitor] = useState(
isConfigure isCreate
? {} ? {
: {
url: "", url: "",
name: "", name: "",
type: "pagespeed", type: "pagespeed",
notifications: [], notifications: [],
interval: 180000, interval: 180000,
} }
: {}
); );
const [https, setHttps] = useState(true); const [https, setHttps] = useState(true);
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [updateTrigger, setUpdateTrigger] = useState(false); const [updateTrigger, setUpdateTrigger] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [isUpdating, setIsUpdating] = useState(false);
const [isPausing, setIsPausing] = useState(false);
// Setup // Setup
const { t } = useTranslation(); const { t } = useTranslation();
@@ -89,9 +85,9 @@ const PageSpeedSetup = () => {
// Hooks for API actions // Hooks for API actions
const [createMonitor, isCreating] = useCreateMonitor(); const [createMonitor, isCreating] = useCreateMonitor();
const [isLoading] = isConfigure const [isLoading] = isCreate
? useFetchMonitorById({ monitorId, setMonitor, updateTrigger }) ? [false]
: [false]; : useFetchMonitorById({ monitorId, setMonitor, updateTrigger });
const [deleteMonitor] = useDeleteMonitor(); const [deleteMonitor] = useDeleteMonitor();
const [updateMonitor] = useUpdateMonitor(); const [updateMonitor] = useUpdateMonitor();
const [pauseMonitor] = usePauseMonitor(); const [pauseMonitor] = usePauseMonitor();
@@ -99,9 +95,7 @@ const PageSpeedSetup = () => {
// Handlers // Handlers
const onSubmit = async (event) => { const onSubmit = async (event) => {
event.preventDefault(); event.preventDefault();
if (isConfigure) { if (isCreate) {
await updateMonitor({ monitor, redirect: "/pagespeed" });
} else {
let form = { let form = {
url: `http${https ? "s" : ""}://` + monitor.url, url: `http${https ? "s" : ""}://` + monitor.url,
name: monitor.name === "" ? monitor.url : monitor.name, name: monitor.name === "" ? monitor.url : monitor.name,
@@ -126,6 +120,25 @@ const PageSpeedSetup = () => {
notifications: monitor.notifications, notifications: monitor.notifications,
}; };
await createMonitor({ monitor: form, redirect: "/pagespeed" }); await createMonitor({ monitor: form, redirect: "/pagespeed" });
} else {
const monitorParams = {
url: monitor.url,
name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type,
interval: monitor.interval,
};
const { error } = monitorValidation.validate(monitorParams, { abortEarly: false });
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
console.log(newErrors);
createToast({ body: t("checkFormError") });
return;
}
await updateMonitor({ monitor, redirect: "/pagespeed" });
} }
}; };
@@ -173,7 +186,7 @@ const PageSpeedSetup = () => {
return ( return (
<Box <Box
className={isConfigure ? "configure-pagespeed" : "create-monitor"} className={!isCreate ? "configure-pagespeed" : "create-monitor"}
sx={{ sx={{
"& h1": { color: theme.palette.primary.contrastText }, "& h1": { color: theme.palette.primary.contrastText },
}} }}
@@ -185,7 +198,7 @@ const PageSpeedSetup = () => {
<Breadcrumbs list={CRUMBS} /> <Breadcrumbs list={CRUMBS} />
<Stack <Stack
component="form" component="form"
className={isConfigure ? "" : "create-monitor-form"} className={!isCreate ? "" : "create-monitor-form"}
onSubmit={onSubmit} onSubmit={onSubmit}
noValidate noValidate
spellCheck="false" spellCheck="false"
@@ -205,14 +218,12 @@ const PageSpeedSetup = () => {
component="span" component="span"
fontSize="inherit" fontSize="inherit"
color={ color={
isConfigure !isCreate ? theme.palette.primary.contrastTextSecondary : undefined
? theme.palette.primary.contrastTextSecondary
: undefined
} }
> >
{isConfigure ? monitor.name : t("createYour") + " "} {!isCreate ? monitor.name : t("createYour") + " "}
</Typography> </Typography>
{!isConfigure ? ( {isCreate ? (
<Typography <Typography
component="span" component="span"
fontSize="inherit" fontSize="inherit"
@@ -225,7 +236,7 @@ const PageSpeedSetup = () => {
<></> <></>
)} )}
</Typography> </Typography>
{isConfigure && ( {!isCreate && (
<Stack <Stack
direction="row" direction="row"
alignItems="center" alignItems="center"
@@ -281,7 +292,7 @@ const PageSpeedSetup = () => {
</Stack> </Stack>
)} )}
</Box> </Box>
{isConfigure && ( {!isCreate && (
<Box <Box
alignSelf="flex-end" alignSelf="flex-end"
ml="auto" ml="auto"
@@ -340,7 +351,7 @@ const PageSpeedSetup = () => {
</Typography> </Typography>
</Box> </Box>
<Stack <Stack
gap={isConfigure ? theme.spacing(20) : theme.spacing(15)} gap={!isCreate ? theme.spacing(20) : theme.spacing(15)}
sx={{ sx={{
".MuiInputBase-root:has(> .Mui-disabled)": { ".MuiInputBase-root:has(> .Mui-disabled)": {
backgroundColor: theme.palette.tertiary.main, backgroundColor: theme.palette.tertiary.main,
@@ -351,17 +362,15 @@ const PageSpeedSetup = () => {
type={"url"} type={"url"}
name="url" name="url"
id="monitor-url" id="monitor-url"
label={isConfigure ? t("url") : t("urlMonitor")} label={!isCreate ? t("url") : t("urlMonitor")}
startAdornment={ startAdornment={isCreate ? <HttpAdornment https={https} /> : undefined}
!isConfigure ? <HttpAdornment https={https} /> : undefined
}
placeholder="random.website.com" placeholder="random.website.com"
value={monitor.url || ""} value={monitor.url || ""}
onChange={handleChange} onChange={handleChange}
onBlur={!isConfigure ? handleBlur : undefined} onBlur={isCreate ? handleBlur : undefined}
error={!!errors["url"]} error={!!errors["url"]}
helperText={errors["url"]} helperText={errors["url"]}
disabled={isConfigure} disabled={!isCreate}
/> />
<TextInput <TextInput
type="text" type="text"
@@ -377,7 +386,7 @@ const PageSpeedSetup = () => {
/> />
</Stack> </Stack>
</ConfigBox> </ConfigBox>
{!isConfigure && ( {isCreate && (
<ConfigBox> <ConfigBox>
<Box> <Box>
<Typography <Typography
@@ -448,7 +457,7 @@ const PageSpeedSetup = () => {
<NotificationsConfig <NotificationsConfig
notifications={notifications} notifications={notifications}
setMonitor={setMonitor} setMonitor={setMonitor}
setNotifications={isConfigure ? monitor.notifications : undefined} setNotifications={isCreate ? undefined : monitor.notifications}
/> />
</ConfigBox> </ConfigBox>
<ConfigBox> <ConfigBox>
@@ -460,7 +469,7 @@ const PageSpeedSetup = () => {
{t("distributedUptimeCreateAdvancedSettings")} {t("distributedUptimeCreateAdvancedSettings")}
</Typography> </Typography>
</Box> </Box>
<Stack gap={theme.spacing(isConfigure ? 20 : 12)}> <Stack gap={theme.spacing(isCreate ? 12 : 20)}>
<Select <Select
id="monitor-interval" id="monitor-interval"
name="interval" name="interval"
@@ -474,23 +483,22 @@ const PageSpeedSetup = () => {
<Stack <Stack
direction="row" direction="row"
justifyContent="flex-end" justifyContent="flex-end"
mt={isConfigure ? "auto" : undefined} mt={isCreate ? undefined : "auto"}
> >
<Button <Button
type="submit" type="submit"
variant="contained" variant="contained"
color="accent" color="accent"
disabled={!Object.values(errors).every((value) => value === undefined)} disabled={!Object.values(errors).every((value) => value === undefined)}
loading={isLoading || isDeleting || isUpdating || isPausing || isCreating} sx={isCreate ? undefined : { px: theme.spacing(12) }}
sx={isConfigure ? { px: theme.spacing(12) } : undefined}
> >
{isConfigure ? t("settingsSave") : t("createMonitor")} {isCreate ? t("createMonitor") : t("settingsSave")}
</Button> </Button>
</Stack> </Stack>
</Stack> </Stack>
</> </>
)} )}
{isConfigure && ( {!isCreate && (
<Dialog <Dialog
open={isOpen} open={isOpen}
theme={theme} theme={theme}
@@ -499,7 +507,6 @@ const PageSpeedSetup = () => {
onCancel={() => setIsOpen(false)} onCancel={() => setIsOpen(false)}
confirmationButtonLabel={t("delete")} confirmationButtonLabel={t("delete")}
onConfirm={handleRemove} onConfirm={handleRemove}
isLoading={isLoading || isDeleting || isUpdating || isPausing}
/> />
)} )}
</Box> </Box>