@@ -578,6 +593,7 @@ const ManageEmployee = () => {
className="form-control form-control-sm"
id="PanNumber"
placeholder="PAN Number"
+ maxLength={10}
/>
{errors.PanNumber && (
{
if (!date) {
- return currentDate;
+ return currentDate;
}
const d = new Date(date);
if (isNaN(d.getTime())) {
return currentDate;
}
- return d.toISOString().split('T')[0];
+ return d.toISOString().split("T")[0];
};
-const ManageProjectInfo = ( {project,handleSubmitForm, onClose} ) =>
-{
-
- const [CurrentProject,setCurrentProject] = useState()
- const [ isloading, setLoading ] = useState( false )
+const ManageProjectInfo = ({ project, handleSubmitForm, onClose }) => {
+ const [CurrentProject, setCurrentProject] = useState();
+ const [isloading, setLoading] = useState(false);
const [addressLength, setAddressLength] = useState(0);
const maxAddressLength = 500;
- const projectSchema = z.object( {
- ...(project?.id ? { id: z.number().optional() } : {}),
- name: z.string().min( 1, {message: "Project Name is required"} ),
- contactPerson: z.string().min( 1, {message: "Contact Person Name is required"} ),
- 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" })
- .transform((val) => {
- const num = Number(val);
- if (isNaN(num)) {
- throw new Error("Status must be a valid number");
+ const projectSchema = z
+ .object({
+ ...(project?.id ? { id: z.number().optional() } : {}),
+ name: z.string().min(1, { message: "Project Name is required" }),
+ contactPerson: z
+ .string()
+ .min(1, { message: "Contact Person Name is required" }),
+ 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" })
+ .transform((val) => {
+ const num = Number(val);
+ if (isNaN(num)) {
+ throw new Error("Status must be a valid number");
+ }
+ return num;
+ }),
+ })
+ .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",
}
- return num;
- }),
-
- } ) .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 || "",
+ contactPerson: project?.contactPerson || "",
+ projectAddress: project?.projectAddress || "",
+ startDate: formatDate(project?.startDate) || currentDate,
+ endDate: formatDate(project?.endDate) || currentDate,
+ projectStatusId: String(project?.projectStatusId || "0"),
+ },
+ mode: "onChange",
});
-
- const {register, control, handleSubmit, formState: {errors}, reset, getValues} = useForm( {
- resolver: zodResolver( projectSchema ),
- defaultValues: {
- id:project?.id || "",
- name:project?.name || "",
- contactPerson:project?.contactPerson || "",
- projectAddress:project?.projectAddress || "",
- startDate: formatDate(project?.startDate )|| currentDate,
- endDate: formatDate(project?.endDate ) || currentDate,
- projectStatusId: String(project?.projectStatusId || "0"),
-
- }
- })
-
- useEffect( () =>
- {
- setCurrentProject(project)
+ useEffect(() => {
+ setCurrentProject(project);
reset(
- project ? {
- id:project?.id || "",
- name:project?.name || "",
- contactPerson: project?.contactPerson || "",
- projectAddress: project?.projectAddress || "",
- startDate:formatDate(project?.startDate )|| "",
- endDate: formatDate(project?.endDate ) || "",
- projectStatusId: String(project.projectStatusId) || "0" ,
-
- } :{}
-
- )
- setAddressLength(project?.projectAddress?.length || 0);
- },[project,reset,])
+ project
+ ? {
+ id: project?.id || "",
+ name: project?.name || "",
+ contactPerson: project?.contactPerson || "",
+ projectAddress: project?.projectAddress || "",
+ startDate: formatDate(project?.startDate) || "",
+ endDate: formatDate(project?.endDate) || "",
+ projectStatusId: String(project.projectStatusId) || "0",
+ }
+ : {}
+ );
+ setAddressLength(project?.projectAddress?.length || 0);
+ }, [project, reset]);
-
+ const onSubmitForm = (updatedProject) => {
+ setLoading(true);
+ handleSubmitForm(updatedProject);
+ };
- const onSubmitForm = (updatedProject) => {
- setLoading( true )
- handleSubmitForm( updatedProject )
- };
-
- useEffect( () =>
- {
- return ()=>setLoading(false)
- },[])
+ useEffect(() => {
+ return () => setLoading(false);
+ }, []);
return (
-
+
@@ -122,12 +137,18 @@ const ManageProjectInfo = ( {project,handleSubmitForm, onClose} ) =>
type="text"
id="name"
name="name"
-
className="form-control"
placeholder="Project Name"
{...register("name")}
/>
- {errors.name &&
{errors.name.message}
}
+ {errors.name && (
+
+ {errors.name.message}
+
+ )}
@@ -152,13 +179,18 @@ const ManageProjectInfo = ( {project,handleSubmitForm, onClose} ) =>
- {errors.startDate &&
{errors.startDate.message}
}
-
+ {errors.startDate && (
+
+ {errors.startDate.message}
+
+ )}
@@ -221,13 +264,20 @@ const ManageProjectInfo = ( {project,handleSubmitForm, onClose} ) =>
{maxAddressLength - addressLength} characters left
- {errors.projectAddress &&
{errors.projectAddress.message}
}
+ {errors.projectAddress && (
+
+ {errors.projectAddress.message}
+
+ )}
-