immediately update log attendances after marking attendance

This commit is contained in:
Pramod Mahajan 2025-04-29 00:41:16 +05:30
parent 9b742a2f7f
commit bf7adc2cc6

View File

@ -4,10 +4,11 @@ import {clearCacheKey} from '../apiDataManager';
// Fetch attendance data // Fetch attendance data
export const fetchAttendanceData = createAsyncThunk( export const fetchAttendanceData = createAsyncThunk(
'attendanceLogs/fetchAttendanceData', // Updated action type prefix 'attendanceLogs/fetchAttendanceData',
async ({ projectId, date }, thunkAPI) => { async ( {projectId, fromDate, toDate}, thunkAPI ) =>
{
try { try {
const response = await AttendanceRepository.getAttendanceFilteredByDate(projectId, date); const response = await AttendanceRepository.getAttendanceFilteredByDate(projectId, fromDate, toDate);
return response?.data?.filter((log) => log.checkInTime !== null && log.activity !== 0); return response?.data?.filter((log) => log.checkInTime !== null && log.activity !== 0);
} catch (error) { } catch (error) {
return thunkAPI.rejectWithValue(error.message); return thunkAPI.rejectWithValue(error.message);
@ -15,10 +16,11 @@ export const fetchAttendanceData = createAsyncThunk(
} }
); );
// This method for marking attendance if a date filter is applied
export const markAttendance = createAsyncThunk( export const markAttendance = createAsyncThunk(
'attendanceLogs/markAttendance', // Updated action type prefix 'attendanceLogs/markAttendance', // Updated action type prefix
async ( formData, thunkAPI ) => async ( formData, thunkAPI ) =>
{ {
try { try {
let newRecordAttendance = { let newRecordAttendance = {
@ -27,7 +29,7 @@ export const markAttendance = createAsyncThunk(
employeeID: formData.employeeId, employeeID: formData.employeeId,
projectID: formData.projectId, projectID: formData.projectId,
date: new Date().toISOString(), date: new Date().toISOString(),
markTime: formData.time, markTime: formData.markTime,
latitude: formData.latitude.toString(), latitude: formData.latitude.toString(),
longitude: formData.longitude.toString(), longitude: formData.longitude.toString(),
action: formData.action, action: formData.action,
@ -35,8 +37,7 @@ export const markAttendance = createAsyncThunk(
}; };
const response = await AttendanceRepository.markAttendance( newRecordAttendance ); const response = await AttendanceRepository.markAttendance( newRecordAttendance );
clearCacheKey("AttendanceLogs") return response.data;
return response.data; // Return the newly created or updated record
} catch (error) { } catch (error) {
return thunkAPI.rejectWithValue(error.message); return thunkAPI.rejectWithValue(error.message);
} }
@ -71,17 +72,18 @@ const attendanceLogsSlice = createSlice({
state.error = action.payload; state.error = action.payload;
}) })
// Mark attendance // Mark attendance - log attenace data
.addCase(markAttendance.fulfilled, (state, action) => { .addCase(markAttendance.fulfilled, (state, action) => {
const updatedRecord = action.payload; const updatedRecord = action.payload;
const index = state.data.findIndex(item => item.employeeId === updatedRecord.employeeId); const index = state.data.findIndex(item => item.id === updatedRecord.id);
if (index !== -1) { if (index !== -1) {
state.data[index] = { ...state.data[index], ...updatedRecord }; // Update existing record state.data[index] = { ...state.data[index], ...updatedRecord };
} else { } else {
state.data.push(updatedRecord); // Add new record if not found state.data.push(updatedRecord);
} }
}); });
}, },
}); });