Skip to content

Commit

Permalink
Update current user handling of getAllActiveProjects (#2951)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Apr 1, 2024
1 parent 65f3ba5 commit 93750d6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ export async function createProject(project: Project): Promise<Project> {
return resp.data.project;
}

export async function getAllActiveProjectsByUser(
userId: string
export async function getAllActiveProjects(
userId?: string
): Promise<Project[]> {
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)
Expand Down
13 changes: 4 additions & 9 deletions src/components/ProjectScreen/ChooseProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<void> => {
Expand Down
5 changes: 1 addition & 4 deletions src/components/ProjectScreen/tests/ChooseProject.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => ({
Expand Down
14 changes: 4 additions & 10 deletions src/components/ProjectSettings/ProjectSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -12,10 +11,7 @@ export default function ProjectSelect(
const [projList, setProjList] = useState<Project[]>([]);

useEffect(() => {
const userId = getUserId();
if (userId) {
getAllActiveProjectsByUser(userId).then(setProjList);
}
getAllActiveProjects().then(setProjList);
}, [props.project.name]);

const handleChange = (e: SelectChangeEvent): void => {
Expand All @@ -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 (
Expand Down
9 changes: 3 additions & 6 deletions src/components/ProjectSettings/tests/ProjectSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()];

Expand All @@ -27,7 +24,7 @@ const renderSwitch = async (proj = newProject()): Promise<void> => {

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);
Expand Down
1 change: 1 addition & 0 deletions src/components/ProjectSettings/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 93750d6

Please sign in to comment.