prevent to select checkout before check in time

This commit is contained in:
pramod mahajan 2025-08-02 17:12:22 +05:30
parent 543b1bf94b
commit 03fb762f24

View File

@ -10,13 +10,37 @@ import showToast from "../../services/toastService";
import { checkIfCurrentDate } from "../../utils/dateUtils"; import { checkIfCurrentDate } from "../../utils/dateUtils";
import { useMarkAttendance } from "../../hooks/useAttendance"; import { useMarkAttendance } from "../../hooks/useAttendance";
const schema = z.object({ const createSchema = (modeldata) => {
return z
.object({
markTime: z.string().nonempty({ message: "Time is required" }), markTime: z.string().nonempty({ message: "Time is required" }),
description: z description: z
.string() .string()
.max(200, "description should less than 200 chracters") .max(200, "Description should be less than 200 characters")
.optional(), .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 }) => { const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm }) => {
const projectId = useSelector((store) => store.localVariables.projectId); const projectId = useSelector((store) => store.localVariables.projectId);
@ -41,7 +65,7 @@ const CheckCheckOutmodel = ({ modeldata, closeModal, handleSubmitForm }) => {
reset, reset,
setValue, setValue,
} = useForm({ } = useForm({
resolver: zodResolver(schema), resolver: zodResolver(createSchema(modeldata)),
mode: "onChange", mode: "onChange",
}); });