Merge branch 'frontend' into gruppDM

This commit is contained in:
Davenludd 2024-03-17 15:36:41 +01:00
commit c708ec47ca
4 changed files with 32 additions and 38 deletions

View file

@ -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<T> {
@ -33,6 +37,8 @@ interface API {
username: string,
token: string,
): Promise<APIResponse<Project[]>>;
/** Login */
login(NewUser: NewUser): Promise<APIResponse<JSON>>;
}
// Export an instance of the API
@ -156,7 +162,7 @@ export const api: API = {
}
},
submitWeeklyReport: function (
async submitWeeklyReport(
project: NewWeeklyReport,
token: string,
): Promise<APIResponse<Project>> {
@ -179,7 +185,7 @@ export const api: API = {
return response.json();
}
})
.then((data) => {
.then((data: Project) => {
return { success: true, data };
});
} catch (e) {
@ -189,4 +195,25 @@ export const api: API = {
});
}
},
async login(NewUser: NewUser): Promise<APIResponse<JSON>> {
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" });
}
},
};

View file

@ -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;
}

View file

@ -1,9 +0,0 @@
export interface TimeReport {
week: string;
development: string;
meeting: string;
administration: string;
ownwork: string;
studies: string;
testing: string;
}

View file

@ -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;
}