import React, { useCallback, useEffect, useState } from "react"; import Avatar from "../common/Avatar"; import { convertShortTime } from "../../utils/dateUtils"; import RegularizationActions from "./RegularizationActions"; import { useSelector } from "react-redux"; import { useRegularizationRequests } from "../../hooks/useAttendance"; import moment from "moment"; import usePagination from "../../hooks/usePagination"; import eventBus from "../../services/eventBus"; import { cacheData, clearCacheKey } from "../../slices/apiDataManager"; const Regularization = ({ handleRequest }) => { var selectedProject = useSelector((store) => store.localVariables.projectId); const [regularizesList, setregularizedList] = useState([]); const { regularizes, loading, error, refetch } = useRegularizationRequests(selectedProject); useEffect(() => { setregularizedList(regularizes); }, [regularizes]); 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 handler = useCallback( (msg) => { if (selectedProject == msg.projectId) { const updatedAttendance = regularizes?.filter( item => item.id !== msg.response.id ); cacheData("regularizedList", { data: updatedAttendance, projectId: selectedProject, }); // clearCacheKey("regularizedList") refetch(); } }, [selectedProject, regularizes] ); const filteredData = [...regularizesList]?.sort(sortByName); const { currentPage, totalPages, currentItems, paginate } = usePagination( filteredData, 20 ); useEffect(() => { eventBus.on("regularization", handler); return () => eventBus.off("regularization", handler); }, [handler]); const employeeHandler = useCallback( (msg) => { if (regularizes.some((item) => item.employeeId == msg.employeeId)) { refetch(); } }, [regularizes] ); useEffect(() => { eventBus.on("employee", employeeHandler); return () => eventBus.off("employee", employeeHandler); }, [employeeHandler]); return (
{/* {loading && ( )} */} {!loading && (regularizes?.length > 0 ? ( regularizes?.map((att, index) => ( )) ) : ( ))}
Name Date Check-In Check-Out Action
Loading...
{moment(att.checkOutTime).format("DD-MMM-YYYY")} {convertShortTime(att.checkInTime)} {att.checkOutTime ? convertShortTime(att.checkOutTime) : "--"} {/*
*/} {/*
*/}
No Record Found
{!loading && totalPages > 1 && ( )}
); }; export default Regularization;