import React, { useEffect, useState } from "react"; import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; const currentDate = new Date().toLocaleDateString('en-CA'); const formatDate = (date) => { if (!date) { return currentDate; } const d = new Date(date); if (isNaN(d.getTime())) { return currentDate; } return d.toLocaleDateString('en-CA'); }; const ManageProjectInfo = ({ project, handleSubmitForm, onClose,isPending }) => { const [CurrentProject, setCurrentProject] = useState(); const [addressLength, setAddressLength] = useState(0); const maxAddressLength = 500; const ACTIVE_STATUS_ID = "b74da4c2-d07e-46f2-9919-e75e49b12731"; const DEFAULT_EMPTY_STATUS_ID = "00000000-0000-0000-0000-000000000000"; const projectSchema = z .object({ ...(project?.id ? { id: z.string().optional() } : {}), name: z.string().min(1, { message: "Project Name is required" }), shortName: z.string().optional(), contactPerson: z .string() .min(1, { message: "Contact Person Name is required" }) .regex(/^[A-Za-z\s]+$/, { message: "Contact Person must contain only letters", }), projectAddress: z .string() .min(1, { message: "Address is required" }) .max(500, "Address must not exceed 150 characters"), startDate: z .string() .min(1, { message: "Start Date is required" }) .default(currentDate), endDate: z .string() .min(1, { message: "End Date is required" }) .default(currentDate), projectStatusId: z .string() .min(1, { message: "Status is required" }) }) .refine( (data) => { const start = new Date(data.startDate); const end = new Date(data.endDate); return end >= start; }, { path: ["endDate"], // attaches the error to the endDate field message: "End Date must be greater than Start Date", } ); const { register, control, handleSubmit, formState: { errors }, reset, getValues, } = useForm({ resolver: zodResolver(projectSchema), defaultValues: { id: project?.id || "", name: project?.name || "", shortName: project?.shortName || "", contactPerson: project?.contactPerson || "", projectAddress: project?.projectAddress || "", startDate: formatDate(project?.startDate) || currentDate, endDate: formatDate(project?.endDate) || currentDate, // projectStatusId: String(project?.projectStatusId || "00000000-0000-0000-0000-000000000000"), projectStatusId: project?.projectStatusId && project.projectStatusId !== DEFAULT_EMPTY_STATUS_ID ? String(project.projectStatusId) : ACTIVE_STATUS_ID, }, mode: "onChange", }); useEffect(() => { setCurrentProject(project); reset( project ? { id: project?.id || "", name: project?.name || "", shortName: project?.shortName || "", contactPerson: project?.contactPerson || "", projectAddress: project?.projectAddress || "", startDate: formatDate(project?.startDate) || "", endDate: formatDate(project?.endDate) || "", projectStatusId: String(project?.projectStatus?.id) || "00000000-0000-0000-0000-000000000000", } : {} ); setAddressLength(project?.projectAddress?.length || 0); }, [project, reset]); /** * Handles the form submission. * @param {object} updatedProject - The project data from the form. */ const onSubmitForm = ( updatedProject ) => { handleSubmitForm(updatedProject); }; const handleCancel = () => { reset({ id: project?.id || "", name: project?.name || "", shortName: project?.shortName || "", contactPerson: project?.contactPerson || "", projectAddress: project?.projectAddress || "", startDate: formatDate(project?.startDate) || currentDate, endDate: formatDate(project?.endDate) || currentDate, projectStatusId: String(project?.projectStatus?.id || "00000000-0000-0000-0000-000000000000"), }); onClose(); }; return (