diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 248ad37..a30151c 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,20 @@ 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>; + /** Login */ + login(NewUser: NewUser): Promise>; } // Export an instance of the API @@ -117,4 +130,86 @@ 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", + }); + } + }, + + async submitWeeklyReport( + 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: Project) => { + return { success: true, data }; + }); + } catch (e) { + 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.ok) { + return { success: false, message: "Failed to login" }; + } else { + const data = (await response.json()) as JSON; + return { success: true, data }; + } + } catch (e) { + return Promise.resolve({ success: false, message: "Failed to login" }); + } + }, }; diff --git a/frontend/src/Components/Register.tsx b/frontend/src/Components/Register.tsx index e4a3ba0..0c0fcd0 100644 --- a/frontend/src/Components/Register.tsx +++ b/frontend/src/Components/Register.tsx @@ -4,6 +4,32 @@ import { api } from "../API/API"; import Logo from "../assets/Logo.svg"; import Button from "./Button"; +function InputField(props: { + label: string; + type: string; + value: string; + onChange: (e: React.ChangeEvent) => void; +}): JSX.Element { + return ( +
+ + +
+ ); +} + export default function Register(): JSX.Element { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); @@ -14,7 +40,7 @@ export default function Register(): JSX.Element { }; return ( -
+
Register New User -
- - { - setUsername(e.target.value); - }} - /> -
-
- - { - setPassword(e.target.value); - }} - /> -
+ { + setUsername(e.target.value); + }} + /> + { + setPassword(e.target.value); + }} + />
+
); } - -export default NewTimeReport; diff --git a/frontend/src/Pages/AdminPages/AdminAddUser.tsx b/frontend/src/Pages/AdminPages/AdminAddUser.tsx index 5e8c01f..c0f9492 100644 --- a/frontend/src/Pages/AdminPages/AdminAddUser.tsx +++ b/frontend/src/Pages/AdminPages/AdminAddUser.tsx @@ -1,18 +1,16 @@ import BasicWindow from "../../Components/BasicWindow"; import Button from "../../Components/Button"; +import Register from "../../Components/Register"; function AdminAddUser(): JSX.Element { - const content = <>; + const content = ( + <> + + + ); const buttons = ( <> -