Merge branch 'frontend' into gruppPP

This commit is contained in:
Peter KW 2024-03-17 11:02:44 +01:00
commit 3e73b11698
18 changed files with 484 additions and 130 deletions

View file

@ -9,7 +9,7 @@ module.exports = {
'plugin:react-hooks/recommended',
'plugin:prettier/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs', 'tailwind.config.js', 'postcss.config.js', 'jest.config.cjs'],
ignorePatterns: ['dist', '.eslintrc.cjs', 'tailwind.config.js', 'postcss.config.js', 'jest.config.cjs', 'goTypes.ts'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh', 'prettier'],
rules: {

View file

@ -1,57 +1,120 @@
import { NewProject, Project } from "../Types/Project";
import { NewUser, User } from "../Types/Users";
// This type of pattern should be hard to misuse
interface APIResponse<T> {
success: boolean;
message?: string;
data?: T;
}
// Note that all protected routes also require a token
// Defines all the methods that an instance of the API must implement
interface API {
/** Register a new user */
registerUser(user: NewUser): Promise<User>;
registerUser(user: NewUser): Promise<APIResponse<User>>;
/** Remove a user */
removeUser(username: string): Promise<User>;
removeUser(username: string, token: string): Promise<APIResponse<User>>;
/** Create a project */
createProject(project: NewProject): Promise<Project>;
createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>>;
/** Renew the token */
renewToken(token: string): Promise<string>;
renewToken(token: string): Promise<APIResponse<string>>;
}
// Export an instance of the API
export const api: API = {
async registerUser(user: NewUser): Promise<User> {
return fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
}).then((res) => res.json() as Promise<User>);
async registerUser(user: NewUser): Promise<APIResponse<User>> {
try {
const response = await fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
});
if (!response.ok) {
return { success: false, message: "Failed to register user" };
} else {
const data = (await response.json()) as User;
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to register user" };
}
},
async removeUser(username: string): Promise<User> {
return fetch("/api/userdelete", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(username),
}).then((res) => res.json() as Promise<User>);
async removeUser(
username: string,
token: string,
): Promise<APIResponse<User>> {
try {
const response = await fetch("/api/userdelete", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(username),
});
if (!response.ok) {
return { success: false, message: "Failed to remove user" };
} else {
const data = (await response.json()) as User;
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to remove user" };
}
},
async createProject(project: NewProject): Promise<Project> {
return fetch("/api/project", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(project),
}).then((res) => res.json() as Promise<Project>);
async createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>> {
try {
const response = await fetch("/api/project", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(project),
});
if (!response.ok) {
return { success: false, message: "Failed to create project" };
} else {
const data = (await response.json()) as Project;
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to create project" };
}
},
async renewToken(token: string): Promise<string> {
return fetch("/api/loginrenew", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
}).then((res) => res.json() as Promise<string>);
async renewToken(token: string): Promise<APIResponse<string>> {
try {
const response = await fetch("/api/loginrenew", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
if (!response.ok) {
return { success: false, message: "Failed to renew token" };
} else {
const data = (await response.json()) as string;
return { success: true, data };
}
} catch (e) {
return { success: false, message: "Failed to renew token" };
}
},
};

View file

@ -0,0 +1,88 @@
// Code generated by tygo. DO NOT EDIT.
//////////
// source: WeeklyReport.go
/**
* This is what should be submitted to the server, the username will be derived from the JWT token
*/
export interface NewWeeklyReport {
/**
* The name of the project, as it appears in the database
*/
projectName: string;
/**
* The week number
*/
week: number /* int */;
/**
* Total time spent on development
*/
developmentTime: number /* int */;
/**
* Total time spent in meetings
*/
meetingTime: number /* int */;
/**
* Total time spent on administrative tasks
*/
adminTime: number /* int */;
/**
* Total time spent on personal projects
*/
ownWorkTime: number /* int */;
/**
* Total time spent on studying
*/
studyTime: number /* int */;
/**
* Total time spent on testing
*/
testingTime: number /* int */;
}
//////////
// source: project.go
/**
* Project is a struct that holds the information about a project
*/
export interface Project {
id: number /* int */;
name: string;
description: string;
owner: string;
}
/**
* As it arrives from the client, Owner is derived from the JWT token
*/
export interface NewProject {
name: string;
description: string;
}
//////////
// source: users.go
/**
* User struct represents a user in the system
*/
export interface User {
userId: string;
username: string;
password: string;
}
/**
* Should be used when registering, for example
*/
export interface NewUser {
username: string;
password: string;
}
/**
* PublicUser represents a user that is safe to send over the API (no password)
*/
export interface PublicUser {
userId: string;
username: string;
}