Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Feb 16, 2024
1 parent d774142 commit 11e6da4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
9 changes: 2 additions & 7 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,9 @@ export async function createProject(project: Project): Promise<Project> {
export async function getAllActiveProjects(
userId?: string
): Promise<Project[]> {
userId ||= LocalStorage.getUserId();
const user = await getUser(userId);
if (user.id === LocalStorage.getUserId()) {
LocalStorage.setCurrentUser(user);
}
const projectIds = Object.keys(user.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
8 changes: 3 additions & 5 deletions src/components/ProjectScreen/ChooseProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ export default function ChooseProject(): ReactElement {
const navigate = useNavigate();

useEffect(() => {
getAllActiveProjects()
.then((projects) => {
setProjectList(projects.sort((a, b) => a.name.localeCompare(b.name)));
})
.catch((err) => console.error(err));
getAllActiveProjects().then((projects) => {
setProjectList(projects.sort((a, b) => a.name.localeCompare(b.name)));
});
}, []);

const selectProject = (project: Project): void => {
Expand Down
10 changes: 3 additions & 7 deletions src/components/ProjectSettings/ProjectSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export default function ProjectSelect(
const [projList, setProjList] = useState<Project[]>([]);

useEffect(() => {
getAllActiveProjects()
.then(setProjList)
.catch((err) => console.error(err));
getAllActiveProjects().then(setProjList);
}, [props.project.name]);

const handleChange = (e: SelectChangeEvent): void => {
Expand All @@ -27,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

0 comments on commit 11e6da4

Please sign in to comment.