From 5e1ccc9b058ccdce2ab968746a7bab6ad897429c Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Sun, 21 Sep 2025 18:17:47 +0530 Subject: [PATCH] Calling api for Attendance component for Organization. --- src/components/Activities/Attendance.jsx | 48 +------- src/components/Activities/AttendcesLogs.jsx | 5 +- src/components/Activities/Regularization.jsx | 4 +- src/hooks/useAttendance.js | 114 ++++++++++++++----- src/pages/Activities/AttendancePage.jsx | 57 +++++----- src/repositories/AttendanceRepository.jsx | 36 +++++- 6 files changed, 155 insertions(+), 109 deletions(-) diff --git a/src/components/Activities/Attendance.jsx b/src/components/Activities/Attendance.jsx index 34df90df..a4443dc9 100644 --- a/src/components/Activities/Attendance.jsx +++ b/src/components/Activities/Attendance.jsx @@ -12,22 +12,19 @@ import { useQueryClient } from "@tanstack/react-query"; import eventBus from "../../services/eventBus"; import { useSelectedProject } from "../../slices/apiDataManager"; -const Attendance = ({ getRole, handleModalData, searchTerm }) => { +const Attendance = ({ getRole, handleModalData, searchTerm, projectId, organizationId, includeInactive, date }) => { const queryClient = useQueryClient(); const [loading, setLoading] = useState(false); const navigate = useNavigate(); const [todayDate, setTodayDate] = useState(new Date()); const [ShowPending, setShowPending] = useState(false); - // const selectedProject = useSelector( - // (store) => store.localVariables.projectId - // ); const selectedProject = useSelectedProject(); const { attendance, loading: attLoading, recall: attrecall, isFetching - } = useAttendance(selectedProject); + } = useAttendance(selectedProject, organizationId, includeInactive, date); const filteredAttendance = ShowPending ? attendance?.filter( (att) => att?.checkInTime !== null && att?.checkOutTime === null @@ -67,43 +64,6 @@ const Attendance = ({ getRole, handleModalData, searchTerm }) => { }); }, [group1, group2, searchTerm]); - // const finalFilteredData = useMemo(() => { - // const combinedData = [...group1, ...group2]; - - // let tempData = combinedData; - - // // Search filter - // if (searchTerm) { - // const lowercasedSearchTerm = searchTerm.toLowerCase(); - // tempData = tempData.filter((item) => { - // const fullName = `${item.firstName} ${item.lastName}`.toLowerCase(); - // const role = item.jobRoleName?.toLowerCase() || ""; - // return ( - // fullName.includes(lowercasedSearchTerm) || - // role.includes(lowercasedSearchTerm) // also search by role - // ); - // }); - // } - - // // Organization filter - // if (filters?.selectedOrganization) { - // tempData = tempData.filter( - // (item) => item.organization?.name === filters.selectedOrganization - // ); - // } - - // // Services filter - // if (filters?.selectedServices?.length > 0) { - // tempData = tempData.filter((item) => - // filters.selectedServices.includes(item.service?.name) - // ); - // } - - // return tempData; - // }, [group1, group2, searchTerm, filters]); - - - const { currentPage, totalPages, currentItems, paginate } = usePagination( finalFilteredData, ITEMS_PER_PAGE @@ -178,7 +138,7 @@ const Attendance = ({ getRole, handleModalData, searchTerm }) => { Name Role - Organization + Organization Check-In @@ -253,7 +213,7 @@ const Attendance = ({ getRole, handleModalData, searchTerm }) => { {!attendance && ( diff --git a/src/components/Activities/AttendcesLogs.jsx b/src/components/Activities/AttendcesLogs.jsx index 881b4c6c..71c7328a 100644 --- a/src/components/Activities/AttendcesLogs.jsx +++ b/src/components/Activities/AttendcesLogs.jsx @@ -33,7 +33,7 @@ const usePagination = (data, itemsPerPage) => { }; }; -const AttendanceLog = ({ handleModalData, searchTerm }) => { +const AttendanceLog = ({ handleModalData, searchTerm ,organizationId}) => { // const selectedProject = useSelector( // (store) => store.localVariables.projectId // ); @@ -81,7 +81,8 @@ const AttendanceLog = ({ handleModalData, searchTerm }) => { } = useAttendancesLogs( selectedProject, dateRange.startDate, - dateRange.endDate + dateRange.endDate, + organizationId ); const filtering = (data) => { const filteredData = showPending diff --git a/src/components/Activities/Regularization.jsx b/src/components/Activities/Regularization.jsx index 0f251bcc..8f1d21d5 100644 --- a/src/components/Activities/Regularization.jsx +++ b/src/components/Activities/Regularization.jsx @@ -10,13 +10,13 @@ import eventBus from "../../services/eventBus"; import { cacheData, clearCacheKey, useSelectedProject } from "../../slices/apiDataManager"; import { useQueryClient } from "@tanstack/react-query"; -const Regularization = ({ handleRequest, searchTerm }) => { +const Regularization = ({ handleRequest, searchTerm,projectId, organizationId, IncludeInActive }) => { const queryClient = useQueryClient(); // var selectedProject = useSelector((store) => store.localVariables.projectId); const selectedProject = useSelectedProject(); const [regularizesList, setregularizedList] = useState([]); const { regularizes, loading, error, refetch } = - useRegularizationRequests(selectedProject); + useRegularizationRequests(selectedProject, organizationId, IncludeInActive); useEffect(() => { setregularizedList(regularizes); diff --git a/src/hooks/useAttendance.js b/src/hooks/useAttendance.js index a7cdb7ac..a334c0c6 100644 --- a/src/hooks/useAttendance.js +++ b/src/hooks/useAttendance.js @@ -12,46 +12,76 @@ import { setDefaultDateRange } from "../slices/localVariablesSlice"; // ----------------------------Query----------------------------- -export const useAttendance = (projectId) => { - const dispatch = useDispatch() +// 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 useAttendance = (projectId, organizationId, includeInactive = false, date = null) => { + const dispatch = useDispatch(); + const { data: attendance = [], isLoading: loading, error, refetch: recall, - isFetching + isFetching, } = useQuery({ - queryKey: ["attendance", projectId], + queryKey: ["attendance", projectId, organizationId, includeInactive, date], // include filters in cache key queryFn: async () => { - const response = await AttendanceRepository.getAttendance(projectId); + const response = await AttendanceRepository.getAttendance( + projectId, + organizationId, + includeInactive, + date + ); return response.data; }, - enabled: !!projectId, + enabled: !!projectId, // only run if projectId exists onError: (error) => { showToast(error.message || "Error while fetching Attendance", "error"); }, }); - return { - attendance, - loading, - error, - recall, - isFetching - }; + return { attendance, loading, error, recall, isFetching }; }; -export const useAttendancesLogs = (projectId, fromDate, toDate) => { +export const useAttendancesLogs = (projectId, fromDate, toDate,organizationId) => { const dispatch = useDispatch(); const enabled = !!projectId && !!fromDate && !!toDate; const query = useQuery({ - queryKey: ['attendanceLogs', projectId, fromDate, toDate], + queryKey: ['attendanceLogs', projectId, fromDate, toDate,organizationId], queryFn: async () => { const res = await AttendanceRepository.getAttendanceFilteredByDate( projectId, fromDate, - toDate + toDate, + organizationId ); return res.data; }, @@ -112,30 +142,58 @@ export const useAttendanceByEmployee = (employeeId, fromDate, toDate) => { }); }; -export const useRegularizationRequests = (projectId) => { +// 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, +// }; +// }; + +export const useRegularizationRequests = (projectId, organizationId, IncludeInActive = false) => { + const dispatch = useDispatch(); + const { data: regularizes = [], isLoading: loading, error, - refetch, + refetch: recall, + isFetching, } = useQuery({ - queryKey: ["regularizedList", projectId], + queryKey: ["regularizedList", projectId, organizationId, IncludeInActive], // include filters in cache key queryFn: async () => { - const response = await AttendanceRepository.getRegularizeList(projectId); + const response = await AttendanceRepository.getRegularizeList( + projectId, + organizationId, + IncludeInActive, + ); return response.data; }, - enabled: !!projectId, + enabled: !!projectId, // only run if projectId exists onError: (error) => { - showToast(error.message || "Error while fetching Regularization Requests", "error"); + showToast(error.message || "Error while fetching regularizes", "error"); }, }); - return { - regularizes, - loading, - error, - refetch, - }; + return { regularizes, loading, error, recall, isFetching }; }; diff --git a/src/pages/Activities/AttendancePage.jsx b/src/pages/Activities/AttendancePage.jsx index cef91843..ff25058d 100644 --- a/src/pages/Activities/AttendancePage.jsx +++ b/src/pages/Activities/AttendancePage.jsx @@ -17,7 +17,7 @@ import { hasUserPermission } from "../../utils/authUtils"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import { REGULARIZE_ATTENDANCE } from "../../utils/constants"; import eventBus from "../../services/eventBus"; -import { useProjectName } from "../../hooks/useProjects"; +import { useProjectAssignedOrganizations, useProjectName } from "../../hooks/useProjects"; import GlobalModel from "../../components/common/GlobalModel"; import CheckCheckOutmodel from "../../components/Activities/CheckCheckOutForm"; import AttendLogs from "../../components/Activities/AttendLogs"; @@ -44,6 +44,9 @@ const AttendancePage = () => { selectedServices: [], }); + const { data: organizations = [], isLoading: orgLoading } = + useProjectAssignedOrganizations(selectedProject); + const [formData, setFormData] = useState({ markTime: "", description: "", @@ -169,37 +172,30 @@ const AttendancePage = () => { - {/* Single search input that moves */} - {/*
- setSearchTerm(e.target.value)} - style={{ minWidth: "200px" }} - /> -
*/} - {/* Search + Organization filter */}
{/* Organization Dropdown */} - + {organizations?.length > 1 && ( + + )} {/* Search Input */} { handleModalData={handleModalData} getRole={getRole} searchTerm={searchTerm} + organizationId={appliedFilters.selectedOrganization} />
)} @@ -232,6 +229,7 @@ const AttendancePage = () => { )} @@ -239,6 +237,7 @@ const AttendancePage = () => {
)} diff --git a/src/repositories/AttendanceRepository.jsx b/src/repositories/AttendanceRepository.jsx index a8f1a0b1..8d2fce4e 100644 --- a/src/repositories/AttendanceRepository.jsx +++ b/src/repositories/AttendanceRepository.jsx @@ -2,8 +2,22 @@ import { api } from "../utils/axiosClient"; const AttendanceRepository = { markAttendance: (data) => api.post("/api/attendance/record", data), - getAttendance: (id) => api.get(`api/attendance/project/team?projectId=${id}`), - getAttendanceFilteredByDate: (projectId, fromDate, toDate) => { + + getAttendance: (projectId, organizationId, includeInactive,date) => { + let url = `/api/attendance/project/team?projectId=${projectId}`; + + const params = []; + if (organizationId) params.push(`organizationId=${organizationId}`); + if (includeInactive) params.push(`includeInactive=${includeInactive}`); + if (date) params.push(`date=${date}`); + + if (params.length > 0) { + url += `&${params.join("&")}`; } + + return api.get(url); + }, + + getAttendanceFilteredByDate: (projectId, fromDate, toDate,organizationId) => { let url = `api/Attendance/project/log?projectId=${projectId}`; if (fromDate) { url += `&dateFrom=${fromDate}`; @@ -12,12 +26,26 @@ const AttendanceRepository = { if (toDate) { url += `&dateTo=${toDate}`; } + if (organizationId) { + url += `&organizationId=${organizationId}`; + } return api.get(url); }, getAttendanceLogs: (id) => api.get(`api/attendance/log/attendance/${id}`), - getRegularizeList: (id) => - api.get(`api/attendance/regularize?projectId=${id}`), + + getRegularizeList: (projectId, organizationId, IncludeInActive) => { + let url = `/api/attendance/regularize?projectId=${projectId}`; + + const params = []; + if (organizationId) params.push(`organizationId=${organizationId}`); + if (IncludeInActive) params.push(`IncludeInActive=${IncludeInActive}`); + + if (params.length > 0) { + url += `&${params.join("&")}`; } + + return api.get(url); + }, getAttendanceByEmployee: (employeeId, fromDate, toDate) => { let url = `api/Attendance/log/employee/${employeeId}?`;