mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-08 01:00:16 +00:00
feat: fetch type & ssr submission progress
This commit is contained in:
parent
71f1e6b0b0
commit
0ec7868b83
4 changed files with 25 additions and 31 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
import axios, { type AxiosProgressEvent } from 'axios';
|
import axios, { type AxiosProgressEvent } from 'axios';
|
||||||
import type { CandidateData, CandidateLogin } from '$lib/stores/candidate';
|
import type { CandidateData, CandidateLogin } from '$lib/stores/candidate';
|
||||||
import type { SubmissionProgress } from '$lib/stores/portfolio';
|
import type { SubmissionProgress } from '$lib/stores/portfolio';
|
||||||
import { API_URL, errorHandler } from '.';
|
import { API_URL, errorHandler, type Fetch } from '.';
|
||||||
|
|
||||||
|
|
||||||
// SSR Compatible
|
// SSR Compatible
|
||||||
export const apiLogout = async (fetchSsr?: any) => {
|
export const apiLogout = async (fetchSsr?: Fetch) => {
|
||||||
try {
|
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) {
|
} catch (e: any) {
|
||||||
|
|
@ -14,7 +14,7 @@ export const apiLogout = async (fetchSsr?: any) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSR Compatible
|
// SSR Compatible
|
||||||
export const apiFetchDetails = async (fetchSsr?: any): Promise<CandidateData | null> => {
|
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData | null> => {
|
||||||
try {
|
try {
|
||||||
if (fetchSsr) {
|
if (fetchSsr) {
|
||||||
const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' });
|
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> => {
|
export const apiWhoami = async (): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(`${API_URL}/whoami`);
|
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 (
|
export const apiUploadCoverLetter = async (
|
||||||
letter: File,
|
letter: File,
|
||||||
progressReporter: (progress: AxiosProgressEvent) => void
|
progressReporter: (progress: AxiosProgressEvent) => void
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { goto } from "$app/navigation";
|
|
||||||
import type { AxiosError } from "axios";
|
import type { AxiosError } from "axios";
|
||||||
|
|
||||||
|
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
|
||||||
|
|
||||||
export const API_URL = "http://localhost:9000";
|
export const API_URL = "http://localhost:9000";
|
||||||
|
|
||||||
export interface ApiError {
|
export interface ApiError {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { apiFetchDetails } from '$lib/@api/candidate';
|
import { apiFetchDetails, apiFetchSubmissionProgress } from '$lib/@api/candidate';
|
||||||
import { redirect } from '@sveltejs/kit';
|
import { redirect } from '@sveltejs/kit';
|
||||||
import type { LayoutServerLoad } from './$types';
|
import type { LayoutServerLoad } from './$types';
|
||||||
|
|
||||||
|
|
@ -9,11 +9,16 @@ export const load: LayoutServerLoad = async ({ fetch }) => {
|
||||||
throw redirect(302, '/register');
|
throw redirect(302, '/register');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const submissionProgress = await apiFetchSubmissionProgress(fetch);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
candidate: {
|
candidate: {
|
||||||
name: details.name,
|
name: details.name,
|
||||||
surname: details.surname,
|
surname: details.surname,
|
||||||
email: details.email
|
email: details.email
|
||||||
|
},
|
||||||
|
submission: {
|
||||||
|
...submissionProgress
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,29 +6,12 @@
|
||||||
|
|
||||||
import DashboardUploadCard from '$lib/components/dashboard/DashboardUploadCard.svelte';
|
import DashboardUploadCard from '$lib/components/dashboard/DashboardUploadCard.svelte';
|
||||||
import DashboardInfoCard from '$lib/components/dashboard/DashboardInfoCard.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 CoverLetterUploadCard from '$lib/components/dashboard/CoverLetterUploadCard.svelte';
|
||||||
import PortfolioLetterUploadCard from '$lib/components/dashboard/PortfolioLetterUploadCard.svelte';
|
import PortfolioLetterUploadCard from '$lib/components/dashboard/PortfolioLetterUploadCard.svelte';
|
||||||
import PortfolioZipUploadCard from '$lib/components/dashboard/PortfolioZipUploadCard.svelte';
|
import PortfolioZipUploadCard from '$lib/components/dashboard/PortfolioZipUploadCard.svelte';
|
||||||
import { fetchSubmProgress } from '$lib/stores/portfolio';
|
|
||||||
import type { PageData } from './$types';
|
import type { PageData } from './$types';
|
||||||
|
|
||||||
|
|
||||||
export let data: PageData;
|
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>
|
</script>
|
||||||
|
|
||||||
<FullLayout>
|
<FullLayout>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue