update client validation/pages
This commit is contained in:
1 parent
48e71d4345
commit
d3605b0149
3 files changed
+13
-26
No files matched your search
@@ -1,14 +1,14 @@
|
|||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { settingsSchema } from "@/Validation/settings";
|
import { settingsSchema } from "@/Validation/settings";
|
||||||
import type { Settings } from "@/Types/Settings";
|
import type { Settings } from "@/Types/Settings";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormInput } from "@/Validation/settings";
|
||||||
|
|
||||||
interface UseSettingsFormOptions {
|
interface UseSettingsFormOptions {
|
||||||
data?: Settings | null;
|
data?: Settings | null;
|
||||||
}
|
}
|
||||||
export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => {
|
export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
const defaults: SettingsFormData = {
|
const defaults: SettingsFormInput = {
|
||||||
systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS ?? false,
|
systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS ?? false,
|
||||||
systemEmailRequireTLS: data?.systemEmailRequireTLS ?? false,
|
systemEmailRequireTLS: data?.systemEmailRequireTLS ?? false,
|
||||||
systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized ?? true,
|
systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized ?? true,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { useGet, usePatch, usePost, useLazyGet } from "@/Hooks/UseApi";
|
|||||||
import { useToast } from "@/Hooks/UseToast";
|
import { useToast } from "@/Hooks/UseToast";
|
||||||
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormData, SettingsFormInput } from "@/Validation/settings";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Controller } from "react-hook-form";
|
import { Controller } from "react-hook-form";
|
||||||
import { TextField, Button, FieldLabel, SliderWithLabel } from "@/Components/inputs";
|
import { TextField, Button, FieldLabel, SliderWithLabel } from "@/Components/inputs";
|
||||||
@@ -89,7 +89,7 @@ export const SettingsPage = () => {
|
|||||||
// Initialize form with schema and defaults
|
// Initialize form with schema and defaults
|
||||||
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
||||||
|
|
||||||
const form = useForm<SettingsFormData>({
|
const form = useForm<SettingsFormInput, unknown, SettingsFormData>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
mode: "onChange",
|
mode: "onChange",
|
||||||
@@ -256,20 +256,7 @@ export const SettingsPage = () => {
|
|||||||
delete (dataToSend as any).systemEmailPassword;
|
delete (dataToSend as any).systemEmailPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert undefined to empty string for backend unsetting
|
const result = await patch("/settings", dataToSend as SettingsFormData);
|
||||||
const processedData = Object.entries(dataToSend).reduce((acc, [key, value]) => {
|
|
||||||
const typedKey = key as keyof SettingsFormData;
|
|
||||||
if (value === undefined) {
|
|
||||||
(acc as any)[typedKey] = "";
|
|
||||||
} else if (typeof value === "object" && value !== null) {
|
|
||||||
(acc as any)[typedKey] = value;
|
|
||||||
} else {
|
|
||||||
(acc as any)[typedKey] = value;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, {} as Partial<SettingsFormData>);
|
|
||||||
|
|
||||||
const result = await patch("/settings", processedData as SettingsFormData);
|
|
||||||
|
|
||||||
if (result?.success) {
|
if (result?.success) {
|
||||||
// Update API key state from response
|
// Update API key state from response
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export const settingsSchema = z.object({
|
|||||||
systemEmailRejectUnauthorized: z.boolean(),
|
systemEmailRejectUnauthorized: z.boolean(),
|
||||||
systemEmailConnectionHost: z
|
systemEmailConnectionHost: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
systemEmailPool: z.boolean().optional(),
|
systemEmailPool: z.boolean().optional(),
|
||||||
@@ -19,31 +19,30 @@ export const settingsSchema = z.object({
|
|||||||
.max(CHECK_TTL_SENTINEL, `Maximum ${CHECK_TTL_SENTINEL}`),
|
.max(CHECK_TTL_SENTINEL, `Maximum ${CHECK_TTL_SENTINEL}`),
|
||||||
pagespeedApiKey: z
|
pagespeedApiKey: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailHost: z
|
systemEmailHost: z
|
||||||
.string()
|
.string()
|
||||||
.regex(/^[a-zA-Z0-9.-]*$/, "Invalid hostname or IP address")
|
.regex(/^[a-zA-Z0-9.-]*$/, "Invalid hostname or IP address")
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailPort: z.number().int().min(1).max(65535).optional(),
|
systemEmailPort: z.number().int().min(1).max(65535).optional(),
|
||||||
systemEmailAddress: z
|
systemEmailAddress: z
|
||||||
.string()
|
|
||||||
.email("Please enter a valid email address")
|
.email("Please enter a valid email address")
|
||||||
.or(z.literal(""))
|
.or(z.literal(""))
|
||||||
.transform((val) => (val === "" ? undefined : val.toLowerCase().trim()))
|
.transform((val) => (val === "" ? null : val.toLowerCase().trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailUser: z
|
systemEmailUser: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailPassword: z
|
systemEmailPassword: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
systemEmailTLSServername: z
|
systemEmailTLSServername: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? null : val.trim()))
|
||||||
.optional(),
|
.optional(),
|
||||||
globalThresholds: z.object({
|
globalThresholds: z.object({
|
||||||
cpu: z.number().int().min(1).max(100),
|
cpu: z.number().int().min(1).max(100),
|
||||||
@@ -53,4 +52,5 @@ export const settingsSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export type SettingsFormInput = z.input<typeof settingsSchema>;
|
||||||
export type SettingsFormData = z.infer<typeof settingsSchema>;
|
export type SettingsFormData = z.infer<typeof settingsSchema>;
|
||||||
Reference in new issue
Block a user