feat: fetch type & ssr submission progress

This commit is contained in:
EETagent 2022-12-03 16:54:45 +01:00
parent 71f1e6b0b0
commit 0ec7868b83
4 changed files with 25 additions and 31 deletions

View file

@ -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<CandidateData | null> => {
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData | null> => {
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<CandidateData | n
}
}
// SSR Compatible
export const apiFetchSubmissionProgress = async (fetchSsr?: Fetch): Promise<SubmissionProgress> => {
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<string> => {
try {
const res = await axios.get(`${API_URL}/whoami`);
@ -61,15 +75,6 @@ export const apiFillDetails = async (data: CandidateData): Promise<CandidateData
}
}
export const apiFetchSubmissionProgress = async (): Promise<SubmissionProgress> => {
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

View file

@ -1,6 +1,7 @@
import { goto } from "$app/navigation";
import type { AxiosError } from "axios";
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
export const API_URL = "http://localhost:9000";
export interface ApiError {

View file

@ -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
}
};
};

View file

@ -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");
}
}
</script>
<FullLayout>