import React, { useState, useEffect } from "react"; import moment from "moment"; import Avatar from "../common/Avatar"; import { convertShortTime } from "../../utils/dateUtils"; import RenderAttendanceStatus from "./RenderAttendanceStatus"; import usePagination from "../../hooks/usePagination"; import { useNavigate } from "react-router-dom"; import { ITEMS_PER_PAGE } from "../../utils/constants"; const Attendance = ({ attendance, getRole, handleModalData, setshowOnlyCheckout, showOnlyCheckout, }) => { const [loading, setLoading] = useState(false); const navigate = useNavigate(); const [todayDate, setTodayDate] = useState(new Date()); // Ensure attendance is an array const attendanceList = Array.isArray(attendance) ? attendance : []; // Function to sort by first and last name const sortByName = (a, b) => { const nameA = (a.firstName + a.lastName).toLowerCase(); const nameB = (b.firstName + b.lastName).toLowerCase(); return nameA?.localeCompare(nameB); }; // Filter employees based on activity const group1 = attendanceList .filter((d) => d.activity === 1 || d.activity === 4) .sort(sortByName); const group2 = attendanceList .filter((d) => d.activity === 0) .sort(sortByName); const filteredData = [...group1, ...group2]; const { currentPage, totalPages, currentItems, paginate } = usePagination( filteredData, ITEMS_PER_PAGE ); return ( <>
Date : {todayDate.toLocaleDateString("en-GB")}
setshowOnlyCheckout(e.target.checked)} />
{attendance && attendance.length > 0 && ( <> {currentItems && currentItems .sort((a, b) => { // If checkInTime exists, compare it, otherwise, treat null as earlier than a date const checkInA = a?.checkInTime ? new Date(a.checkInTime) : new Date(0); const checkInB = b?.checkInTime ? new Date(b.checkInTime) : new Date(0); return checkInB - checkInA; // Sort in descending order of checkInTime }) .map((item) => ( ))} {!attendance && ( No employees assigned to the project )}
Name Role Check-In Check-Out Actions
{item.jobRoleName} {item.checkInTime ? convertShortTime(item.checkInTime) : "--"} {item.checkOutTime ? convertShortTime(item.checkOutTime) : "--"}
{!loading > 20 && ( )} )}
); }; export default Attendance;