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 === 4 && new Date(checkOutTime) < now) {
}else if(activity === 0&& checkInTime === null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){ setStatus({
setStatus({ status: "Approved",
status: "Check-In", action: ACTIONS.APPROVED,
action: ACTIONS.CHECK_IN, disabled: true,
disabled: false, text: "Approved",
text: "Check In", color: "btn-success",
color: 'btn-primary', });
}) } else if (
activity === 0 &&
} else if(activity === 0&& checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){ checkInTime === null &&
setStatus({ checkOutTime === null &&
status: "Request Regularize", !timeElapsed(checkInTime, THRESH_HOLD)
action: ACTIONS.REGULARIZATION, ) {
disabled: false, setStatus({
text: "Regularizes", status: "Check-In",
color: 'btn-warning', action: ACTIONS.CHECK_IN,
}); disabled: false,
text: "Check In",
} color: "btn-primary",
});
else if(activity === 1 && checkInTime !== null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){ } else if (
setStatus({ activity === 0 &&
status: "Check-Out", checkInTime !== null &&
action: ACTIONS.CHECK_OUT, checkOutTime === null &&
disabled: false, timeElapsed(checkInTime, THRESH_HOLD)
text: "Check Out", ) {
color: 'btn-primary', setStatus({
}); status: "Request Regularize",
}else if(activity === 1 && checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){ action: ACTIONS.REGULARIZATION,
setStatus({ disabled: false,
status: "Request Regularize", text: "Regularizes",
action: ACTIONS.REGULARIZATION, color: "btn-warning",
disabled: false, });
text: "Regularize", } else if (
color: 'btn-warning', activity === 1 &&
}); checkInTime !== null &&
} else if ( activity === 4 && checkInTime !== null && checkOutTime !== null && !timeElapsed( checkInTime, THRESH_HOLD ) ) checkOutTime === null &&
{ !timeElapsed(checkInTime, THRESH_HOLD)
) {
if ( activity === 4 && checkInTime !== null && checkOutTime !== null && new Date(checkOutTime).toDateString() !== new Date().toDateString()) setStatus({
{ status: "Check-Out",
setStatus( { action: ACTIONS.CHECK_OUT,
status: "Approved", disabled: false,
action: ACTIONS.APPROVED, text: "Check Out",
disabled: true, color: "btn-primary",
text: "Approved", });
color: 'btn-success', } else if (
} ); activity === 1 &&
} else checkInTime !== null &&
{ checkOutTime === null &&
setStatus( { timeElapsed(checkInTime, THRESH_HOLD)
status: "Check-In", ) {
action: ACTIONS.CHECK_IN, setStatus({
disabled: false, status: "Request Regularize",
text: "Check In", action: ACTIONS.REGULARIZATION,
color: 'btn-primary', disabled: false,
} ) text: "Regularize",
} color: "btn-warning",
} });
else if ( activity === 2 && checkInTime !== null ) } else if (
{ activity === 4 &&
setStatus({ checkInTime !== null &&
status: "Requested", checkOutTime !== null &&
action: ACTIONS.REQUESTED, !timeElapsed(checkInTime, THRESH_HOLD)
disabled: true, ) {
text: "Requested", if (
color: 'btn-info', activity === 4 &&
}); checkInTime !== null &&
}else if(activity === 5 && checkInTime !== null ){ checkOutTime !== null &&
new Date(checkOutTime).toDateString() !== new Date().toDateString()
) {
setStatus({
status: "Rejected",
action: ACTIONS.REJECTED,
disabled: true,
text: "Rejected",
color: 'btn-danger',
});
}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",
});
} else {
setStatus({
status: "Check-In",
action: ACTIONS.CHECK_IN,
disabled: false,
text: "Check In",
color: "btn-primary",
}); });
} }
} else if (activity === 2 && checkInTime !== null) {
setStatus({
status: "Requested",
action: ACTIONS.REQUESTED,
disabled: true,
text: "Requested",
color: "btn-info",
});
} else if (activity === 5 && checkInTime !== null) {
setStatus({
status: "Rejected",
action: ACTIONS.REJECTED,
disabled: true,
text: "Rejected",
color: "btn-danger",
});
} else {
setStatus({
status: "Approved",
action: ACTIONS.APPROVED,
disabled: true,
text: "Approved",
color: "btn-success",
});
}
}, [attendanceData]); }, [attendanceData]);
return status; return status;