import React, { useEffect, useState } from "react"; import Avatar from "../common/Avatar"; import { convertShortTime } from "../../utils/dateUtils"; import RenderAttendanceStatus from "./RenderAttendanceStatus"; import { useSelector, useDispatch } from "react-redux"; import { fetchAttendanceData } from "../../slices/apiSlice/attedanceLogsSlice"; const AttendanceLog = ({ attendance, handleModalData, projectId }) => { const [attendances, setAttendnaces] = useState([]); const [selectedDate, setSelectedDate] = useState(""); const dispatch = useDispatch(); const { data, loading, error } = useSelector((store) => store.attendanceLogs); // Set the default selected date to the current date const currentDate = new Date().toISOString().split("T")[0]; // "YYYY-MM-DD" const handleDateChange = (e) => { const date = e.target.value; setSelectedDate(date); if (date) { dispatch(fetchAttendanceData({ projectId, date: date })); } }; useEffect(() => { // If attendance has check-in time, filter it setAttendnaces(attendance?.filter((record) => record.checkInTime !== null)); setSelectedDate(currentDate); // Set default selected date to today }, [attendance]); const renderAttendanceData = selectedDate === currentDate ? attendances : data; return ( <>
{attendance && attendance.length > 0 ? ( {loading && } {error && } {selectedDate && renderAttendanceData.length === 0 && ( )} {renderAttendanceData?.map((attendance, index) => ( ))}
Name Role {" "} Check-In Check-Out Action
Loading...{error}
No Data Found
{attendance.jobRoleName} {convertShortTime(attendance.checkInTime)} {attendance.checkOutTime ? convertShortTime(attendance.checkOutTime) : "--"}
) : ( No employee logs )}
); }; export default AttendanceLog;