diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 248ad37..ed64f71 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -1,5 +1,6 @@ import { NewProject, Project } from "../Types/Project"; import { NewUser, User } from "../Types/Users"; +import { NewWeeklyReport } from "../Types/goTypes"; // This type of pattern should be hard to misuse interface APIResponse { @@ -20,8 +21,13 @@ interface API { project: NewProject, token: string, ): Promise>; + /** Submit a weekly report */ + 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>; + } // Export an instance of the API @@ -49,7 +55,7 @@ export const api: API = { async removeUser( username: string, - token: string, + token: string ): Promise> { try { const response = await fetch("/api/userdelete", { @@ -74,7 +80,7 @@ export const api: API = { async createProject( project: NewProject, - token: string, + token: string ): Promise> { try { const response = await fetch("/api/project", { @@ -117,4 +123,51 @@ export const api: API = { return { success: false, message: "Failed to renew token" }; } }, + + async getUserProjects(token: string): Promise> { + try { + const response = await fetch("/api/getUserProjects", { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + }); + + if (!response.ok) { + 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" }); + } + }, + + submitWeeklyReport: function (project: NewWeeklyReport, token: string): Promise> { + try { + return fetch("/api/submitWeeklyReport", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + body: JSON.stringify(project), + }) + .then((response) => { + if (!response.ok) { + return { success: false, message: "Failed to submit weekly report" }; + } else { + return response.json(); + } + }) + .then((data) => { + return { success: true, data }; + }); + } catch (e) { + return Promise.resolve({ success: false, message: "Failed to submit weekly report" }); + } + } };