diff --git a/client/src/Hooks/UseApi.ts b/client/src/Hooks/UseApi.ts index 08ef9a9a5..dfd256bdd 100644 --- a/client/src/Hooks/UseApi.ts +++ b/client/src/Hooks/UseApi.ts @@ -1,7 +1,7 @@ import useSWR from "swr"; import type { SWRConfiguration } from "swr"; import type { AxiosRequestConfig } from "axios"; -import { get, patch, post, deleteOp } from "@/Utils/ApiClient"; +import { get, patch, post, put, deleteOp } from "@/Utils/ApiClient"; import { useState } from "react"; import { useToast } from "@/Hooks/UseToast"; @@ -109,6 +109,42 @@ export const usePatch = () => { return { patch: patchFn, loading, error }; }; +export const usePut = () => { + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + const { toastError, toastSuccess } = useToast(); + + const putFn = async ( + endpoint: string, + body?: B, + config?: AxiosRequestConfig + ): Promise | null> => { + setLoading(true); + setError(null); + + try { + const res = await put>(endpoint, body, { + ...config, + headers: { + ...config?.headers, + }, + }); + toastSuccess(res.data?.msg || "Operation successful"); + return res.data; + } catch (err: any) { + console.error(err); + const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; + toastError(errMsg); + setError(errMsg); + return null; + } finally { + setLoading(false); + } + }; + + return { put: putFn, loading, error }; +}; + export const useDelete = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); diff --git a/client/src/Hooks/useMonitorForm.ts b/client/src/Hooks/useMonitorForm.ts index 2d7df97ab..50dc7b920 100644 --- a/client/src/Hooks/useMonitorForm.ts +++ b/client/src/Hooks/useMonitorForm.ts @@ -9,6 +9,7 @@ interface UseMonitorFormOptions { const getBaseDefaults = (data?: Monitor | null) => ({ name: data?.name || "", + description: data?.description || "", interval: data?.interval || 60000, notifications: data?.notifications || [], statusWindowSize: data?.statusWindowSize || 5, diff --git a/client/src/Pages/CreateMonitor/index.tsx b/client/src/Pages/CreateMonitor/index.tsx index efa1c9484..28515b236 100644 --- a/client/src/Pages/CreateMonitor/index.tsx +++ b/client/src/Pages/CreateMonitor/index.tsx @@ -1,6 +1,6 @@ import { useMemo } from "react"; import { useEffect } from "react"; -import { useParams } from "react-router"; +import { useParams, useLocation, useNavigate } from "react-router"; import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTheme } from "@mui/material"; @@ -24,7 +24,7 @@ import { SwitchComponent as Switch, SliderWithLabel, } from "@/Components/v2/inputs"; -import { useGet } from "@/Hooks/UseApi"; +import { useGet, usePost, usePut } from "@/Hooks/UseApi"; import { useMonitorForm } from "@/Hooks/useMonitorForm"; import type { Monitor, MonitorType } from "@/Types/Monitor"; import type { Notification } from "@/Types/Notification"; @@ -116,8 +116,22 @@ const CreateMonitorPage = () => { const theme = useTheme(); const { t } = useTranslation(); const { monitorId } = useParams(); + const location = useLocation(); + const navigate = useNavigate(); const isEditMode = Boolean(monitorId); + // Extract page type from URL path (e.g., /pagespeed/create -> pagespeed) + const pageType = useMemo(() => { + const pathSegments = location.pathname.split("/").filter(Boolean); + const firstSegment = pathSegments[0]; + if (firstSegment === "pagespeed") return "pagespeed"; + if (firstSegment === "infrastructure") return "hardware"; + return "uptime"; + }, [location.pathname]); + + const showTypeSelector = pageType === "uptime" && !isEditMode; + const defaultType: MonitorType = pageType === "pagespeed" ? "pagespeed" : pageType === "hardware" ? "hardware" : "http"; + const { data: existingMonitor } = useGet( isEditMode ? `/monitors/${monitorId}` : null ); @@ -125,7 +139,7 @@ const CreateMonitorPage = () => { // Fetch notifications for the team const { data: notifications } = useGet("/notifications/team"); - const { schema, defaults } = useMonitorForm({ data: existingMonitor ?? null }); + const { schema, defaults } = useMonitorForm({ data: existingMonitor ?? null, defaultType }); const form = useForm({ resolver: zodResolver(schema), @@ -148,8 +162,28 @@ const CreateMonitorPage = () => { [watchedType, t] ); + const { post, loading: isCreating } = usePost(); + const { put, loading: isUpdating } = usePut(); + const isSubmitting = isCreating || isUpdating; + const onSubmit = async (data: MonitorFormData) => { - console.log(data); + let result; + if (isEditMode && monitorId) { + result = await put(`/monitors/${monitorId}`, data); + } else { + result = await post("/monitors", data); + } + + if (result?.success) { + // Navigate based on page type + if (pageType === "pagespeed") { + navigate("/pagespeed"); + } else if (pageType === "hardware") { + navigate("/infrastructure"); + } else { + navigate("/uptime"); + } + } }; const onError = (errors: unknown) => { @@ -161,67 +195,55 @@ const CreateMonitorPage = () => { component="form" onSubmit={handleSubmit(onSubmit, onError)} > - {/* Monitor Type Selection */} - ( - - - - - - - - - - - - )} - /> - } - /> + {/* Monitor Type Selection - only shown for uptime monitors */} + {showTypeSelector && ( + ( + + + + + + + + + + )} + /> + } + /> + )} {/* General Settings - Dynamic based on type */} { fieldLabel={generalSettingsConfig.urlLabel} placeholder={generalSettingsConfig.urlPlaceholder} fullWidth + disabled={isEditMode} error={!!fieldState.error} helperText={fieldState.error?.message ?? ""} /> @@ -506,7 +529,7 @@ const CreateMonitorPage = () => { justifyContent="flex-end" >