Small fixes in all files that fetches user projects, so that they pass username as argument to GetProjects-function

This commit is contained in:
Peter KW 2024-03-28 21:31:30 +01:00
parent 85795f5406
commit b036ef906c
6 changed files with 28 additions and 28 deletions

View file

@ -114,10 +114,14 @@ interface API {
): Promise<APIResponse<WeeklyReport[]>>; ): Promise<APIResponse<WeeklyReport[]>>;
/** Gets all the projects of a user /** Gets all the projects of a user
* @param {string} username - The authentication token.
* @param {string} token - The authentication token. * @param {string} token - The authentication token.
* @returns {Promise<APIResponse<Project[]>>} A promise containing the API response with the user's projects. * @returns {Promise<APIResponse<Project[]>>} A promise containing the API response with the user's projects.
*/ */
getUserProjects(token: string): Promise<APIResponse<Project[]>>; getUserProjects(
username: string,
token: string,
): Promise<APIResponse<Project[]>>;
/** Gets a project by its id. /** Gets a project by its id.
* @param {number} id The id of the project to retrieve. * @param {number} id The id of the project to retrieve.
@ -302,9 +306,12 @@ export const api: API = {
} }
}, },
async getUserProjects(token: string): Promise<APIResponse<Project[]>> { async getUserProjects(
username: string,
token: string,
): Promise<APIResponse<Project[]>> {
try { try {
const response = await fetch("/api/getUserProjects", { const response = await fetch(`/api/getUserProjects/${username}`, {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",

View file

@ -1,7 +1,7 @@
import { useState, useEffect } from "react"; import { useState } from "react";
import { Project } from "../Types/goTypes"; import { Project } from "../Types/goTypes";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { api } from "../API/API"; import GetProjects from "./GetProjects";
/** /**
* Renders a component that displays the projects a user is a part of and links to the projects start-page. * Renders a component that displays the projects a user is a part of and links to the projects start-page.
@ -10,21 +10,10 @@ import { api } from "../API/API";
function DisplayUserProject(): JSX.Element { function DisplayUserProject(): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]); const [projects, setProjects] = useState<Project[]>([]);
const getProjects = async (): Promise<void> => { GetProjects({
const token = localStorage.getItem("accessToken") ?? ""; setProjectsProp: setProjects,
const response = await api.getUserProjects(token); username: localStorage.getItem("username") ?? "",
console.log(response); });
if (response.success) {
setProjects(response.data ?? []);
} else {
console.error(response.message);
}
};
// Call getProjects when the component mounts
useEffect(() => {
void getProjects();
}, []);
return ( return (
<> <>

View file

@ -12,6 +12,7 @@ import { api } from "../API/API";
*/ */
function GetProjects(props: { function GetProjects(props: {
setProjectsProp: Dispatch<React.SetStateAction<Project[]>>; setProjectsProp: Dispatch<React.SetStateAction<Project[]>>;
username: string;
}): void { }): void {
const setProjects: Dispatch<React.SetStateAction<Project[]>> = const setProjects: Dispatch<React.SetStateAction<Project[]>> =
props.setProjectsProp; props.setProjectsProp;
@ -19,7 +20,7 @@ function GetProjects(props: {
const fetchUsers = async (): Promise<void> => { const fetchUsers = async (): Promise<void> => {
try { try {
const token = localStorage.getItem("accessToken") ?? ""; const token = localStorage.getItem("accessToken") ?? "";
const response = await api.getUserProjects(token); const response = await api.getUserProjects(props.username, token);
if (response.success) { if (response.success) {
setProjects(response.data ?? []); setProjects(response.data ?? []);
} else { } else {
@ -31,7 +32,7 @@ function GetProjects(props: {
}; };
void fetchUsers(); void fetchUsers();
}, [setProjects]); }, [props.username, setProjects]);
} }
export default GetProjects; export default GetProjects;

View file

@ -42,7 +42,7 @@ function UserInfoModal(props: {
Member of these projects: Member of these projects:
</h2> </h2>
<div className="pr-6 pl-6"> <div className="pr-6 pl-6">
<UserProjectListAdmin /> <UserProjectListAdmin username={props.username} />
</div> </div>
</div> </div>
<div className="items-center space-x-6 pr-6 pl-6"> <div className="items-center space-x-6 pr-6 pl-6">

View file

@ -2,16 +2,16 @@ import { useEffect, useState } from "react";
import { api } from "../API/API"; import { api } from "../API/API";
import { Project } from "../Types/goTypes"; import { Project } from "../Types/goTypes";
function UserProjectListAdmin(): JSX.Element { function UserProjectListAdmin(props: { username: string }): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]); const [projects, setProjects] = useState<Project[]>([]);
useEffect(() => { useEffect(() => {
const fetchProjects = async (): Promise<void> => { const fetchProjects = async (): Promise<void> => {
try { try {
const token = localStorage.getItem("accessToken") ?? ""; const token = localStorage.getItem("accessToken") ?? "";
// const username = props.username; const username = props.username;
const response = await api.getUserProjects(token); const response = await api.getUserProjects(username, token);
if (response.success) { if (response.success) {
setProjects(response.data ?? []); setProjects(response.data ?? []);
} else { } else {
@ -23,7 +23,7 @@ function UserProjectListAdmin(): JSX.Element {
}; };
void fetchProjects(); void fetchProjects();
}, []); }, [props.username]);
return ( return (
<div className="border-2 border-black bg-white p-2 rounded-lg text-center"> <div className="border-2 border-black bg-white p-2 rounded-lg text-center">

View file

@ -9,7 +9,10 @@ import { useState } from "react";
function AdminManageProjects(): JSX.Element { function AdminManageProjects(): JSX.Element {
const [projects, setProjects] = useState<Project[]>([]); const [projects, setProjects] = useState<Project[]>([]);
GetProjects({ setProjectsProp: setProjects }); GetProjects({
setProjectsProp: setProjects,
username: localStorage.getItem("username") ?? "",
});
const content = ( const content = (
<> <>
<h1 className="font-bold text-[30px] mb-[20px]">Manage Projects</h1> <h1 className="font-bold text-[30px] mb-[20px]">Manage Projects</h1>