From 76e290fe79bbb3d7d55fd7c9759bd6df719b6df0 Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Tue, 29 Jul 2025 10:29:21 +0530 Subject: [PATCH] Hide Project Dropdown and All Projects Data for Users Assigned to Only One Project. --- src/components/Layout/Header.jsx | 78 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/src/components/Layout/Header.jsx b/src/components/Layout/Header.jsx index b5b62579..006b5c74 100644 --- a/src/components/Layout/Header.jsx +++ b/src/components/Layout/Header.jsx @@ -37,7 +37,6 @@ const Header = () => { const allowedProjectStatusIds = [ "603e994b-a27f-4e5d-a251-f3d69b0498ba", // On Hold "cdad86aa-8a56-4ff4-b633-9c629057dfef", // In Progress - // "ef1c356e-0fe0-42df-a5d3-8daee355492d", // Inactive - Removed as per requirement "b74da4c2-d07e-46f2-9919-e75e49b12731", // Active ]; @@ -93,44 +92,54 @@ const Header = () => { (store) => store.localVariables.projectId ); - // Conditional filtering for projectsForDropdown - // If on Dashboard, show all projects. Otherwise, filter by allowedProjectStatusIds. const projectsForDropdown = isDashboard ? projectNames // On dashboard, show all projects : projectNames?.filter(project => allowedProjectStatusIds.includes(project.projectStatusId) ); - // Determine the display text for the project dropdown - let displayText = "All Projects"; - if (selectedProject === null) { - displayText = "All Projects"; - } else if (selectedProject) { - // Find the selected project from the full projectNames list - const selectedProjectObj = projectNames?.find( - (p) => p?.id === selectedProject - ); - // Fallback to selectedProject ID if name not found during loading or mismatch - displayText = selectedProjectObj ? selectedProjectObj.name : selectedProject; - } else if (projectLoading) { - displayText = "Loading..."; + // Determine the display text for the project section + let currentProjectDisplayName = "Loading..."; + if (!projectLoading && projectNames) { + if (projectNames.length === 0) { + // If no projects are assigned at all + currentProjectDisplayName = "No Projects Assigned"; + } else if (projectNames.length === 1) { + // If there's exactly one project overall (unfiltered list) + currentProjectDisplayName = projectNames[0].name; + } else { // projectNames.length > 1 (multiple projects) + if (selectedProject === null) { + currentProjectDisplayName = "All Projects"; + } else { + const selectedProjectObj = projectNames.find( + (p) => p?.id === selectedProject + ); + currentProjectDisplayName = selectedProjectObj ? selectedProjectObj.name : "Unknown Project"; + } + } } const { openChangePassword } = useChangePassword(); + // Effect to set initial projectId based on scenarios useEffect(() => { if ( projectNames && projectNames.length > 0 && - selectedProject === undefined && - !getCachedData("hasReceived") + selectedProject === undefined && // Only set if no project is explicitly selected yet + !getCachedData("hasReceived") // To avoid re-setting on every render ) { - if (isDashboard) { - dispatch(setProjectId(null)); // Always set to null for "All Projects" on Dashboard initial load + if (projectNames.length === 1) { + // If only one project exists, automatically select it + dispatch(setProjectId(projectNames[0]?.id || null)); } else { - // If not dashboard, set to the first project that matches the allowed statuses if available - const firstAllowedProject = projectNames.find(project => allowedProjectStatusIds.includes(project.projectStatusId)); - dispatch(setProjectId(firstAllowedProject?.id || null)); // Fallback to null if no allowed projects + // If multiple projects, default to "All Projects" on dashboard, or first allowed for others + if (isDashboard) { + dispatch(setProjectId(null)); // Default to "All Projects" for dashboard when multiple projects + } else { + const firstAllowedProject = projectNames.find(project => allowedProjectStatusIds.includes(project.projectStatusId)); + dispatch(setProjectId(firstAllowedProject?.id || null)); + } } } }, [projectNames, selectedProject, dispatch, isDashboard]); @@ -183,12 +192,10 @@ const Header = () => { if (isProjectPath && project !== null) { navigate("/projects/details"); // Navigate only if on /projects and a specific project is selected } - // No navigation if on dashboard or if "All Projects" is selected }; - // Determine if the dropdown should be shown - const shouldShowDropdown = - isDashboard || (projectsForDropdown && projectsForDropdown.length > 1); + // Determine if the dropdown should be shown: Only if there are genuinely multiple projects + const shouldShowDropdown = projectNames && projectNames.length > 1; return (