2024-03-18 21:08:33 +01:00
|
|
|
import { useState, createContext, useEffect } from "react";
|
2024-03-18 09:56:07 +01:00
|
|
|
import { Project } from "../Types/goTypes";
|
2024-03-18 10:07:59 +01:00
|
|
|
import { api } from "../API/API";
|
2024-03-18 10:22:38 +01:00
|
|
|
import { Link } from "react-router-dom";
|
2024-03-18 10:07:59 +01:00
|
|
|
import BasicWindow from "../Components/BasicWindow";
|
2024-03-07 10:05:45 +01:00
|
|
|
|
2024-03-18 12:49:58 +01:00
|
|
|
export const ProjectNameContext = createContext("");
|
|
|
|
|
2024-03-18 10:07:59 +01:00
|
|
|
function UserProjectPage(): JSX.Element {
|
|
|
|
const [projects, setProjects] = useState<Project[]>([]);
|
2024-03-18 12:49:58 +01:00
|
|
|
const [selectedProject, setSelectedProject] = useState("");
|
2024-03-18 10:07:59 +01:00
|
|
|
|
|
|
|
const getProjects = async (): Promise<void> => {
|
2024-03-18 10:22:38 +01:00
|
|
|
const username = localStorage.getItem("username") ?? ""; // replace with actual username
|
|
|
|
const token = localStorage.getItem("accessToken") ?? ""; // replace with actual token
|
2024-03-18 10:07:59 +01:00
|
|
|
const response = await api.getUserProjects(username, token);
|
2024-03-18 10:43:52 +01:00
|
|
|
console.log(response);
|
2024-03-18 10:07:59 +01:00
|
|
|
if (response.success) {
|
|
|
|
setProjects(response.data ?? []);
|
|
|
|
} else {
|
|
|
|
console.error(response.message);
|
|
|
|
}
|
|
|
|
};
|
2024-03-18 12:49:58 +01:00
|
|
|
// Call getProjects when the component mounts
|
|
|
|
useEffect(() => {
|
2024-03-18 21:08:33 +01:00
|
|
|
void getProjects();
|
2024-03-18 12:49:58 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleProjectClick = (projectName: string): void => {
|
|
|
|
setSelectedProject(projectName);
|
|
|
|
};
|
2024-03-16 02:23:52 +01:00
|
|
|
|
2024-03-07 11:43:19 +01:00
|
|
|
const content = (
|
2024-03-18 12:49:58 +01:00
|
|
|
<ProjectNameContext.Provider value={selectedProject}>
|
2024-03-07 11:43:19 +01:00
|
|
|
<h1 className="font-bold text-[30px] mb-[20px]">Your Projects</h1>
|
2024-03-18 10:07:59 +01:00
|
|
|
<div className="border-4 border-black bg-white flex flex-col items-center justify-center min-h-[65vh] h-fit w-[50vw] rounded-3xl content-center overflow-scroll space-y-[10vh] p-[30px]">
|
|
|
|
{projects.map((project, index) => (
|
2024-03-18 12:49:58 +01:00
|
|
|
<Link
|
|
|
|
to={`/project/${project.id}`}
|
|
|
|
onClick={() => {
|
|
|
|
handleProjectClick(project.name);
|
|
|
|
}}
|
|
|
|
key={index}
|
|
|
|
>
|
2024-03-18 10:07:59 +01:00
|
|
|
<h1 className="font-bold underline text-[30px] cursor-pointer">
|
|
|
|
{project.name}
|
|
|
|
</h1>
|
|
|
|
</Link>
|
|
|
|
))}
|
2024-03-07 11:43:19 +01:00
|
|
|
</div>
|
2024-03-18 12:49:58 +01:00
|
|
|
</ProjectNameContext.Provider>
|
2024-03-07 10:05:45 +01:00
|
|
|
);
|
2024-03-07 11:43:19 +01:00
|
|
|
|
|
|
|
const buttons = <></>;
|
|
|
|
|
2024-03-18 21:24:26 +01:00
|
|
|
return <BasicWindow content={content} buttons={buttons} />;
|
2024-03-07 10:05:45 +01:00
|
|
|
}
|
2024-03-18 10:07:59 +01:00
|
|
|
|
|
|
|
export default UserProjectPage;
|