diff --git a/frontend/src/lib/@api/candidate.ts b/frontend/src/lib/@api/candidate.ts index 1c679b3..d0b2287 100644 --- a/frontend/src/lib/@api/candidate.ts +++ b/frontend/src/lib/@api/candidate.ts @@ -3,48 +3,59 @@ import type { CandidateData, CandidateLogin } from '$lib/stores/candidate'; import type { SubmissionProgress } from '$lib/stores/portfolio'; import { API_URL, errorHandler, type Fetch } from '.'; - // SSR Compatible export const apiLogout = async (fetchSsr?: Fetch) => { try { - fetchSsr ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) : await axios.post(API_URL + '/candidate/logout', { withCredentials: true }); + fetchSsr + ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) + : await axios.post(API_URL + '/candidate/logout', { withCredentials: true }); } catch (e: any) { throw errorHandler(e, 'Logout failed'); } -} +}; // SSR Compatible -export const apiFetchDetails = async (fetchSsr?: Fetch): Promise => { +export const apiFetchDetails = async (fetchSsr?: Fetch): Promise => { try { if (fetchSsr) { - const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' }); - const body = await res.text(); - console.log(body); + const res = await fetchSsr(API_URL + '/candidate/details', { + method: 'GET', + credentials: 'include' + }); if (res.status != 200) { - return null; + throw new Error(await res.text()); } - return JSON.parse(body); + return await res.json(); } const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true }); return res.data; } catch (e: any) { + console.log(e); throw errorHandler(e, 'Failed to fill details'); } -} +}; // SSR Compatible export const apiFetchSubmissionProgress = async (fetchSsr?: Fetch): Promise => { try { if (fetchSsr) { - const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', { method: "GET", credentials: 'include' }); + const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', { + method: 'GET', + credentials: 'include' + }); + if (res.status != 200) { + throw Error(await res.text()); + } return await res.json(); } - const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', { withCredentials: true }); + const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', { + withCredentials: true + }); return res.data; } catch (e: any) { throw errorHandler(e, 'Failed to fetch submission progress'); } -} +}; export const apiWhoami = async (): Promise => { try { @@ -53,7 +64,7 @@ export const apiWhoami = async (): Promise => { } catch (e: any) { throw errorHandler(e, 'Whoami failed'); } -} +}; export const apiLogin = async (data: CandidateLogin): Promise => { try { @@ -62,8 +73,7 @@ export const apiLogin = async (data: CandidateLogin): Promise => { } catch (e: any) { throw errorHandler(e, 'Login failed'); } -} - +}; export const apiFillDetails = async (data: CandidateData): Promise => { console.log(data); @@ -73,7 +83,7 @@ export const apiFillDetails = async (data: CandidateData): Promise { - const details = await apiFetchDetails(fetch); - - if (details === null) { + let details; + + try { + details = await apiFetchDetails(fetch); + } catch { throw redirect(302, '/register'); } - const submissionProgress = await apiFetchSubmissionProgress(fetch); + try { + await apiFetchSubmissionProgress(fetch); + } catch { + // TODO: + } return { candidate: { name: details.name, surname: details.surname, email: details.email - }, - submission: { - ...submissionProgress } + /*submission: { + ...submissionProgress + }*/ }; };