Added a toggle switch in the Employee List module to view active and inactive employees separately.
This commit is contained in:
parent
0180793456
commit
a3867dfb2f
@ -137,15 +137,14 @@ export const useEmployeesByProject = (projectId) => {
|
|||||||
return { employees, loading, error, recallProjectEmplloyee: fetchData };
|
return { employees, loading, error, recallProjectEmplloyee: fetchData };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useEmployeesAllOrByProjectId = (projectId) => {
|
export const useEmployeesAllOrByProjectId = (projectId, showInactive) => {
|
||||||
const [employees, setEmployees] = useState([]);
|
const [employees, setEmployees] = useState([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async (showInactive) => {
|
||||||
if (projectId) {
|
if (projectId) {
|
||||||
const Employees_cache = getCachedData("employeeListByProject");
|
const Employees_cache = getCachedData("employeeListByProject");
|
||||||
|
|
||||||
if (!Employees_cache || Employees_cache.projectId !== projectId) {
|
if (!Employees_cache || Employees_cache.projectId !== projectId) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
@ -168,30 +167,35 @@ export const useEmployeesAllOrByProjectId = (projectId) => {
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await EmployeeRepository.getAllEmployeeList();
|
const response = await EmployeeRepository.getAllEmployeeList(
|
||||||
|
showInactive
|
||||||
|
);
|
||||||
setEmployees(response.data);
|
setEmployees(response.data);
|
||||||
cacheData("allEmployeeList", { data: response.data });
|
cacheData(cacheKey, { data: response.data });
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError("Failed to fetch data.");
|
setError("Failed to fetch data.");
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
setEmployees(employeesCache.data);
|
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
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
|
}, [projectId]); // Re-fetch when projectId changes
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -25,10 +25,11 @@ const EmployeeList = () => {
|
|||||||
const { profile: loginUser } = useProfile();
|
const { profile: loginUser } = useProfile();
|
||||||
const [selectedProject, setSelectedProject] = useState("");
|
const [selectedProject, setSelectedProject] = useState("");
|
||||||
const { projects, loading: projectLoading } = useProjects();
|
const { projects, loading: projectLoading } = useProjects();
|
||||||
|
const [showInactive, setShowInactive] = useState(false);
|
||||||
const ManageEmployee = useHasUserPermission(MANAGE_EMPLOYEES);
|
const ManageEmployee = useHasUserPermission(MANAGE_EMPLOYEES);
|
||||||
|
|
||||||
const { employees, loading, setLoading, error, recallEmployeeData } =
|
const { employees, loading, setLoading, error, recallEmployeeData } =
|
||||||
useEmployeesAllOrByProjectId(selectedProject);
|
useEmployeesAllOrByProjectId(selectedProject,showInactive);
|
||||||
const [projectsList, setProjectsList] = useState(projects || []);
|
const [projectsList, setProjectsList] = useState(projects || []);
|
||||||
|
|
||||||
const [employeeList, setEmployeeList] = useState([]);
|
const [employeeList, setEmployeeList] = useState([]);
|
||||||
@ -109,9 +110,10 @@ const EmployeeList = () => {
|
|||||||
showToast("Employee deleted successfully.", "success");
|
showToast("Employee deleted successfully.", "success");
|
||||||
clearCacheKey("employeeListByProject");
|
clearCacheKey("employeeListByProject");
|
||||||
clearCacheKey("allEmployeeList");
|
clearCacheKey("allEmployeeList");
|
||||||
|
clearCacheKey("allInactiveEmployeeList");
|
||||||
clearCacheKey("employeeProfile");
|
clearCacheKey("employeeProfile");
|
||||||
setEmployeeList([]);
|
setEmployeeList([]);
|
||||||
recallEmployeeData();
|
recallEmployeeData(showInactive);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
const message =
|
const message =
|
||||||
@ -154,6 +156,12 @@ const EmployeeList = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleToggle = (e) => {
|
||||||
|
setShowInactive(e.target.checked);
|
||||||
|
recallEmployeeData(e.target.checked);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{isCreateModalOpen && (
|
{isCreateModalOpen && (
|
||||||
@ -317,6 +325,25 @@ const EmployeeList = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="text-end mb-2">
|
||||||
|
<label class="switch switch-primary">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
class="switch-input"
|
||||||
|
checked={showInactive}
|
||||||
|
onChange={handleToggle}
|
||||||
|
/>
|
||||||
|
<span class="switch-toggle-slider">
|
||||||
|
<span class="switch-on">
|
||||||
|
<i class="icon-base bx bx-check"></i>
|
||||||
|
</span>
|
||||||
|
<span class="switch-off">
|
||||||
|
<i class="icon-base bx bx-x"></i>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<span class="switch-label">Show Inactive Employees</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
<table
|
<table
|
||||||
className="datatables-users table border-top dataTable no-footer dtr-column text-nowrap"
|
className="datatables-users table border-top dataTable no-footer dtr-column text-nowrap"
|
||||||
id="DataTables_Table_0"
|
id="DataTables_Table_0"
|
||||||
@ -448,7 +475,9 @@ const EmployeeList = () => {
|
|||||||
<div className="d-flex flex-column">
|
<div className="d-flex flex-column">
|
||||||
<a
|
<a
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
navigate(`/employee/${item.id}?for=attendance`)
|
navigate(
|
||||||
|
`/employee/${item.id}?for=attendance`
|
||||||
|
)
|
||||||
}
|
}
|
||||||
className="text-heading text-truncate cursor-pointer"
|
className="text-heading text-truncate cursor-pointer"
|
||||||
>
|
>
|
||||||
@ -490,12 +519,19 @@ const EmployeeList = () => {
|
|||||||
{moment(item.joiningDate).format("DD-MMM-YYYY")}
|
{moment(item.joiningDate).format("DD-MMM-YYYY")}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
|
{showInactive ? <span
|
||||||
|
className="badge bg-label-danger"
|
||||||
|
text-capitalized=""
|
||||||
|
>
|
||||||
|
Inactive
|
||||||
|
</span>
|
||||||
|
:
|
||||||
<span
|
<span
|
||||||
className="badge bg-label-success"
|
className="badge bg-label-success"
|
||||||
text-capitalized=""
|
text-capitalized=""
|
||||||
>
|
>
|
||||||
Active
|
Active
|
||||||
</span>
|
</span>}
|
||||||
</td>
|
</td>
|
||||||
{ManageEmployee && (
|
{ManageEmployee && (
|
||||||
<td className="text-end">
|
<td className="text-end">
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { api } from "../utils/axiosClient";
|
import { api } from "../utils/axiosClient";
|
||||||
|
|
||||||
const EmployeeRepository = {
|
const EmployeeRepository = {
|
||||||
getAllEmployeeList:()=>api.get(`api/employee/list`),
|
getAllEmployeeList:(showInactive)=>api.get(`api/employee/list?showInactive=${showInactive}`),
|
||||||
getEmployeeListByproject: (projectid) =>
|
getEmployeeListByproject: (projectid) =>
|
||||||
api.get(`/api/employee/list/${projectid}`),
|
api.get(`/api/employee/list/${projectid}`),
|
||||||
searchEmployees: (query) =>
|
searchEmployees: (query) =>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user