From 0ec7868b83f95650869829f9eaaef6cf31afc701 Mon Sep 17 00:00:00 2001 From: EETagent Date: Sat, 3 Dec 2022 16:54:45 +0100 Subject: [PATCH] feat: fetch type & ssr submission progress --- frontend/src/lib/@api/candidate.ts | 29 +++++++++++-------- frontend/src/lib/@api/index.ts | 3 +- .../dashboard/+layout.server.ts | 7 ++++- .../(authenticated)/dashboard/+page.svelte | 17 ----------- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/frontend/src/lib/@api/candidate.ts b/frontend/src/lib/@api/candidate.ts index f478d47..ec678d0 100644 --- a/frontend/src/lib/@api/candidate.ts +++ b/frontend/src/lib/@api/candidate.ts @@ -1,11 +1,11 @@ import axios, { type AxiosProgressEvent } from 'axios'; import type { CandidateData, CandidateLogin } from '$lib/stores/candidate'; import type { SubmissionProgress } from '$lib/stores/portfolio'; -import { API_URL, errorHandler } from '.'; +import { API_URL, errorHandler, type Fetch } from '.'; // SSR Compatible -export const apiLogout = async (fetchSsr?: any) => { +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 }); } catch (e: any) { @@ -14,7 +14,7 @@ export const apiLogout = async (fetchSsr?: any) => { } // SSR Compatible -export const apiFetchDetails = async (fetchSsr?: any): Promise => { +export const apiFetchDetails = async (fetchSsr?: Fetch): Promise => { try { if (fetchSsr) { const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' }); @@ -32,6 +32,20 @@ export const apiFetchDetails = async (fetchSsr?: any): Promise => { + try { + if (fetchSsr) { + const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', { method: "GET", credentials: 'include' }); + return await res.json(); + } + 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 { const res = await axios.get(`${API_URL}/whoami`); @@ -61,15 +75,6 @@ export const apiFillDetails = async (data: CandidateData): Promise => { - try { - 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 apiUploadCoverLetter = async ( letter: File, progressReporter: (progress: AxiosProgressEvent) => void diff --git a/frontend/src/lib/@api/index.ts b/frontend/src/lib/@api/index.ts index 2b81ce6..e4c1242 100644 --- a/frontend/src/lib/@api/index.ts +++ b/frontend/src/lib/@api/index.ts @@ -1,6 +1,7 @@ -import { goto } from "$app/navigation"; import type { AxiosError } from "axios"; +export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise + export const API_URL = "http://localhost:9000"; export interface ApiError { diff --git a/frontend/src/routes/(candidate)/(authenticated)/dashboard/+layout.server.ts b/frontend/src/routes/(candidate)/(authenticated)/dashboard/+layout.server.ts index d50f32d..e9dc66b 100644 --- a/frontend/src/routes/(candidate)/(authenticated)/dashboard/+layout.server.ts +++ b/frontend/src/routes/(candidate)/(authenticated)/dashboard/+layout.server.ts @@ -1,4 +1,4 @@ -import { apiFetchDetails } from '$lib/@api/candidate'; +import { apiFetchDetails, apiFetchSubmissionProgress } from '$lib/@api/candidate'; import { redirect } from '@sveltejs/kit'; import type { LayoutServerLoad } from './$types'; @@ -9,11 +9,16 @@ export const load: LayoutServerLoad = async ({ fetch }) => { throw redirect(302, '/register'); } + const submissionProgress = await apiFetchSubmissionProgress(fetch); + return { candidate: { name: details.name, surname: details.surname, email: details.email + }, + submission: { + ...submissionProgress } }; }; diff --git a/frontend/src/routes/(candidate)/(authenticated)/dashboard/+page.svelte b/frontend/src/routes/(candidate)/(authenticated)/dashboard/+page.svelte index 78e747a..e2a522b 100644 --- a/frontend/src/routes/(candidate)/(authenticated)/dashboard/+page.svelte +++ b/frontend/src/routes/(candidate)/(authenticated)/dashboard/+page.svelte @@ -6,29 +6,12 @@ import DashboardUploadCard from '$lib/components/dashboard/DashboardUploadCard.svelte'; import DashboardInfoCard from '$lib/components/dashboard/DashboardInfoCard.svelte'; - import { candidateData, fetchDetails } from '$lib/stores/candidate'; import CoverLetterUploadCard from '$lib/components/dashboard/CoverLetterUploadCard.svelte'; import PortfolioLetterUploadCard from '$lib/components/dashboard/PortfolioLetterUploadCard.svelte'; import PortfolioZipUploadCard from '$lib/components/dashboard/PortfolioZipUploadCard.svelte'; - import { fetchSubmProgress } from '$lib/stores/portfolio'; import type { PageData } from './$types'; - export let data: PageData; - - fetchSubmProgress(); // TODO: move to a better place - - $: if ($candidateData.name === undefined) { - fetch(); - } - - const fetch = async () => { - try { - await fetchDetails(); - } catch { - console.error("error"); - } - }