From 2eecf17d42ab962d7c0f01cf49e4ee4fe2153e27 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 14:43:09 +0100 Subject: [PATCH 1/5] Login feature API --- frontend/src/API/API.ts | 72 +++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index ed64f71..be041f1 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -22,12 +22,19 @@ interface API { token: string, ): Promise>; /** Submit a weekly report */ - submitWeeklyReport(project: NewWeeklyReport, token: string): Promise>; + submitWeeklyReport( + project: NewWeeklyReport, + token: string, + ): Promise>; /** Renew the token */ renewToken(token: string): Promise>; /** Gets all the projects of a user*/ - getUserProjects(username: string, token: string): Promise>; - + getUserProjects( + username: string, + token: string, + ): Promise>; + /** Login */ + login(NewUser: NewUser): Promise>; } // Export an instance of the API @@ -55,7 +62,7 @@ export const api: API = { async removeUser( username: string, - token: string + token: string, ): Promise> { try { const response = await fetch("/api/userdelete", { @@ -80,7 +87,7 @@ export const api: API = { async createProject( project: NewProject, - token: string + token: string, ): Promise> { try { const response = await fetch("/api/project", { @@ -135,18 +142,26 @@ export const api: API = { }); if (!response.ok) { - return Promise.resolve({ success: false, message: "Failed to get user projects" }); + return Promise.resolve({ + success: false, + message: "Failed to get user projects", + }); } else { const data = (await response.json()) as Project[]; return Promise.resolve({ success: true, data }); } - } - catch (e) { - return Promise.resolve({ success: false, message: "Failed to get user projects" }); + } catch (e) { + return Promise.resolve({ + success: false, + message: "Failed to get user projects", + }); } }, - - submitWeeklyReport: function (project: NewWeeklyReport, token: string): Promise> { + + async submitWeeklyReport( + project: NewWeeklyReport, + token: string, + ): Promise> { try { return fetch("/api/submitWeeklyReport", { method: "POST", @@ -158,7 +173,10 @@ export const api: API = { }) .then((response) => { if (!response.ok) { - return { success: false, message: "Failed to submit weekly report" }; + return { + success: false, + message: "Failed to submit weekly report", + }; } else { return response.json(); } @@ -167,7 +185,35 @@ export const api: API = { return { success: true, data }; }); } catch (e) { - return Promise.resolve({ success: false, message: "Failed to submit weekly report" }); + return Promise.resolve({ + success: false, + message: "Failed to submit weekly report", + }); + } + }, + + async login(NewUser: NewUser): Promise> { + try { + const response = await fetch("/api/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(NewUser), + }); + + if (response) { + if (!response.ok) { + return { success: false, message: "Failed to login" }; + } else { + const data = await response.json(); + return { success: true, data }; + } + } + + return { success: false, message: "Failed to login" }; // Add return statement + } catch (e) { + return Promise.resolve({ success: false, message: "Failed to login" }); } } }; From cab7c3bdfd5281a5bd176e6a8d49a92b4445044e Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 17 Mar 2024 14:44:25 +0100 Subject: [PATCH 2/5] Formatting and typing to make linter happy --- frontend/src/API/API.ts | 49 ++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index ed64f71..2769650 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -22,12 +22,17 @@ interface API { token: string, ): Promise>; /** Submit a weekly report */ - submitWeeklyReport(project: NewWeeklyReport, token: string): Promise>; + submitWeeklyReport( + project: NewWeeklyReport, + token: string, + ): Promise>; /** Renew the token */ renewToken(token: string): Promise>; /** Gets all the projects of a user*/ - getUserProjects(username: string, token: string): Promise>; - + getUserProjects( + username: string, + token: string, + ): Promise>; } // Export an instance of the API @@ -55,7 +60,7 @@ export const api: API = { async removeUser( username: string, - token: string + token: string, ): Promise> { try { const response = await fetch("/api/userdelete", { @@ -80,7 +85,7 @@ export const api: API = { async createProject( project: NewProject, - token: string + token: string, ): Promise> { try { const response = await fetch("/api/project", { @@ -135,18 +140,26 @@ export const api: API = { }); if (!response.ok) { - return Promise.resolve({ success: false, message: "Failed to get user projects" }); + return Promise.resolve({ + success: false, + message: "Failed to get user projects", + }); } else { const data = (await response.json()) as Project[]; return Promise.resolve({ success: true, data }); } - } - catch (e) { - return Promise.resolve({ success: false, message: "Failed to get user projects" }); + } catch (e) { + return Promise.resolve({ + success: false, + message: "Failed to get user projects", + }); } }, - - submitWeeklyReport: function (project: NewWeeklyReport, token: string): Promise> { + + submitWeeklyReport: function ( + project: NewWeeklyReport, + token: string, + ): Promise> { try { return fetch("/api/submitWeeklyReport", { method: "POST", @@ -158,16 +171,22 @@ export const api: API = { }) .then((response) => { if (!response.ok) { - return { success: false, message: "Failed to submit weekly report" }; + return { + success: false, + message: "Failed to submit weekly report", + }; } else { return response.json(); } }) - .then((data) => { + .then((data: Project) => { return { success: true, data }; }); } catch (e) { - return Promise.resolve({ success: false, message: "Failed to submit weekly report" }); + return Promise.resolve({ + success: false, + message: "Failed to submit weekly report", + }); } - } + }, }; From 5327ac7ad16300e9e77461d64d9418e77fec4e70 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 15:03:34 +0100 Subject: [PATCH 3/5] lint + login function for API --- frontend/src/Types/Project.ts | 13 ------------- frontend/src/Types/TimeReport.ts | 9 --------- frontend/src/Types/Users.ts | 11 ----------- 3 files changed, 33 deletions(-) delete mode 100644 frontend/src/Types/Project.ts delete mode 100644 frontend/src/Types/TimeReport.ts delete mode 100644 frontend/src/Types/Users.ts diff --git a/frontend/src/Types/Project.ts b/frontend/src/Types/Project.ts deleted file mode 100644 index bb4f8c7..0000000 --- a/frontend/src/Types/Project.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface Project { - id: number; - name: string; - description: string; - owner: string; - created: string; // This is a date -} - -export interface NewProject { - name: string; - description: string; - owner: string; -} diff --git a/frontend/src/Types/TimeReport.ts b/frontend/src/Types/TimeReport.ts deleted file mode 100644 index 9685964..0000000 --- a/frontend/src/Types/TimeReport.ts +++ /dev/null @@ -1,9 +0,0 @@ -export interface TimeReport { - week: string; - development: string; - meeting: string; - administration: string; - ownwork: string; - studies: string; - testing: string; -} diff --git a/frontend/src/Types/Users.ts b/frontend/src/Types/Users.ts deleted file mode 100644 index 8323b2e..0000000 --- a/frontend/src/Types/Users.ts +++ /dev/null @@ -1,11 +0,0 @@ -// This is how the API responds -export interface User { - id: number; - userName: string; -} - -// Used to create a new user -export interface NewUser { - userName: string; - password: string; -} From f5787fbc2ee261e20e7d4ca1a17c7cc7fe4ef311 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 15:23:21 +0100 Subject: [PATCH 4/5] correct routes in main --- frontend/src/main.tsx | 106 ------------------------------------------ 1 file changed, 106 deletions(-) diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index ea6ede4..c63ef22 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -20,112 +20,6 @@ const router = createBrowserRouter([ path: "/pm", element: , }, - { - path: "/user", - element: , - path: "/edit-time-report", - element: , - }, - { - path: "/new-time-report", - element: , - }, - { - path: "/project", - element: , - }, - { - path: "/register", - element: , - }, - { - path: "/project-page", - element: , - }, - { - path: "/change-role", - element: , - }, - { - path: "/other-users-time-reports", - element: , - }, - { - path: "/project-members", - element: , - }, - { - path: "/PM-project-page", - element: , - }, - { - path: "/PM-time-activity", - element: , - }, - { - path: "/PM-time-role", - element: , - }, - { - path: "/PM-unsigned-reports", - element: , - }, - { - path: "/PM-view-unsigned-report", - element: , - }, - { - path: "/admin-add-project", - element: , - }, - { - path: "/admin-add-user", - element: , - }, - { - path: "/admin-change-username", - element: , - }, - { - path: "/admin-manage-projects", - element: , - }, - { - path: "/admin-manage-users", - element: , - }, - { - path: "/admin-menu", - element: , - }, - { - path: "/admin-project-add-member", - element: , - }, - { - path: "/admin-project-change-user-role", - element: , - }, - { - path: "/admin-project-manage-members", - element: , - }, - { - path: "/admin-project-page", - element: , - }, - { - path: "/admin-project-statistics", - element: , - }, - { - path: "/admin-project-view-members", - element: , - }, - { - path: "/admin-view-user", - element: , - }, ]); // Semi-hacky way to get the root element From 9511d509ca65d231bfa923b2177eb9e05162a377 Mon Sep 17 00:00:00 2001 From: al8763be Date: Sun, 17 Mar 2024 15:31:15 +0100 Subject: [PATCH 5/5] API imports fixed --- frontend/src/API/API.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index a30151c..f1bef85 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -1,6 +1,10 @@ -import { NewProject, Project } from "../Types/Project"; -import { NewUser, User } from "../Types/Users"; -import { NewWeeklyReport } from "../Types/goTypes"; +import { + NewWeeklyReport, + NewUser, + User, + Project, + NewProject, +} from "../Types/goTypes"; // This type of pattern should be hard to misuse interface APIResponse {