import Stack from "@mui/material/Stack"; import { Button, DateTimePicker } from "@/components/inputs"; import { ConfigBox, BasePage, InfoBox } from "@/components/design-elements"; import { TextInput, Select, AutoComplete } from "@/components/inputs"; import MenuItem from "@mui/material/MenuItem"; import Typography from "@mui/material/Typography"; import dayjs from "dayjs"; import type { Dayjs } from "dayjs"; import { useTheme } from "@mui/material/styles"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTranslation } from "react-i18next"; import { z } from "zod"; import { MaintenanceRepeats } from "@/types/maintenance"; import { maintenanceSchema } from "@/validation"; import { useForm, Controller, type SubmitHandler } from "react-hook-form"; import { useEffect } from "react"; import { useInitForm } from "@/hooks/forms/UseInitMaintenanceForm"; import type { IMonitor } from "@/types/monitor"; export type FormValues = z.infer; export const MaintenanceForm = ({ monitorOptions, initialData, onSubmit, loading, }: { monitorOptions: IMonitor[]; initialData?: Partial; onSubmit: SubmitHandler; loading: boolean; }) => { const { t } = useTranslation(); const theme = useTheme(); const { defaults } = useInitForm({ initialData: initialData }); const { handleSubmit, control, reset, formState: { errors }, } = useForm({ resolver: zodResolver(maintenanceSchema), defaultValues: defaults, mode: "onChange", }); useEffect(() => { reset(defaults); }, [initialData, reset, defaults]); const onError = (errors: any) => { console.error(errors); }; return ( ( )} /> ( )} /> } /> { const value = field.value ? dayjs(field.value) : null; return ( <> field.onChange(val?.toDate()) } /> {errors?.startTime?.message && ( {errors.startTime.message} )} ); }} /> { const value = field.value ? dayjs(field.value) : null; return ( <> field.onChange(val?.toDate()) } /> {errors?.endTime?.message && ( {errors.endTime.message} )} ); }} /> } /> { const selectedMonitors = monitorOptions.filter((o: any) => (field.value || []).includes(o._id) ); const count = selectedMonitors.length; return ( option.name} value={selectedMonitors} onChange={(_, newValue) => { field.onChange(newValue.map((o: any) => o._id)); }} renderInput={(params) => ( 0 ? `${count} ${t("maintenanceWindow.form.monitors.optionMonitorsSelected")}` : t( "maintenanceWindow.form.monitors.optionMonitorsPlaceholder" ) } /> )} /> ); }} /> } /> ); };