From a3867dfb2f1630639d88e54cd5f8fce75b5ecd96 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Thu, 8 May 2025 10:05:42 +0530 Subject: [PATCH] Added a toggle switch in the Employee List module to view active and inactive employees separately. --- src/hooks/useEmployees.js | 28 +++++++++------- src/pages/employee/EmployeeList.jsx | 44 ++++++++++++++++++++++--- src/repositories/EmployeeRepository.jsx | 2 +- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/hooks/useEmployees.js b/src/hooks/useEmployees.js index 31e4220b..a80d8c8f 100644 --- a/src/hooks/useEmployees.js +++ b/src/hooks/useEmployees.js @@ -16,7 +16,7 @@ export const useAllEmployees = () => { setLoading(true); const response = await EmployeeRepository.getAllEmployeeList(); cacheData("AllEmployees", response.data); - setEmployeeList( response.data ); + setEmployeeList(response.data); setLoading(false); } else { setEmployeeList(EmployeeList_cached); @@ -137,15 +137,14 @@ export const useEmployeesByProject = (projectId) => { return { employees, loading, error, recallProjectEmplloyee: fetchData }; }; -export const useEmployeesAllOrByProjectId = (projectId) => { +export const useEmployeesAllOrByProjectId = (projectId, showInactive) => { const [employees, setEmployees] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); - const fetchData = async () => { + const fetchData = async (showInactive) => { if (projectId) { const Employees_cache = getCachedData("employeeListByProject"); - if (!Employees_cache || Employees_cache.projectId !== projectId) { setLoading(true); setError(null); @@ -168,30 +167,35 @@ export const useEmployeesAllOrByProjectId = (projectId) => { setLoading(false); } } else { - const employeesCache = getCachedData("allEmployeeList"); + const cacheKey = showInactive + ? "allInactiveEmployeeList" + : "allEmployeeList"; + const employeesCache = getCachedData(cacheKey); - if (!employeesCache) { + if (employeesCache) { + setEmployees(employeesCache.data); + setLoading(false); + } else { setLoading(true); setError(null); try { - const response = await EmployeeRepository.getAllEmployeeList(); + const response = await EmployeeRepository.getAllEmployeeList( + showInactive + ); setEmployees(response.data); - cacheData("allEmployeeList", { data: response.data }); + cacheData(cacheKey, { data: response.data }); setLoading(false); } catch (err) { setError("Failed to fetch data."); setLoading(false); } - } else { - setEmployees(employeesCache.data); - setLoading(false); } } }; useEffect(() => { - fetchData(); // Fetch data when the component mounts or projectId changes + fetchData(showInactive); // Fetch data when the component mounts or projectId changes }, [projectId]); // Re-fetch when projectId changes return { diff --git a/src/pages/employee/EmployeeList.jsx b/src/pages/employee/EmployeeList.jsx index ce21b92d..a7f0a723 100644 --- a/src/pages/employee/EmployeeList.jsx +++ b/src/pages/employee/EmployeeList.jsx @@ -25,10 +25,11 @@ const EmployeeList = () => { const { profile: loginUser } = useProfile(); const [selectedProject, setSelectedProject] = useState(""); const { projects, loading: projectLoading } = useProjects(); + const [showInactive, setShowInactive] = useState(false); const ManageEmployee = useHasUserPermission(MANAGE_EMPLOYEES); const { employees, loading, setLoading, error, recallEmployeeData } = - useEmployeesAllOrByProjectId(selectedProject); + useEmployeesAllOrByProjectId(selectedProject,showInactive); const [projectsList, setProjectsList] = useState(projects || []); const [employeeList, setEmployeeList] = useState([]); @@ -109,9 +110,10 @@ const EmployeeList = () => { showToast("Employee deleted successfully.", "success"); clearCacheKey("employeeListByProject"); clearCacheKey("allEmployeeList"); + clearCacheKey("allInactiveEmployeeList"); clearCacheKey("employeeProfile"); setEmployeeList([]); - recallEmployeeData(); + recallEmployeeData(showInactive); }) .catch((error) => { const message = @@ -154,6 +156,12 @@ const EmployeeList = () => { } }; + const handleToggle = (e) => { + setShowInactive(e.target.checked); + recallEmployeeData(e.target.checked); + }; + + return ( <> {isCreateModalOpen && ( @@ -317,6 +325,25 @@ const EmployeeList = () => { +
+ +
{
- navigate(`/employee/${item.id}?for=attendance`) + navigate( + `/employee/${item.id}?for=attendance` + ) } className="text-heading text-truncate cursor-pointer" > @@ -490,12 +519,19 @@ const EmployeeList = () => { {moment(item.joiningDate).format("DD-MMM-YYYY")}
{ManageEmployee && (
+ {showInactive ? + Inactive + + : Active - + } diff --git a/src/repositories/EmployeeRepository.jsx b/src/repositories/EmployeeRepository.jsx index de01948e..ab5d2e05 100644 --- a/src/repositories/EmployeeRepository.jsx +++ b/src/repositories/EmployeeRepository.jsx @@ -1,7 +1,7 @@ import { api } from "../utils/axiosClient"; const EmployeeRepository = { - getAllEmployeeList:()=>api.get(`api/employee/list`), + getAllEmployeeList:(showInactive)=>api.get(`api/employee/list?showInactive=${showInactive}`), getEmployeeListByproject: (projectid) => api.get(`/api/employee/list/${projectid}`), searchEmployees: (query) =>