simplify form
This commit is contained in:
1 parent
bbb2da3935
commit
cdbf5fd0c0
2 files changed
+61
-56
No files matched your search
@@ -1,9 +1,15 @@
|
|||||||
import { BasePage, ConfigBox } from "@/Components/v2/design-elements";
|
import { BasePage, ConfigBox } from "@/Components/v2/design-elements";
|
||||||
import { Autocomplete, Select } from "@/Components/v2/inputs";
|
import { Autocomplete, Select } from "@/Components/v2/inputs";
|
||||||
import { Stack, useTheme, MenuItem, type SelectChangeEvent } from "@mui/material";
|
import { Stack, useTheme, MenuItem, type SelectChangeEvent } from "@mui/material";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import DummyChart from "@/Pages/Settings/DummyChart";
|
import DummyChart from "@/Pages/Settings/DummyChart";
|
||||||
|
import { useGet } from "@/Hooks/UseApi";
|
||||||
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||||
|
import type { SettingsFormData } from "@/Validation/settings";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
setTimezone,
|
setTimezone,
|
||||||
@@ -25,6 +31,23 @@ export const SettingsPage = () => {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { t, i18n } = useTranslation();
|
const { t, i18n } = useTranslation();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
// Fetch settings data from API
|
||||||
|
const { data: fetchedSettings } = useGet<any>("/settings");
|
||||||
|
|
||||||
|
// Initialize form with schema and defaults
|
||||||
|
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
||||||
|
|
||||||
|
const form = useForm<SettingsFormData>({
|
||||||
|
resolver: zodResolver(schema),
|
||||||
|
defaultValues: defaults,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reset form when defaults change
|
||||||
|
useEffect(() => {
|
||||||
|
form.reset(defaults);
|
||||||
|
}, [defaults, form]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
timezone: selectedTimezoneId,
|
timezone: selectedTimezoneId,
|
||||||
mode,
|
mode,
|
||||||
@@ -61,7 +84,7 @@ export const SettingsPage = () => {
|
|||||||
const languages = Object.keys(i18n.options.resources || {});
|
const languages = Object.keys(i18n.options.resources || {});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BasePage>
|
<BasePage component="form">
|
||||||
<Stack gap={theme.spacing(8)}>
|
<Stack gap={theme.spacing(8)}>
|
||||||
<ConfigBox
|
<ConfigBox
|
||||||
title={t("pages.settings.form.timezone.title")}
|
title={t("pages.settings.form.timezone.title")}
|
||||||
|
|||||||
@@ -1,69 +1,51 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
// Helper to normalize empty strings to undefined
|
|
||||||
const optionalString = () =>
|
|
||||||
z.preprocess(
|
|
||||||
(val) =>
|
|
||||||
val === "" || val === null || val === undefined ? undefined : String(val).trim(),
|
|
||||||
z.string().optional()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Helper for threshold fields
|
|
||||||
const thresholdField = (min: number, max: number, unit: string) =>
|
|
||||||
z.preprocess(
|
|
||||||
(val) => (val === "" || val === null || val === undefined ? undefined : val),
|
|
||||||
z.coerce
|
|
||||||
.number()
|
|
||||||
.int()
|
|
||||||
.min(min, `Min ${min}${unit}`)
|
|
||||||
.max(max, `Max ${max}${unit}`)
|
|
||||||
.optional()
|
|
||||||
);
|
|
||||||
|
|
||||||
export const settingsSchema = z.object({
|
export const settingsSchema = z.object({
|
||||||
systemEmailIgnoreTLS: z.boolean(),
|
systemEmailIgnoreTLS: z.boolean(),
|
||||||
systemEmailRequireTLS: z.boolean(),
|
systemEmailRequireTLS: z.boolean(),
|
||||||
systemEmailRejectUnauthorized: z.boolean(),
|
systemEmailRejectUnauthorized: z.boolean(),
|
||||||
systemEmailConnectionHost: optionalString(),
|
systemEmailConnectionHost: z
|
||||||
|
.string()
|
||||||
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
|
.optional(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
systemEmailPool: z.boolean().optional(),
|
systemEmailPool: z.boolean().optional(),
|
||||||
showURL: z.boolean().optional(),
|
showURL: z.boolean().optional(),
|
||||||
checkTTL: z.coerce
|
checkTTL: z.number().int().min(1, "Please enter a value").max(365, "Maximum 365 days"),
|
||||||
.number()
|
pagespeedApiKey: z
|
||||||
.int()
|
.string()
|
||||||
.min(1, "Please enter a value")
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
.max(365, "Maximum 365 days"),
|
.optional(),
|
||||||
pagespeedApiKey: optionalString(),
|
systemEmailHost: z
|
||||||
systemEmailHost: z.preprocess(
|
.string()
|
||||||
(val) =>
|
.regex(/^[a-zA-Z0-9.-]*$/, "Invalid hostname or IP address")
|
||||||
val === "" || val === null || val === undefined ? undefined : String(val).trim(),
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
z
|
.optional(),
|
||||||
.string()
|
systemEmailPort: z.number().int().min(1).max(65535).optional(),
|
||||||
.regex(/^[a-zA-Z0-9.-]+$/, "Invalid hostname or IP address")
|
systemEmailAddress: z
|
||||||
.optional()
|
.string()
|
||||||
),
|
.email("Please enter a valid email address")
|
||||||
systemEmailPort: z.preprocess(
|
.or(z.literal(""))
|
||||||
(val) => (val === "" || val === null || val === undefined ? undefined : val),
|
.transform((val) => (val === "" ? undefined : val.toLowerCase().trim()))
|
||||||
z.coerce
|
.optional(),
|
||||||
.number()
|
systemEmailUser: z
|
||||||
.int()
|
.string()
|
||||||
.min(1, "Port must be at least 1")
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
.max(65535, "Port must be at most 65535")
|
.optional(),
|
||||||
.optional()
|
systemEmailPassword: z
|
||||||
),
|
.string()
|
||||||
systemEmailAddress: z.preprocess((val) => {
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
if (val === "" || val === null || val === undefined) return undefined;
|
.optional(),
|
||||||
return String(val).toLowerCase().trim();
|
systemEmailTLSServername: z
|
||||||
}, z.string().email("Please enter a valid email address").optional()),
|
.string()
|
||||||
systemEmailUser: optionalString(),
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
systemEmailPassword: optionalString(),
|
.optional(),
|
||||||
systemEmailTLSServername: optionalString(),
|
|
||||||
globalThresholds: z
|
globalThresholds: z
|
||||||
.object({
|
.object({
|
||||||
cpu: thresholdField(0, 100, "%"),
|
cpu: z.number().int().min(0).max(100).optional(),
|
||||||
memory: thresholdField(0, 100, "%"),
|
memory: z.number().int().min(0).max(100).optional(),
|
||||||
disk: thresholdField(0, 100, "%"),
|
disk: z.number().int().min(0).max(100).optional(),
|
||||||
temperature: thresholdField(0, 150, "°C"),
|
temperature: z.number().int().min(0).max(150).optional(),
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user