immediately update log attendances after marking attendance
This commit is contained in:
parent
9b742a2f7f
commit
bf7adc2cc6
@ -4,10 +4,11 @@ import {clearCacheKey} from '../apiDataManager';
|
||||
|
||||
// Fetch attendance data
|
||||
export const fetchAttendanceData = createAsyncThunk(
|
||||
'attendanceLogs/fetchAttendanceData', // Updated action type prefix
|
||||
async ({ projectId, date }, thunkAPI) => {
|
||||
'attendanceLogs/fetchAttendanceData',
|
||||
async ( {projectId, fromDate, toDate}, thunkAPI ) =>
|
||||
{
|
||||
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);
|
||||
} catch (error) {
|
||||
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(
|
||||
'attendanceLogs/markAttendance', // Updated action type prefix
|
||||
async ( formData, thunkAPI ) =>
|
||||
|
||||
{
|
||||
try {
|
||||
let newRecordAttendance = {
|
||||
@ -27,7 +29,7 @@ export const markAttendance = createAsyncThunk(
|
||||
employeeID: formData.employeeId,
|
||||
projectID: formData.projectId,
|
||||
date: new Date().toISOString(),
|
||||
markTime: formData.time,
|
||||
markTime: formData.markTime,
|
||||
latitude: formData.latitude.toString(),
|
||||
longitude: formData.longitude.toString(),
|
||||
action: formData.action,
|
||||
@ -35,8 +37,7 @@ export const markAttendance = createAsyncThunk(
|
||||
};
|
||||
|
||||
const response = await AttendanceRepository.markAttendance( newRecordAttendance );
|
||||
clearCacheKey("AttendanceLogs")
|
||||
return response.data; // Return the newly created or updated record
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return thunkAPI.rejectWithValue(error.message);
|
||||
}
|
||||
@ -71,17 +72,18 @@ const attendanceLogsSlice = createSlice({
|
||||
state.error = action.payload;
|
||||
})
|
||||
|
||||
// Mark attendance
|
||||
// Mark attendance - log attenace data
|
||||
.addCase(markAttendance.fulfilled, (state, action) => {
|
||||
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) {
|
||||
state.data[index] = { ...state.data[index], ...updatedRecord }; // Update existing record
|
||||
state.data[index] = { ...state.data[index], ...updatedRecord };
|
||||
} else {
|
||||
state.data.push(updatedRecord); // Add new record if not found
|
||||
state.data.push(updatedRecord);
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user