94 lines
2.7 KiB
TypeScript
94 lines
2.7 KiB
TypeScript
import { Course, CourseAttendeeRole, CourseEventDetails, CourseEventSummary } from '@shared/types/course'
|
|
import { PagedData } from '@shared/types/pagination';
|
|
|
|
//@ts-ignore
|
|
const addr = import.meta.env.VITE_APIHOST;
|
|
|
|
export async function getTrainingReports(sortMode?: string, search?: string, page?: number, pageSize?: number): Promise<PagedData<CourseEventSummary>> {
|
|
const params = new URLSearchParams();
|
|
|
|
if (page !== undefined) {
|
|
params.set("page", page.toString());
|
|
}
|
|
|
|
if (pageSize !== undefined) {
|
|
params.set("pageSize", pageSize.toString());
|
|
}
|
|
|
|
if (sortMode !== undefined) {
|
|
params.set("sort", sortMode.toString());
|
|
}
|
|
|
|
if (search !== undefined || search !== "") {
|
|
params.set("search", search.toString());
|
|
}
|
|
|
|
const res = await fetch(`${addr}/courseEvent?${params}`, {
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (res.ok) {
|
|
return await res.json() as Promise<PagedData<CourseEventSummary>>;
|
|
} else {
|
|
console.error("Something went wrong");
|
|
throw new Error("Failed to load training reports");
|
|
}
|
|
}
|
|
|
|
export async function getTrainingReport(id: number): Promise<CourseEventDetails> {
|
|
const res = await fetch(`${addr}/courseEvent/${id}`, {
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (res.ok) {
|
|
return await res.json() as Promise<CourseEventDetails>;
|
|
} else {
|
|
console.error("Something went wrong");
|
|
throw new Error("Failed to load training reports");
|
|
}
|
|
}
|
|
|
|
export async function getAllTrainings(): Promise<Course[]> {
|
|
const res = await fetch(`${addr}/course`, {
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (res.ok) {
|
|
return await res.json() as Promise<Course[]>;
|
|
} else {
|
|
console.error("Something went wrong");
|
|
throw new Error("Failed to load training list");
|
|
}
|
|
}
|
|
|
|
export async function getAllAttendeeRoles(): Promise<CourseAttendeeRole[]> {
|
|
const res = await fetch(`${addr}/course/roles`, {
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (res.ok) {
|
|
return await res.json() as Promise<CourseAttendeeRole[]>;
|
|
} else {
|
|
console.error("Something went wrong");
|
|
throw new Error("Failed to load attendee roles");
|
|
}
|
|
}
|
|
|
|
export async function postTrainingReport(report: CourseEventDetails) {
|
|
const res = await fetch(`${addr}/courseEvent`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(report),
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const errorText = await res.text();
|
|
throw new Error(`Failed to post training report: ${res.status} ${errorText}`);
|
|
}
|
|
|
|
return res.json(); // expected to return the inserted report or new ID
|
|
}
|