import Stack from "@mui/material/Stack"; import { Button, FormControlLabel } from "@/components/inputs"; import { ConfigBox, BasePage } from "@/components/design-elements"; import { TextInput, AutoComplete, Checkbox } from "@/components/inputs"; import Typography from "@mui/material/Typography"; import { Trash2 } from "lucide-react"; import { useTheme } from "@mui/material/styles"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTranslation } from "react-i18next"; import { z } from "zod"; import { statusPageSchema } from "@/validation/zod"; import { useForm, Controller, useWatch, type SubmitHandler, } from "react-hook-form"; import { useEffect } from "react"; import { useInitForm } from "@/hooks/forms/UseInitStatusPageForm"; import type { IMonitor } from "@/types/monitor"; type FormValues = z.infer; export const StatusPageForm = ({ initialData, monitorOptions, onSubmit, loading, }: { mode?: string; monitorOptions: IMonitor[]; initialData?: Partial; onSubmit: SubmitHandler; loading: boolean; }) => { const { t } = useTranslation(); const { defaults } = useInitForm({ initialData: initialData }); const theme = useTheme(); const { handleSubmit, control, reset, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(statusPageSchema), defaultValues: defaults, mode: "onChange", }); useEffect(() => { reset(defaults); }, [initialData, reset, defaults]); const onError = (errors: any) => { console.error(errors); }; const monitors = useWatch({ control, name: "monitors", }); return ( ( )} /> ( )} /> } /> ( )} /> } /> ( field.onChange(e.target.checked)} /> } /> )} /> } /> ( option.name} value={monitorOptions.filter((o: any) => (field.value || []).includes(o._id) )} onChange={(_, newValue) => { field.onChange(newValue.map((o: any) => o._id)); }} /> )} /> {monitors.map((monitorId) => { const option = monitorOptions.find((o) => o._id === monitorId); if (!option) return null; return ( {option.name} { const updated = monitors.filter( (id) => id !== monitorId ); setValue("monitors", updated); }} style={{ cursor: "pointer" }} /> ); })} } /> ); };