From eaa85bb9c46aa61527a43ff2f6f45e1135102bb8 Mon Sep 17 00:00:00 2001 From: Jesulayomy Date: Thu, 10 Jul 2025 15:21:27 -0400 Subject: [PATCH] [PageSpeed]: Renamed to isCreate to follow convention, removed redundant loading states, Added validation on update submit --- client/src/Pages/PageSpeed/Create/index.jsx | 93 +++++++++++---------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/client/src/Pages/PageSpeed/Create/index.jsx b/client/src/Pages/PageSpeed/Create/index.jsx index c2ab81add..8941d8c9a 100644 --- a/client/src/Pages/PageSpeed/Create/index.jsx +++ b/client/src/Pages/PageSpeed/Create/index.jsx @@ -35,37 +35,33 @@ import { const PageSpeedSetup = () => { const { monitorId } = useParams(); - const isConfigure = !!monitorId; + const isCreate = typeof monitorId === "undefined"; const CRUMBS = [ { name: "pagespeed", path: "/pagespeed" }, - ...(isConfigure - ? [ + ...(isCreate + ? [{ name: "create", path: `/pagespeed/create` }] + : [ { name: "details", path: `/pagespeed/${monitorId}` }, { name: "configure", path: `/pagespeed/configure/${monitorId}` }, - ] - : [{ name: "create", path: `/pagespeed/create` }]), + ]), ]; // States const [monitor, setMonitor] = useState( - isConfigure - ? {} - : { + isCreate + ? { url: "", name: "", type: "pagespeed", notifications: [], interval: 180000, } + : {} ); const [https, setHttps] = useState(true); const [errors, setErrors] = useState({}); - const [isOpen, setIsOpen] = useState(false); const [updateTrigger, setUpdateTrigger] = useState(false); - const [isDeleting, setIsDeleting] = useState(false); - const [isUpdating, setIsUpdating] = useState(false); - const [isPausing, setIsPausing] = useState(false); // Setup const { t } = useTranslation(); @@ -89,9 +85,9 @@ const PageSpeedSetup = () => { // Hooks for API actions const [createMonitor, isCreating] = useCreateMonitor(); - const [isLoading] = isConfigure - ? useFetchMonitorById({ monitorId, setMonitor, updateTrigger }) - : [false]; + const [isLoading] = isCreate + ? [false] + : useFetchMonitorById({ monitorId, setMonitor, updateTrigger }); const [deleteMonitor] = useDeleteMonitor(); const [updateMonitor] = useUpdateMonitor(); const [pauseMonitor] = usePauseMonitor(); @@ -99,9 +95,7 @@ const PageSpeedSetup = () => { // Handlers const onSubmit = async (event) => { event.preventDefault(); - if (isConfigure) { - await updateMonitor({ monitor, redirect: "/pagespeed" }); - } else { + if (isCreate) { let form = { url: `http${https ? "s" : ""}://` + monitor.url, name: monitor.name === "" ? monitor.url : monitor.name, @@ -126,6 +120,25 @@ const PageSpeedSetup = () => { notifications: monitor.notifications, }; 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 ( { { component="span" fontSize="inherit" color={ - isConfigure - ? theme.palette.primary.contrastTextSecondary - : undefined + !isCreate ? theme.palette.primary.contrastTextSecondary : undefined } > - {isConfigure ? monitor.name : t("createYour") + " "} + {!isCreate ? monitor.name : t("createYour") + " "} - {!isConfigure ? ( + {isCreate ? ( { <> )} - {isConfigure && ( + {!isCreate && ( { )} - {isConfigure && ( + {!isCreate && ( { .Mui-disabled)": { backgroundColor: theme.palette.tertiary.main, @@ -351,17 +362,15 @@ const PageSpeedSetup = () => { type={"url"} name="url" id="monitor-url" - label={isConfigure ? t("url") : t("urlMonitor")} - startAdornment={ - !isConfigure ? : undefined - } + label={!isCreate ? t("url") : t("urlMonitor")} + startAdornment={isCreate ? : undefined} placeholder="random.website.com" value={monitor.url || ""} onChange={handleChange} - onBlur={!isConfigure ? handleBlur : undefined} + onBlur={isCreate ? handleBlur : undefined} error={!!errors["url"]} helperText={errors["url"]} - disabled={isConfigure} + disabled={!isCreate} /> { /> - {!isConfigure && ( + {isCreate && ( { @@ -460,7 +469,7 @@ const PageSpeedSetup = () => { {t("distributedUptimeCreateAdvancedSettings")} - +