Add condition to move entries to 'approved' if checkOut and date are in the past.

This commit is contained in:
Pramod Mahajan 2025-05-14 15:34:40 +05:30 committed by Gitea Admin
parent 1e57e6fc03
commit ce4b517ce5

View File

@ -1,8 +1,7 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from "react";
import { timeElapsed } from '../utils/dateUtils'; import { timeElapsed } from "../utils/dateUtils";
import { useSelector } from 'react-redux'; import { useSelector } from "react-redux";
import {THRESH_HOLD} from '../utils/constants'; import { THRESH_HOLD } from "../utils/constants";
export const ACTIONS = { export const ACTIONS = {
CHECK_IN: 0, CHECK_IN: 0,
@ -10,124 +9,143 @@ export const ACTIONS = {
REGULARIZATION: 2, REGULARIZATION: 2,
REQUESTED: 3, REQUESTED: 3,
APPROVED: 4, APPROVED: 4,
REJECTED: 5 REJECTED: 5,
}; };
const now = new Date();
const useAttendanceStatus = (attendanceData) => { const useAttendanceStatus = (attendanceData) => {
const [status, setStatus] = useState({ const [status, setStatus] = useState({
status: "Unknown", status: "Unknown",
action: null, action: null,
disabled: true, disabled: true,
text: "Unknown", text: "Unknown",
color: 'btn-secondary', color: "btn-secondary",
}); });
useEffect(() => { useEffect(() => {
const { checkInTime, checkOutTime, activity } = attendanceData; const { checkInTime, checkOutTime, activity } = attendanceData;
if (activity === 0 && checkInTime === null && checkOutTime === null) {
if(activity === 0 && checkInTime === null && checkOutTime === null){
setStatus({ setStatus({
status: "Check-In", status: "Check-In",
action: ACTIONS.CHECK_IN, action: ACTIONS.CHECK_IN,
disabled: false, disabled: false,
text: "Check In", text: "Check In",
color: 'btn-primary', color: "btn-primary",
}) });
}else if(activity === 0&& checkInTime === null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){ } else if (activity === 4 && new Date(checkOutTime) < now) {
setStatus({
status: "Approved",
action: ACTIONS.APPROVED,
disabled: true,
text: "Approved",
color: "btn-success",
});
} else if (
activity === 0 &&
checkInTime === null &&
checkOutTime === null &&
!timeElapsed(checkInTime, THRESH_HOLD)
) {
setStatus({ setStatus({
status: "Check-In", status: "Check-In",
action: ACTIONS.CHECK_IN, action: ACTIONS.CHECK_IN,
disabled: false, disabled: false,
text: "Check In", text: "Check In",
color: 'btn-primary', color: "btn-primary",
}) });
} else if (
} else if(activity === 0&& checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){ activity === 0 &&
checkInTime !== null &&
checkOutTime === null &&
timeElapsed(checkInTime, THRESH_HOLD)
) {
setStatus({ setStatus({
status: "Request Regularize", status: "Request Regularize",
action: ACTIONS.REGULARIZATION, action: ACTIONS.REGULARIZATION,
disabled: false, disabled: false,
text: "Regularizes", text: "Regularizes",
color: 'btn-warning', color: "btn-warning",
}); });
} else if (
} activity === 1 &&
checkInTime !== null &&
else if(activity === 1 && checkInTime !== null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){ checkOutTime === null &&
!timeElapsed(checkInTime, THRESH_HOLD)
) {
setStatus({ setStatus({
status: "Check-Out", status: "Check-Out",
action: ACTIONS.CHECK_OUT, action: ACTIONS.CHECK_OUT,
disabled: false, disabled: false,
text: "Check Out", text: "Check Out",
color: 'btn-primary', color: "btn-primary",
}); });
}else if(activity === 1 && checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){ } else if (
activity === 1 &&
checkInTime !== null &&
checkOutTime === null &&
timeElapsed(checkInTime, THRESH_HOLD)
) {
setStatus({ setStatus({
status: "Request Regularize", status: "Request Regularize",
action: ACTIONS.REGULARIZATION, action: ACTIONS.REGULARIZATION,
disabled: false, disabled: false,
text: "Regularize", text: "Regularize",
color: 'btn-warning', color: "btn-warning",
}); });
} else if ( activity === 4 && checkInTime !== null && checkOutTime !== null && !timeElapsed( checkInTime, THRESH_HOLD ) ) } else if (
{ activity === 4 &&
checkInTime !== null &&
if ( activity === 4 && checkInTime !== null && checkOutTime !== null && new Date(checkOutTime).toDateString() !== new Date().toDateString()) checkOutTime !== null &&
{ !timeElapsed(checkInTime, THRESH_HOLD)
setStatus( { ) {
if (
activity === 4 &&
checkInTime !== null &&
checkOutTime !== null &&
new Date(checkOutTime).toDateString() !== new Date().toDateString()
) {
setStatus({
status: "Approved", status: "Approved",
action: ACTIONS.APPROVED, action: ACTIONS.APPROVED,
disabled: true, disabled: true,
text: "Approved", text: "Approved",
color: 'btn-success', color: "btn-success",
} ); });
} else } else {
{ setStatus({
setStatus( {
status: "Check-In", status: "Check-In",
action: ACTIONS.CHECK_IN, action: ACTIONS.CHECK_IN,
disabled: false, disabled: false,
text: "Check In", text: "Check In",
color: 'btn-primary', color: "btn-primary",
} ) });
} }
} } else if (activity === 2 && checkInTime !== null) {
else if ( activity === 2 && checkInTime !== null )
{
setStatus({ setStatus({
status: "Requested", status: "Requested",
action: ACTIONS.REQUESTED, action: ACTIONS.REQUESTED,
disabled: true, disabled: true,
text: "Requested", text: "Requested",
color: 'btn-info', color: "btn-info",
}); });
}else if(activity === 5 && checkInTime !== null ){ } else if (activity === 5 && checkInTime !== null) {
setStatus({ setStatus({
status: "Rejected", status: "Rejected",
action: ACTIONS.REJECTED, action: ACTIONS.REJECTED,
disabled: true, disabled: true,
text: "Rejected", text: "Rejected",
color: 'btn-danger', color: "btn-danger",
}); });
} else {
}else {
setStatus({ setStatus({
status: "Approved", status: "Approved",
action: ACTIONS.APPROVED, action: ACTIONS.APPROVED,
disabled: true, disabled: true,
text: "Approved", text: "Approved",
color: 'btn-success', color: "btn-success",
}); });
} }
}, [attendanceData]); }, [attendanceData]);
return status; return status;