diff --git a/client/src/Hooks/useSettingsForm.ts b/client/src/Hooks/useSettingsForm.ts index 6abf386be..17a67af25 100644 --- a/client/src/Hooks/useSettingsForm.ts +++ b/client/src/Hooks/useSettingsForm.ts @@ -4,32 +4,34 @@ import type { Settings } from "@/Types/Settings"; import type { SettingsFormData } from "@/Validation/settings"; interface UseSettingsFormOptions { - data?: Settings | null; + data?: Settings | null; } export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => { - return useMemo(() => { - const defaults: SettingsFormData = { - systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS || false, - systemEmailRequireTLS: data?.systemEmailRequireTLS || false, - systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized || true, - systemEmailConnectionHost: data?.systemEmailConnectionHost || "", - systemEmailSecure: data?.systemEmailSecure || true, - systemEmailPool: data?.systemEmailPool || false, - showURL: data?.showURL || false, - //timezone: data?.timezone || "UTC", - language: data?.language || "en", - //chartType: data?.chartType || "histogram", - checkTTL: data?.checkTTL || 30, - pagespeedApiKey: data?.pagespeedApiKey || "", - systemEmailHost: data?.systemEmailHost || "", - systemEmailPort: data?.systemEmailPort || "", - systemEmailAddress: data?.systemEmailAddress || "", - systemEmailUser: data?.systemEmailUser || "", - systemEmailPassword: data?.systemEmailPassword || "", - - } + return useMemo(() => { + const defaults: SettingsFormData = { + systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS || false, + systemEmailRequireTLS: data?.systemEmailRequireTLS || false, + systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized || true, + systemEmailSecure: data?.systemEmailSecure || false, + systemEmailPool: data?.systemEmailPool || false, + showURL: data?.showURL || false, + systemEmailHost: data?.systemEmailHost || "", + systemEmailUser: data?.systemEmailUser || "", + systemEmailAddress: data?.systemEmailAddress || "", + systemEmailConnectionHost: data?.systemEmailConnectionHost || "localhost", + systemEmailTLSServername: data?.systemEmailTLSServername || "", + systemEmailPort: data?.systemEmailPort || "", + globalThresholds: { + cpu: data?.globalThresholds?.cpu || "", + memory: data?.globalThresholds?.memory || "", + disk: data?.globalThresholds?.disk || "", + temperature: data?.globalThresholds?.temperature || "", + }, + checkTTL: data?.checkTTL || 30, + pagespeedApiKey: "", + systemEmailPassword: "", + }; - - - } -} \ No newline at end of file + return { schema: settingsSchema, defaults }; + }, [data]); +}; diff --git a/client/src/Pages/Settings/SettingsDemoMonitors.jsx b/client/src/Pages/Settings/SettingsDemoMonitors.jsx index 47e699851..eb7f86c7a 100644 --- a/client/src/Pages/Settings/SettingsDemoMonitors.jsx +++ b/client/src/Pages/Settings/SettingsDemoMonitors.jsx @@ -8,8 +8,9 @@ import { PropTypes } from "prop-types"; import { useTranslation } from "react-i18next"; import Dialog from "@/Components/v1/Dialog/index.jsx"; import { useState } from "react"; +import { useDelete, usePost } from "@/Hooks/UseApi"; -const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, handleChange, isLoading }) => { +const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, isLoading }) => { const { t } = useTranslation(); const theme = useTheme(); // Local state @@ -18,7 +19,8 @@ const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, handleChange, isLoading }) = if (!isAdmin) { return null; } - + const { post: postDemoMonitors } = usePost(); + const { deleteFn: deleteAllMonitorsFn } = useDelete(); return ( <> @@ -38,13 +40,8 @@ const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, handleChange, isLoading }) = variant="contained" color="accent" loading={isLoading} - onClick={() => { - const syntheticEvent = { - target: { - name: "demo", - }, - }; - handleChange(syntheticEvent); + onClick={async () => { + await postDemoMonitors("/monitors/demo", {}); }} sx={{ mt: theme.spacing(4) }} > @@ -81,13 +78,8 @@ const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, handleChange, isLoading }) = title={t("settingsPage.systemResetSettings.dialogTitle")} onCancel={() => setIsOpen(false)} confirmationButtonLabel={t("settingsPage.systemResetSettings.dialogConfirm")} - onConfirm={() => { - const syntheticEvent = { - target: { - name: "deleteMonitors", - }, - }; - handleChange(syntheticEvent); + onConfirm={async () => { + await deleteAllMonitorsFn("/monitors/"); setIsOpen(false); }} isLoading={isLoading} @@ -99,7 +91,6 @@ const SettingsDemoMonitors = ({ isAdmin, HEADER_SX, handleChange, isLoading }) = SettingsDemoMonitors.propTypes = { isAdmin: PropTypes.bool, - handleChange: PropTypes.func, HEADER_SX: PropTypes.object, }; diff --git a/client/src/Pages/Settings/SettingsEmail.jsx b/client/src/Pages/Settings/SettingsEmail.jsx index f31373ed9..024fd5854 100644 --- a/client/src/Pages/Settings/SettingsEmail.jsx +++ b/client/src/Pages/Settings/SettingsEmail.jsx @@ -15,16 +15,18 @@ import { PasswordEndAdornment } from "@/Components/v1/Inputs/TextInput/Adornment import { usePost } from "@/Hooks/UseApi"; import { useSelector } from "react-redux"; import { createToast } from "@/Utils/toastUtils.jsx"; +import { Controller } from "react-hook-form"; const SettingsEmail = ({ isAdmin, HEADER_SX, - handleChange, - settingsData, - setSettingsData, isEmailPasswordSet, emailPasswordHasBeenReset, setEmailPasswordHasBeenReset, + control, + defaults, + formValues, + setValue, }) => { // Setup const { t } = useTranslation(); @@ -44,22 +46,13 @@ const SettingsEmail = ({ systemEmailIgnoreTLS = false, systemEmailRequireTLS = false, systemEmailRejectUnauthorized = true, - } = settingsData?.settings || {}; - // Local state - const [password, setPassword] = useState(""); + } = formValues || {}; // Network const { post: sendTestEmailFn, loading: isSending } = usePost(); const user = useSelector((state) => state.auth.user); // Handlers - const handlePasswordChange = (e) => { - setPassword(e.target.value); - setSettingsData({ - ...settingsData, - settings: { ...settingsData.settings, systemEmailPassword: e.target.value }, - }); - }; /** * Handle sending test email with current form values @@ -70,7 +63,7 @@ const SettingsEmail = ({ !systemEmailHost || !systemEmailPort || !systemEmailAddress || - !(password || systemEmailPassword) + !systemEmailPassword ) { createToast({ body: t("settingsPage.emailSettings.toastEmailRequiredFieldsError"), @@ -117,52 +110,87 @@ const SettingsEmail = ({ - ( + + )} /> - ( + + )} /> - ( + + )} /> - ( + + )} /> {(isEmailPasswordSet === false || emailPasswordHasBeenReset === true) && ( - } + control={control} + defaultValue={defaults.systemEmailPassword} + render={({ field, fieldState }) => ( + } + error={!!fieldState.error} + helperText={fieldState.error?.message} + /> + )} /> )} @@ -172,11 +200,6 @@ const SettingsEmail = ({ {t("settingsPage.emailSettings.labelPasswordSet")}