From 633e60d141c9896c2d440fd1eab1df0be2109d5c Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Tue, 22 Jul 2025 11:35:15 +0530 Subject: [PATCH] At the time of Regularize status code 400 were shown now solved we add error message in popup "Checkout time must be later than check-in time". --- .../Activities/CheckCheckOutForm.jsx | 78 ++++++++++++++----- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/src/components/Activities/CheckCheckOutForm.jsx b/src/components/Activities/CheckCheckOutForm.jsx index fe12730b..016f088f 100644 --- a/src/components/Activities/CheckCheckOutForm.jsx +++ b/src/components/Activities/CheckCheckOutForm.jsx @@ -10,10 +10,43 @@ import showToast from "../../services/toastService"; import { checkIfCurrentDate } from "../../utils/dateUtils"; -const schema = z.object({ - markTime: z.string().nonempty({ message: "Time is required" }), - description: z.string().max(200, "description should less than 200 chracters").optional() -}); +// const schema = z.object({ +// markTime: z.string().nonempty({ message: "Time is required" }), +// description: z.string().max(200, "description should less than 200 chracters").optional() +// }); + +const createSchema = (modeldata) => { + return z + .object({ + markTime: z.string().nonempty({ message: "Time is required" }), + description: z + .string() + .max(200, "Description should be less than 200 characters") + .optional(), + }) + .refine((data) => { + if (modeldata?.checkInTime && !modeldata?.checkOutTime) { + const checkIn = new Date(modeldata.checkInTime); + const [time, modifier] = data.markTime.split(" "); + const [hourStr, minuteStr] = time.split(":"); + let hour = parseInt(hourStr, 10); + const minute = parseInt(minuteStr, 10); + + if (modifier === "PM" && hour !== 12) hour += 12; + if (modifier === "AM" && hour === 12) hour = 0; + + const checkOut = new Date(checkIn); + checkOut.setHours(hour, minute, 0, 0); + + return checkOut > checkIn; + } + return true; + }, { + message: "Checkout time must be later than check-in time", + path: ["markTime"], + }); +}; + const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm, }) => { @@ -31,16 +64,28 @@ const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm, }) => { return `${day}-${month}-${year}`; }; + // const { + // register, + // handleSubmit, + // formState: { errors }, + // reset, + // setValue, + // } = useForm({ + // resolver: zodResolver(schema), + // mode: "onChange" + // }); + const { - register, - handleSubmit, - formState: { errors }, - reset, - setValue, - } = useForm({ - resolver: zodResolver(schema), - mode: "onChange" - }); + register, + handleSubmit, + formState: { errors }, + reset, + setValue, +} = useForm({ + resolver: zodResolver(createSchema(modeldata)), + mode: "onChange", +}); + const onSubmit = (data) => { let record = { ...data, date: new Date().toLocaleDateString(), latitude: coords.latitude, longitude: coords.longitude, employeeId: modeldata.employeeId, action: modeldata.action, id: modeldata?.id || null } @@ -48,8 +93,6 @@ const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm, }) => { handleSubmitForm(record) } else { - // if ( modeldata?.currentDate && checkIfCurrentDate( modeldata?.currentDate ) ) - // { dispatch(markAttendance(record)) .unwrap() .then((data) => { @@ -61,11 +104,6 @@ const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm, }) => { showToast(error, "error"); }); - - // } else - // { - // let formData = {...data, date: new Date().toLocaleDateString(),latitude:coords.latitude,longitude:coords.longitude,employeeId:modeldata.employeeId,projectId:projectId,action:modeldata.action,id:modeldata?.id || null} - // } } closeModal()