From 93750d69b41fbaa3cad0b4691684863981d4ecd3 Mon Sep 17 00:00:00 2001 From: "D. Ror" Date: Mon, 1 Apr 2024 10:43:35 -0400 Subject: [PATCH] Update current user handling of getAllActiveProjects (#2951) --- src/backend/index.ts | 8 ++++---- src/components/ProjectScreen/ChooseProject.tsx | 13 ++++--------- .../ProjectScreen/tests/ChooseProject.test.tsx | 5 +---- src/components/ProjectSettings/ProjectSelect.tsx | 14 ++++---------- .../ProjectSettings/tests/ProjectSelect.test.tsx | 9 +++------ .../ProjectSettings/tests/index.test.tsx | 1 + 6 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/backend/index.ts b/src/backend/index.ts index 6837bd945d..c2d425c921 100644 --- a/src/backend/index.ts +++ b/src/backend/index.ts @@ -363,12 +363,12 @@ export async function createProject(project: Project): Promise { return resp.data.project; } -export async function getAllActiveProjectsByUser( - userId: string +export async function getAllActiveProjects( + userId?: string ): Promise { - const projectIds = Object.keys((await getUser(userId)).projectRoles); + const user = await getUser(userId || LocalStorage.getUserId()); const projects: Project[] = []; - for (const projectId of projectIds) { + for (const projectId of Object.keys(user.projectRoles)) { try { await getProject(projectId).then( (project) => project.isActive && projects.push(project) diff --git a/src/components/ProjectScreen/ChooseProject.tsx b/src/components/ProjectScreen/ChooseProject.tsx index b77fba67a5..317c488349 100644 --- a/src/components/ProjectScreen/ChooseProject.tsx +++ b/src/components/ProjectScreen/ChooseProject.tsx @@ -10,8 +10,7 @@ import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { type Project } from "api/models"; -import { getAllActiveProjectsByUser } from "backend"; -import { getUserId } from "backend/localStorage"; +import { getAllActiveProjects } from "backend"; import { asyncSetNewCurrentProject } from "components/Project/ProjectActions"; import { useAppDispatch } from "types/hooks"; import { Path } from "types/path"; @@ -25,13 +24,9 @@ export default function ChooseProject(): ReactElement { const navigate = useNavigate(); useEffect(() => { - const userId = getUserId(); - if (userId) { - getAllActiveProjectsByUser(userId).then((projects) => { - projects.sort((a: Project, b: Project) => a.name.localeCompare(b.name)); - setProjectList(projects); - }); - } + getAllActiveProjects().then((projects) => { + setProjectList(projects.sort((a, b) => a.name.localeCompare(b.name))); + }); }, []); const selectProject = async (project: Project): Promise => { diff --git a/src/components/ProjectScreen/tests/ChooseProject.test.tsx b/src/components/ProjectScreen/tests/ChooseProject.test.tsx index a291b8877b..f454dd761f 100644 --- a/src/components/ProjectScreen/tests/ChooseProject.test.tsx +++ b/src/components/ProjectScreen/tests/ChooseProject.test.tsx @@ -8,10 +8,7 @@ import { testInstanceHasText } from "utilities/testRendererUtilities"; import { randomIntString } from "utilities/utilities"; jest.mock("backend", () => ({ - getAllActiveProjectsByUser: () => mockGetProjects(), -})); -jest.mock("backend/localStorage", () => ({ - getUserId: () => "mockId", + getAllActiveProjects: () => mockGetProjects(), })); jest.mock("types/hooks"); jest.mock("react-router-dom", () => ({ diff --git a/src/components/ProjectSettings/ProjectSelect.tsx b/src/components/ProjectSettings/ProjectSelect.tsx index 606dc4d7c2..00304c6bb0 100644 --- a/src/components/ProjectSettings/ProjectSelect.tsx +++ b/src/components/ProjectSettings/ProjectSelect.tsx @@ -2,8 +2,7 @@ import { MenuItem, Select, SelectChangeEvent } from "@mui/material"; import { type ReactElement, useEffect, useState } from "react"; import { type Project } from "api/models"; -import { getAllActiveProjectsByUser } from "backend"; -import { getUserId } from "backend/localStorage"; +import { getAllActiveProjects } from "backend"; import { type ProjectSettingProps } from "components/ProjectSettings/ProjectSettingsTypes"; export default function ProjectSelect( @@ -12,10 +11,7 @@ export default function ProjectSelect( const [projList, setProjList] = useState([]); useEffect(() => { - const userId = getUserId(); - if (userId) { - getAllActiveProjectsByUser(userId).then(setProjList); - } + getAllActiveProjects().then(setProjList); }, [props.project.name]); const handleChange = (e: SelectChangeEvent): void => { @@ -29,10 +25,8 @@ export default function ProjectSelect( }; // This prevents an out-of-range Select error while useEffect is underway. - const projectList = [...projList]; - if (projectList.every((p) => p.name !== props.project.name)) { - projectList.push(props.project); - } + const hasProj = projList.some((p) => p.name === props.project.name); + const projectList = hasProj ? [...projList] : [...projList, props.project]; projectList.sort((a: Project, b: Project) => a.name.localeCompare(b.name)); return ( diff --git a/src/components/ProjectSettings/tests/ProjectSelect.test.tsx b/src/components/ProjectSettings/tests/ProjectSelect.test.tsx index b3dbb2fc5b..4fae880408 100644 --- a/src/components/ProjectSettings/tests/ProjectSelect.test.tsx +++ b/src/components/ProjectSettings/tests/ProjectSelect.test.tsx @@ -5,13 +5,10 @@ import ProjectSelect from "components/ProjectSettings/ProjectSelect"; import { newProject, randomProject } from "types/project"; jest.mock("backend", () => ({ - getAllActiveProjectsByUser: () => mockGetAllActiveProjectsByUser(), -})); -jest.mock("backend/localStorage", () => ({ - getUserId: () => "nonempty-string", + getAllActiveProjects: () => mockGetAllActiveProjects(), })); -const mockGetAllActiveProjectsByUser = jest.fn(); +const mockGetAllActiveProjects = jest.fn(); const mockProjects = [randomProject(), randomProject(), randomProject()]; @@ -27,7 +24,7 @@ const renderSwitch = async (proj = newProject()): Promise => { describe("ProjectSelect", () => { it("has the correct number of options and the current project selected", async () => { - mockGetAllActiveProjectsByUser.mockResolvedValueOnce(mockProjects); + mockGetAllActiveProjects.mockResolvedValueOnce(mockProjects); await renderSwitch(mockProjects[1]); const select = selectMaster.root.findByType(Select); expect(select.props.children).toHaveLength(mockProjects.length); diff --git a/src/components/ProjectSettings/tests/index.test.tsx b/src/components/ProjectSettings/tests/index.test.tsx index 0360e13601..7270774a2c 100644 --- a/src/components/ProjectSettings/tests/index.test.tsx +++ b/src/components/ProjectSettings/tests/index.test.tsx @@ -26,6 +26,7 @@ jest.mock("react-router-dom", () => ({ jest.mock("backend", () => ({ canUploadLift: () => Promise.resolve(false), + getAllActiveProjects: () => Promise.resolve([]), getAllSpeakers: () => Promise.resolve([]), getAllUsers: () => Promise.resolve([]), getCurrentPermissions: () => mockGetCurrentPermissions(),