From ae0208ff2315be51f304e3fb87cf6872eaed1fd3 Mon Sep 17 00:00:00 2001 From: Davenludd Date: Thu, 21 Mar 2024 10:02:48 +0100 Subject: [PATCH] Add project navigation based on user and pm role --- .../src/Components/DisplayUserProjects.tsx | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/frontend/src/Components/DisplayUserProjects.tsx b/frontend/src/Components/DisplayUserProjects.tsx index f4fd782..1d1ecfe 100644 --- a/frontend/src/Components/DisplayUserProjects.tsx +++ b/frontend/src/Components/DisplayUserProjects.tsx @@ -1,6 +1,6 @@ import { useState, useEffect } from "react"; import { Project } from "../Types/goTypes"; -import { Link } from "react-router-dom"; +import { Link, useNavigate } from "react-router-dom"; import { api } from "../API/API"; /** @@ -9,6 +9,7 @@ import { api } from "../API/API"; */ function DisplayUserProject(): JSX.Element { const [projects, setProjects] = useState([]); + const navigate = useNavigate(); const getProjects = async (): Promise => { const token = localStorage.getItem("accessToken") ?? ""; @@ -21,6 +22,26 @@ function DisplayUserProject(): JSX.Element { } }; + const handleProjectClick = async (projectName: string): Promise => { + const username = localStorage.getItem("username") ?? ""; + const token = localStorage.getItem("accessToken") ?? ""; + const response = await api.checkIfProjectManager( + username, + projectName, + token, + ); + if (response.success) { + if (response.data) { + navigate(`/PMProjectPage/${projectName}`); + } else { + navigate(`/project/${projectName}`); + } + } else { + // handle error + console.error(response.message); + } + }; + // Call getProjects when the component mounts useEffect(() => { void getProjects(); @@ -30,12 +51,15 @@ function DisplayUserProject(): JSX.Element { <>

Your Projects

- {projects.map((project, index) => ( - + {projects.map((project) => ( +
void handleProjectClick(project.name)} + key={project.id} + >

{project.name}

- +
))}