import React, { useState, useEffect, useCallback } from "react"; import ProjectCard from "../../components/Project/ProjectCard"; import ManageProjectInfo from "../../components/Project/ManageProjectInfo"; import Breadcrumb from "../../components/common/Breadcrumb"; import ProjectRepository from "../../repositories/ProjectRepository"; import { useProjects, useCreateProject } from "../../hooks/useProjects"; import showToast from "../../services/toastService"; // import { // getCachedData, // cacheData, // clearCacheKey, // } from "../../slices/apiDataManager"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import { useProfile } from "../../hooks/useProfile"; import { ITEMS_PER_PAGE, MANAGE_PROJECT } from "../../utils/constants"; import ProjectListView from "./ProjectListView"; import eventBus from "../../services/eventBus"; import { clearApiCacheKey } from "../../slices/apiCacheSlice"; import { defaultCheckBoxAppearanceProvider } from "pdf-lib"; import { useMutation } from "@tanstack/react-query"; import usePagination from "../../hooks/usePagination"; import GlobalModel from "../../components/common/GlobalModel"; const ProjectList = () => { const { profile: loginUser } = useProfile(); const [listView, setListView] = useState(false); const [showModal, setShowModal] = useState(false); const { projects, loading, error, refetch } = useProjects(); const [projectList, setProjectList] = useState([]); const HasManageProjectPermission = useHasUserPermission(MANAGE_PROJECT); const [HasManageProject, setHasManageProject] = useState(HasManageProjectPermission); const { mutate: createProject,isPending } = useCreateProject({ onSuccessCallback: () => { setShowModal(false); }, }); const [searchTerm, setSearchTerm] = useState(""); const [selectedStatuses, setSelectedStatuses] = useState([ "b74da4c2-d07e-46f2-9919-e75e49b12731", "603e994b-a27f-4e5d-a251-f3d69b0498ba", "ef1c356e-0fe0-42df-a5d3-8daee355492d", "cdad86aa-8a56-4ff4-b633-9c629057dfef", "33deaef9-9af1-4f2a-b443-681ea0d04f81", ]); const handleShow = () => setShowModal(true); const handleClose = () => setShowModal(false); const sortingProject = (projects) => { if (!loading && Array.isArray(projects)) { const grouped = {}; projects.forEach((project) => { const statusId = project.projectStatusId; if (!grouped[statusId]) grouped[statusId] = []; grouped[statusId].push(project); }); const sortedGrouped = selectedStatuses .filter((statusId) => grouped[statusId]) .flatMap((statusId) => grouped[statusId].sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) ) ); setProjectList((prev) => { const isSame = JSON.stringify(prev) === JSON.stringify(sortedGrouped); return isSame ? prev : sortedGrouped; }); } }; useEffect(() => { if (!loading && projects) { sortingProject(projects); } }, [projects, loading, selectedStatuses]); useEffect(() => { setHasManageProject(loginUser ? HasManageProjectPermission : false); }, [loginUser, HasManageProjectPermission]); const handleSubmitForm = (newProject) => { createProject(newProject); }; const handleStatusChange = (statusId) => { setCurrentPage(1); setSelectedStatuses((prev) => prev.includes(statusId) ? prev.filter((id) => id !== statusId) : [...prev, statusId] ); }; const handleStatusFilterFromChild = (statusesFromChild) => { setSelectedStatuses(statusesFromChild); }; const filteredProjects = projectList.filter((project) => { const matchesStatus = selectedStatuses.includes(project.projectStatusId); const matchesSearch = project.name .toLowerCase() .includes(searchTerm.toLowerCase()); return matchesStatus && matchesSearch; }); const totalPages = Math.ceil(filteredProjects.length / ITEMS_PER_PAGE); const { currentItems, currentPage, paginate, setCurrentPage, } = usePagination(filteredProjects, ITEMS_PER_PAGE); useEffect(() => { const tooltipTriggerList = Array.from( document.querySelectorAll('[data-bs-toggle="tooltip"]') ); tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); }, []); return ( <> {showModal && ( )}
{ setSearchTerm(e.target.value); setCurrentPage(1); }} />
    {[ { id: "b74da4c2-d07e-46f2-9919-e75e49b12731", label: "Active", }, { id: "cdad86aa-8a56-4ff4-b633-9c629057dfef", label:"In Progress" }, { id: "603e994b-a27f-4e5d-a251-f3d69b0498ba", label: "On Hold", }, { id: "ef1c356e-0fe0-42df-a5d3-8daee355492d", label: "Inactive", }, { id: "33deaef9-9af1-4f2a-b443-681ea0d04f81", label: "Completed", }, ].map(({ id, label }) => (
  • handleStatusChange(id)} />
  • ))}
{loading &&

Loading...

} {!loading && filteredProjects.length === 0 && !listView && (

No projects found.

)} {listView ? (
{currentItems.length === 0 ? ( ) : ( currentItems.map((project) => ( )) )}
Project Name Contact Person START DATE DEADLINE Task Progress
    {[ { id: "b74da4c2-d07e-46f2-9919-e75e49b12731", label: "Active", }, { id: "603e994b-a27f-4e5d-a251-f3d69b0498ba", label: "On Hold", }, { id: "ef1c356e-0fe0-42df-a5d3-8daee355492d", label: "Inactive", }, { id: "33deaef9-9af1-4f2a-b443-681ea0d04f81", label: "Completed", }, ].map(({ id, label }) => (
  • handleStatusChange(id)} />
  • ))}
Action

No projects found

) : (
{currentItems.map((project) => ( ))}
)} {!loading && totalPages > 1 && ( )} {!loading && totalPages > 1 && ( )}
); }; export default ProjectList;