From 9886fac03ebfaced0541dc2a2427c4827ad455b8 Mon Sep 17 00:00:00 2001 From: "pramod.mahajan" Date: Mon, 6 Oct 2025 17:20:36 +0530 Subject: [PATCH] employee attendance display sorted by date, --- src/components/Employee/EmpAttendance.jsx | 110 ++++------------------ src/hooks/useAttendance.js | 26 +---- src/pages/employee/EmployeeProfile.jsx | 2 +- 3 files changed, 22 insertions(+), 116 deletions(-) diff --git a/src/components/Employee/EmpAttendance.jsx b/src/components/Employee/EmpAttendance.jsx index 548471d8..b630ac7f 100644 --- a/src/components/Employee/EmpAttendance.jsx +++ b/src/components/Employee/EmpAttendance.jsx @@ -14,28 +14,24 @@ import { FormProvider, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { localToUtc } from "../../utils/appUtils"; +import { useParams } from "react-router-dom"; -const EmpAttendance = ({ employee }) => { +const EmpAttendance = () => { + const { employeeId } = useParams(); const [attendances, setAttendnaces] = useState([]); const [selectedDate, setSelectedDate] = useState(""); const [isModalOpen, setIsModalOpen] = useState(false); const [attendanceId, setAttendanecId] = useState(); const methods = useForm({ - resolver: zodResolver( - z.object({ - startDate: z.string(), - endDate: z.string(), - }) - ), defaultValues: { - startDate: "", - endDate: "", + startDate: moment().subtract(6, "days").format("DD-MM-YYYY"), + endDate: moment().format("DD-MM-YYYY"), }, }); - const { control, register, handleSubmit, reset, watch } = methods; - const startDate = watch("startDate"); - const endDate = watch("endDate"); + const { watch } = methods; + + const [startDate, endDate] = watch(["startDate", "endDate"]); const { data = [], isLoading: loading, @@ -44,76 +40,21 @@ const EmpAttendance = ({ employee }) => { error, refetch, } = useAttendanceByEmployee( - employee, - localToUtc(startDate), - localToUtc(endDate) + employeeId, + startDate ? localToUtc(startDate) : null, + endDate ? localToUtc(endDate) : null ); + const dispatch = useDispatch(); - - const today = new Date(); - today.setHours(0, 0, 0, 0); - - const isSameDay = (dateStr) => { - if (!dateStr) return false; - const d = new Date(dateStr); - d.setHours(0, 0, 0, 0); - return d.getTime() === today.getTime(); - }; - - const isBeforeToday = (dateStr) => { - if (!dateStr) return false; - const d = new Date(dateStr); - d.setHours(0, 0, 0, 0); - return d.getTime() < today.getTime(); - }; - - const sortByName = (a, b) => { - const nameA = a.firstName.toLowerCase() + a.lastName.toLowerCase(); - const nameB = b.firstName.toLowerCase() + b.lastName.toLowerCase(); - return nameA?.localeCompare(nameB); - }; - - const group1 = data - .filter((d) => d.activity === 1 && isSameDay(d.checkInTime)) - .sort(sortByName); - const group2 = data - .filter((d) => d.activity === 4 && isSameDay(d.checkOutTime)) - .sort(sortByName); - const group3 = data - .filter((d) => d.activity === 1 && isBeforeToday(d.checkInTime)) - .sort(sortByName); - const group4 = data - .filter((d) => d.activity === 4 && isBeforeToday(d.checkOutTime)) - .sort(sortByName); - const group5 = data.filter((d) => d.activity === 5).sort(sortByName); - - - const uniqueMap = new Map(); - - [...group1, ...group2, ...group3, ...group4, ...group5].forEach((rec) => { - const date = moment(rec.checkInTime || rec.checkOutTime).format( - "YYYY-MM-DD" - ); - const key = `${rec.employeeId}-${date}`; - const existing = uniqueMap.get(key); - if ( - !existing || - new Date(rec.checkInTime || rec.checkOutTime) > - new Date(existing.checkInTime || existing.checkOutTime) - ) { - uniqueMap.set(key, rec); - } - }); - - const sortedFinalList = [...uniqueMap.values()].sort( + const sorted = [...data].sort( (a, b) => - new Date(b.checkInTime || b.checkOutTime) - - new Date(a.checkInTime || a.checkOutTime) + new Date(b?.checkInTime).getTime() - new Date(a?.checkInTime).getTime() ); - const currentDate = new Date().toLocaleDateString("en-CA"); + console.log(sorted); + const { currentPage, totalPages, currentItems, paginate } = usePagination( - sortedFinalList, + sorted, ITEMS_PER_PAGE ); @@ -123,7 +64,6 @@ const EmpAttendance = ({ employee }) => { }; const closeModal = () => setIsModalOpen(false); - const onSubmit = (formData) => {}; return ( <> {isModalOpen && ( @@ -136,20 +76,10 @@ const EmpAttendance = ({ employee }) => { className="dataTables_length text-start py-2 d-flex justify-content-between " id="DataTables_Table_0_length" > -
+
<> -
- - +
@@ -234,7 +164,7 @@ const EmpAttendance = ({ employee }) => { )}
- {!loading && sortedFinalList.length > 20 && ( + {!loading && data.length > 20 && (