From c38a92b8b71ef8ab0c7b7e68a786f2b9d9277649 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Wed, 23 Apr 2025 11:01:14 +0530 Subject: [PATCH] added list view component --- src/pages/project/ProjectList.jsx | 222 ++++++++++++++---------------- 1 file changed, 105 insertions(+), 117 deletions(-) diff --git a/src/pages/project/ProjectList.jsx b/src/pages/project/ProjectList.jsx index 3323652a..a21a09a0 100644 --- a/src/pages/project/ProjectList.jsx +++ b/src/pages/project/ProjectList.jsx @@ -17,42 +17,39 @@ const ProjectList = () => { const [listView, setListView] = useState(true); const [showModal, setShowModal] = useState(false); const { projects, loading, error, refetch } = useProjects(); - const [refresh, setRefresh] = useState(false); const [projectList, setProjectList] = useState([]); const HasManageProjectPermission = useHasUserPermission(MANAGE_PROJECT); const [HasManageProject, setHasManageProject] = useState( HasManageProjectPermission ); const dispatch = useDispatch(); + const [currentPage, setCurrentPage] = useState(1); const [itemsPerPage] = useState(6); + const [searchTerm, setSearchTerm] = useState(""); + const [selectedStatuses, setSelectedStatuses] = useState([1, 2, 3, 4]); const handleShow = () => setShowModal(true); const handleClose = () => setShowModal(false); useEffect(() => { if (!loading && Array.isArray(projects)) { - // Step 1: Group projects by statusId const grouped = {}; - projects.forEach((project) => { const statusId = project.projectStatusId; - if (!grouped[statusId]) { - grouped[statusId] = []; - } + if (!grouped[statusId]) grouped[statusId] = []; grouped[statusId].push(project); }); - // Step 2: Sort each group by name const sortedGrouped = Object.keys(grouped) - .sort() // sort group keys (status IDs) + .sort() .flatMap((statusId) => grouped[statusId].sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) ) ); - setProjectList(sortedGrouped); // final sorted flat list + setProjectList(sortedGrouped); } }, [projects, loginUser?.projects, loading]); @@ -67,22 +64,16 @@ const ProjectList = () => { const handleSubmitForm = (newProject) => { ProjectRepository.manageProject(newProject) .then((response) => { - const cachedProjects_list = getCachedData("projectslist") || []; - - const updated_Projects_list = [...cachedProjects_list, response.data]; - - cacheData("projectslist", updated_Projects_list); - setProjectList((prevProjectList) => [ - ...prevProjectList, - response.data, - ]); - + const cachedProjects = getCachedData("projectslist") || []; + const updatedProjects = [...cachedProjects, response.data]; + cacheData("projectslist", updatedProjects); + setProjectList((prev) => [...prev, response.data]); showToast("Project Created successfully.", "success"); setShowModal(false); }) .catch((error) => { - closeModal(); showToast(error.message, "error"); + setShowModal(false); }); }; @@ -90,19 +81,41 @@ const ProjectList = () => { if (!projects || projects.length === 0) { refetch(); } - setRefresh((prev) => !prev); }; + 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 indexOfLastItem = currentPage * itemsPerPage; const indexOfFirstItem = indexOfLastItem - itemsPerPage; - const currentItems = Array.isArray(projectList) - ? projectList.slice(indexOfFirstItem, indexOfLastItem) - : []; - - const paginate = (pageNumber) => setCurrentPage(pageNumber); - const totalPages = Array.isArray(projectList) - ? Math.ceil(projectList.length / itemsPerPage) - : 0; + const currentItems = filteredProjects.slice( + indexOfFirstItem, + indexOfLastItem + ); + const totalPages = Math.ceil( filteredProjects.length / itemsPerPage ); + + useEffect(() => { + const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]')); + tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); + }, []); return ( <> @@ -117,7 +130,7 @@ const ProjectList = () => { project={null} handleSubmitForm={handleSubmitForm} onClose={handleClose} - > + />
@@ -126,55 +139,54 @@ const ProjectList = () => { { label: "Home", link: "/dashboard" }, { label: "Projects", link: null }, ]} - > + /> -
- {/*
- - -
*/} +
+
+
+ { + setSearchTerm(e.target.value); + setCurrentPage(1); + }} + /> +
-
-
- +
+
+
- {((error && !loading) || !projects) && ( -

- There was an error loading the projects. Please try again. -

+ {loading &&

Loading...

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

No projects found.

)} - {(!projects || projects.length === 0 || projectList.length == 0) && - !loading && - error && ( -
- -
- )} -
- {loading &&

Loading...

} - - {listView ? : - currentItems && - currentItems.map((item) => ( - - ))} -======= - - - {Array.isArray(currentItems) && loginUser?.projects && ( - currentItems - .filter((item) => loginUser.projects.includes(String(item.id))) - .map((item) => ( - - )) -)} ->>>>>>> 148058d1d22430d0c2a314cdade3509b38c5d090 + {listView ? ( + + ) : ( + currentItems.map((project) => ( + + )) + )}
- {/* Pagination */} - {!loading && ( -