190 lines
5.2 KiB
JavaScript
190 lines
5.2 KiB
JavaScript
import { useEffect, useState } from "react";
|
|
import { cacheData, getCachedData, useSelectedproject } from "../slices/apiDataManager";
|
|
import AttendanceRepository from "../repositories/AttendanceRepository";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import showToast from "../services/toastService";
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
import { store } from "../store/store";
|
|
import { setDefaultDateRange } from "../slices/localVariablesSlice";
|
|
|
|
|
|
|
|
// ----------------------------Query-----------------------------
|
|
|
|
|
|
export const useAttendance = (projectId) => {
|
|
const dispatch = useDispatch()
|
|
const {
|
|
data: attendance = [],
|
|
isLoading: loading,
|
|
error,
|
|
refetch: recall,
|
|
isFetching
|
|
} = useQuery({
|
|
queryKey: ["attendance", projectId],
|
|
queryFn: async () => {
|
|
const response = await AttendanceRepository.getAttendance(projectId);
|
|
return response.data;
|
|
},
|
|
enabled: !!projectId,
|
|
onError: (error) => {
|
|
showToast(error.message || "Error while fetching Attendance", "error");
|
|
},
|
|
});
|
|
|
|
return {
|
|
attendance,
|
|
loading,
|
|
error,
|
|
recall,
|
|
isFetching
|
|
};
|
|
};
|
|
|
|
export const useAttendancesLogs = (projectId, fromDate, toDate) => {
|
|
const dispatch = useDispatch();
|
|
const enabled = !!projectId && !!fromDate && !!toDate;
|
|
|
|
const query = useQuery({
|
|
queryKey: ['attendanceLogs', projectId, fromDate, toDate],
|
|
queryFn: async () => {
|
|
const res = await AttendanceRepository.getAttendanceFilteredByDate(
|
|
projectId,
|
|
fromDate,
|
|
toDate
|
|
);
|
|
return res.data;
|
|
},
|
|
enabled,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (query.data && fromDate && toDate) {
|
|
dispatch(
|
|
setDefaultDateRange({
|
|
startDate: fromDate,
|
|
endDate: toDate,
|
|
})
|
|
);
|
|
}
|
|
}, [dispatch, query.data, fromDate, toDate]);
|
|
return query;
|
|
};
|
|
|
|
|
|
|
|
export const useEmployeeAttendacesLog = (id) => {
|
|
const {
|
|
data: logs = [],
|
|
isLoading: loading,
|
|
error,
|
|
refetch: recall,
|
|
} = useQuery({
|
|
queryKey: ["employeeAttendanceLogs", id],
|
|
queryFn: async () => {
|
|
const response = await AttendanceRepository.getAttendanceLogs(id);
|
|
return response.data;
|
|
},
|
|
enabled: !!id,
|
|
onError: (error) => {
|
|
showToast(error.message || "Error while fetching Attendance Logs", "error");
|
|
},
|
|
});
|
|
|
|
return {
|
|
logs,
|
|
loading,
|
|
error,
|
|
recall,
|
|
};
|
|
};
|
|
|
|
export const useAttendanceByEmployee = (employeeId, fromDate, toDate) => {
|
|
const enabled = !!employeeId && !!fromDate && !!toDate;
|
|
|
|
return useQuery({
|
|
queryKey: ["employeeAttendance", employeeId, fromDate, toDate],
|
|
queryFn: async () => {
|
|
const res = await AttendanceRepository.getAttendanceByEmployee(employeeId, fromDate, toDate);
|
|
return res.data;
|
|
},
|
|
enabled,
|
|
});
|
|
};
|
|
|
|
export const useRegularizationRequests = (projectId) => {
|
|
const {
|
|
data: regularizes = [],
|
|
isLoading: loading,
|
|
error,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: ["regularizedList", projectId],
|
|
queryFn: async () => {
|
|
const response = await AttendanceRepository.getRegularizeList(projectId);
|
|
return response.data;
|
|
},
|
|
enabled: !!projectId,
|
|
onError: (error) => {
|
|
showToast(error.message || "Error while fetching Regularization Requests", "error");
|
|
},
|
|
});
|
|
|
|
return {
|
|
regularizes,
|
|
loading,
|
|
error,
|
|
refetch,
|
|
};
|
|
};
|
|
|
|
|
|
// -------------------Mutation--------------------------------------
|
|
|
|
export const useMarkAttendance = () => {
|
|
const queryClient = useQueryClient();
|
|
// const selectedProject = useSelector((store)=>store.localVariables.projectId)
|
|
const selectedProject = useSelectedproject();
|
|
const selectedDateRange = useSelector((store)=>store.localVariables.defaultDateRange)
|
|
|
|
return useMutation({
|
|
mutationFn: async ({payload,forWhichTab}) => {
|
|
const res = await AttendanceRepository.markAttendance(payload);
|
|
return res.data;
|
|
},
|
|
|
|
onSuccess: (data,variables) => {
|
|
if(variables.forWhichTab == 1){
|
|
queryClient.setQueryData(["attendance",selectedProject], (oldData) => {
|
|
if (!oldData) return oldData;
|
|
return oldData.map((emp) =>
|
|
emp.employeeId === data.employeeId ? { ...emp, ...data } : emp
|
|
);
|
|
});
|
|
}else if(variables.forWhichTab == 2){
|
|
// queryClient.invalidateQueries({
|
|
// queryKey: ["attendanceLogs"],
|
|
// });
|
|
queryClient.setQueryData(["attendanceLogs",selectedProject,selectedDateRange.startDate,selectedDateRange.endDate], (oldData) => {
|
|
if (!oldData) return oldData;
|
|
return oldData.map((record) =>
|
|
record.id === data.id ? { ...record, ...data } : record
|
|
);
|
|
});
|
|
queryClient.invalidateQueries({queryKey:["regularizedList"]})
|
|
}else(
|
|
queryClient.setQueryData(["regularizedList",selectedProject], (oldData) => {
|
|
if (!oldData) return oldData;
|
|
return oldData.filter((record) => record.id !== data.id)
|
|
}),
|
|
queryClient.invalidateQueries({queryKey:["attendanceLogs"]})
|
|
)
|
|
|
|
if(variables.forWhichTab !== 3) showToast("Attendance marked successfully", "success");
|
|
},
|
|
onError: (error) => {
|
|
showToast(error.message || "Failed to mark attendance", "error");
|
|
},
|
|
});
|
|
};
|