system-praktyk-front/src/api/internship.ts
2020-11-12 20:16:09 +01:00

48 lines
1.4 KiB
TypeScript

import { InternshipInfoDTO, InternshipRegistrationUpdate, SubmissionState } from "@/api/dto/internship-registration";
import { axios } from "@/api/index";
import { Nullable } from "@/helpers";
const INTERNSHIP_REGISTRATION_ENDPOINT = '/internshipRegistration';
const INTERNSHIP_ENDPOINT = '/internship';
export type ValidationMessage = {
key: string;
parameters: { [name: string]: string },
}
export class ValidationError extends Error {
public readonly messages: ValidationMessage[];
constructor(messages: ValidationMessage[], message: string = "There were validation errors.") {
super(message);
Object.setPrototypeOf(this, ValidationError.prototype);
this.messages = messages;
}
}
interface ApiError {
key: string;
parameters: { [name: string]: string },
}
interface UpdateResponse {
status: SubmissionState;
errors?: ApiError[];
}
export async function update(internship: Nullable<InternshipRegistrationUpdate>): Promise<SubmissionState> {
const response = (await axios.put<UpdateResponse>(INTERNSHIP_REGISTRATION_ENDPOINT, internship)).data;
if (response.status == SubmissionState.Draft) {
throw new ValidationError(response.errors || []);
}
return response.status;
}
export async function get(): Promise<InternshipInfoDTO> {
const response = await axios.get<InternshipInfoDTO>(INTERNSHIP_ENDPOINT);
return response.data;
}