API - SubmitWeeklyReport & getUserProjects added
This commit is contained in:
parent
d6d4eb3336
commit
8d0da111eb
1 changed files with 55 additions and 2 deletions
|
@ -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<T> {
|
||||
|
@ -20,8 +21,13 @@ interface API {
|
|||
project: NewProject,
|
||||
token: string,
|
||||
): Promise<APIResponse<Project>>;
|
||||
/** Submit a weekly report */
|
||||
submitWeeklyReport(project: NewWeeklyReport, token: string): Promise<APIResponse<Project>>;
|
||||
/** Renew the token */
|
||||
renewToken(token: string): Promise<APIResponse<string>>;
|
||||
/** Gets all the projects of a user*/
|
||||
getUserProjects(username: string, token: string): Promise<APIResponse<Project[]>>;
|
||||
|
||||
}
|
||||
|
||||
// Export an instance of the API
|
||||
|
@ -49,7 +55,7 @@ export const api: API = {
|
|||
|
||||
async removeUser(
|
||||
username: string,
|
||||
token: string,
|
||||
token: string
|
||||
): Promise<APIResponse<User>> {
|
||||
try {
|
||||
const response = await fetch("/api/userdelete", {
|
||||
|
@ -74,7 +80,7 @@ export const api: API = {
|
|||
|
||||
async createProject(
|
||||
project: NewProject,
|
||||
token: string,
|
||||
token: string
|
||||
): Promise<APIResponse<Project>> {
|
||||
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<APIResponse<Project[]>> {
|
||||
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<APIResponse<Project>> {
|
||||
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" });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue