From cae9e387b9623a66bff8087640b54c9ba275c3e9 Mon Sep 17 00:00:00 2001 From: EETagent Date: Fri, 2 Dec 2022 15:42:24 +0100 Subject: [PATCH] feat: file upload preview --- frontend/src/@api/candidate.ts | 112 ++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 28 deletions(-) diff --git a/frontend/src/@api/candidate.ts b/frontend/src/@api/candidate.ts index 962fe74..72e997b 100644 --- a/frontend/src/@api/candidate.ts +++ b/frontend/src/@api/candidate.ts @@ -1,41 +1,97 @@ -import axios from "axios"; -import type { CandidateData, CandidateLogin } from "src/stores/candidate"; -import { API_URL, errorHandler } from "."; +import axios, { type AxiosProgressEvent } from 'axios'; +import type { CandidateData, CandidateLogin } from 'src/stores/candidate'; +import { API_URL, errorHandler } from '.'; +export async function apiWhoami(): Promise { + try { + const res = await axios.get(`${API_URL}/whoami`); + return res.data; + } catch (e) { + throw errorHandler(e, 'Whoami failed'); + } +} export async function apiLogin(data: CandidateLogin): Promise { - try { - const res = await axios.post(API_URL + '/candidate/login', data, {withCredentials: true}); - return data.applicationId; - } catch (e: any) { - throw errorHandler(e, "Login failed"); - } + try { + const res = await axios.post(API_URL + '/candidate/login', data, { withCredentials: true }); + return data.applicationId; + } catch (e: any) { + throw errorHandler(e, 'Login failed'); + } } // TODO export async function apiLogout() { - try { - await axios.post(API_URL + '/candidate/logout', {withCredentials: true}); - } catch (e: any) { - throw errorHandler(e, "Logout failed"); - } + try { + await axios.post(API_URL + '/candidate/logout', { withCredentials: true }); + } catch (e: any) { + throw errorHandler(e, 'Logout failed'); + } } export async function apiFillDetails(data: CandidateData): Promise { - console.log(data); - try { - const res = await axios.post(API_URL + '/candidate/details', data, {withCredentials: true}); - return res.data; - } catch (e: any) { - throw errorHandler(e, "Failed to fill details"); - } + console.log(data); + try { + const res = await axios.post(API_URL + '/candidate/details', data, { withCredentials: true }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Failed to fill details'); + } } export async function apiFetchDetails(): Promise { - try { - const res = await axios.get(API_URL + '/candidate/details', {withCredentials: true}); - return res.data; - } catch (e: any) { - throw errorHandler(e, "Failed to fill details"); - } -} \ No newline at end of file + try { + const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Failed to fill details'); + } +} + +export async function apiUploadCoverLetter( + letter: File, + progressReporter: (progress: AxiosProgressEvent) => void +): Promise { + try { + const res = await axios.post( + API_URL + '/candidate/cover_letter', + { letter: letter }, + { withCredentials: true, onUploadProgress: progressReporter } + ); + return res.data === 'true'; + } catch (e: any) { + throw errorHandler(e, 'Failed to upload cover letter'); + } +} + +export async function apiUploadPortfolioLetter( + letter: File, + progressReporter: (progress: AxiosProgressEvent) => void +): Promise { + try { + const res = await axios.post( + API_URL + '/candidate/portfolio_letter', + { letter: letter }, + { withCredentials: true, onUploadProgress: progressReporter } + ); + return res.data === 'true'; + } catch (e: any) { + throw errorHandler(e, 'Failed to upload portfolio letter'); + } +} + +export async function apiUploadPortfolioZip( + portfolio: File, + progressReporter: (progress: AxiosProgressEvent) => void +): Promise { + try { + const res = await axios.post( + API_URL + '/candidate/portfolio_zip', + { portfolio: portfolio }, + { withCredentials: true, onUploadProgress: progressReporter } + ); + return res.data === 'true'; + } catch (e: any) { + throw errorHandler(e, 'Failed to upload portfolio zip'); + } +}