system-praktyk-front/src/api/edition.ts
2020-11-07 23:33:23 +01:00

63 lines
2.0 KiB
TypeScript

import { axios } from "@/api/index";
import { Edition } from "@/data/edition";
import { prepare } from "@/routing";
import { EditionDTO, editionDtoTransformer, EditionTeaserDTO, editionTeaserDtoTransformer, programEntryDtoTransformer } from "@/api/dto/edition";
import { Subset } from "@/helpers";
import { InternshipProgramEntry } from "@/data";
const EDITIONS_ENDPOINT = "/editions";
const EDITION_INFO_ENDPOINT = "/editions/:key";
const EDITION_CURRENT_ENDPOINT = "/editions/current";
const EDITION_REGISTER_ENDPOINT = "/register";
const EDITION_LOGIN_ENDPOINT = "/access/loginEdition";
export async function available() {
try {
const response = await axios.get(EDITIONS_ENDPOINT);
return (response.data || []).map(editionTeaserDtoTransformer.transform);
} catch (e) {
return [];
}
}
export async function join(key: string): Promise<boolean> {
try {
await axios.post(EDITION_REGISTER_ENDPOINT, JSON.stringify(key), { headers: { "Content-Type": "application/json" } });
return true;
} catch (error) {
console.error(error);
return false;
}
}
export async function get(key: string): Promise<Subset<Edition> | null> {
if (!key) {
return null;
}
const response = await axios.get<EditionTeaserDTO>(prepare(EDITION_INFO_ENDPOINT, { key }));
const dto = response.data;
return editionTeaserDtoTransformer.transform(dto);
}
export async function current(): Promise<{
edition: Edition,
program: InternshipProgramEntry[],
}> {
const response = await axios.get<EditionDTO>(EDITION_CURRENT_ENDPOINT);
const dto = response.data;
return {
edition: editionDtoTransformer.transform(dto),
program: dto.availableSubjects.map(programEntryDtoTransformer.transform as any),
};
}
export async function login(key: string): Promise<string> {
const response = await axios.post<string>(EDITION_LOGIN_ENDPOINT, JSON.stringify(key), { headers: { "Content-Type": "application/json" } })
return response.data;
}