From b48d20c68967114fd5fe0f38b18440ca7d6829aa Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Tue, 29 Nov 2022 12:34:13 +0100 Subject: [PATCH] feat: login, register PoC --- frontend/src/api/candidate.ts | 9 ++ frontend/src/routes/dashboard/+page.svelte | 30 ++++++- frontend/src/routes/register/+page.svelte | 95 +++++++++++++--------- frontend/src/stores/candidate.ts | 12 ++- 4 files changed, 101 insertions(+), 45 deletions(-) diff --git a/frontend/src/api/candidate.ts b/frontend/src/api/candidate.ts index 4608a46..275f312 100644 --- a/frontend/src/api/candidate.ts +++ b/frontend/src/api/candidate.ts @@ -29,4 +29,13 @@ export async function apiFillDetails(data: CandidateData): Promise { + try { + let 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 diff --git a/frontend/src/routes/dashboard/+page.svelte b/frontend/src/routes/dashboard/+page.svelte index 00b9b07..1a1201a 100644 --- a/frontend/src/routes/dashboard/+page.svelte +++ b/frontend/src/routes/dashboard/+page.svelte @@ -6,13 +6,35 @@ import DashboardUploadCard from '$lib/components/dashboard/DashboardUploadCard.svelte'; import DashboardInfoCard from '$lib/components/dashboard/DashboardInfoCard.svelte'; + import { candidateData, fetchDetails } from '../../stores/candidate'; + + + let name = ""; + let email = ""; + + $: if ($candidateData === undefined) { + fetch(); + } else { + name = $candidateData.name + " " + $candidateData.surname; + email = $candidateData.email; + } + + const fetch = async () => { + try { + await fetchDetails(); + name = $candidateData.name + " " + $candidateData.surname; + email = $candidateData.email; + } catch { + console.error("error"); + } + }
- - damina.vysin@example.com + + {email} Uchazeč na SSPŠ
@@ -28,8 +50,8 @@
- - damina.vysin@example.com + + {name} Uchazeč na SSPŠ
diff --git a/frontend/src/routes/register/+page.svelte b/frontend/src/routes/register/+page.svelte index 3b260ff..7c0ada9 100644 --- a/frontend/src/routes/register/+page.svelte +++ b/frontend/src/routes/register/+page.svelte @@ -10,9 +10,11 @@ import IdField from '$lib/components/textfield/IdField.svelte'; import TelephoneField from '$lib/components/textfield/TelephoneField.svelte'; import TextField from '$lib/components/textfield/TextField.svelte'; + import { fillDetails, candidateData, type CandidateData } from '../../stores/candidate'; import { createForm } from 'svelte-forms-lib'; import * as yup from 'yup'; + import { writable } from 'svelte/store'; let applicationValue = ''; @@ -20,49 +22,58 @@ let pageIndex = 0; let pagesFilled = 0; - const formInitialValues = { + const formInitialValues: CandidateData = { name: '', + surname: '', email: '', telephone: '', - birthSurname: '', - birthPlace: '', - birthDate: '', + birthplace: '', + birthdate: '', sex: '', address: '', - parentEmail: '', - parentTelephone: '', citizenship: '', - personalId: '', + personalIdNumber: '', study: '', - applicationId: '' + parentName: 'TODO name', + parentSurname: 'TODO', + parentTelephone: '', + parentEmail: '' }; - const { form, errors, state, handleChange, handleSubmit } = createForm({ + const { form, errors, handleSubmit, handleChange } = createForm({ initialValues: formInitialValues, validationSchema: yup.object().shape({ name: yup.string().required(), + surname: yup.string(), email: yup.string().email().required(), - telephone: yup - .string() - .required() - .matches(/^\+\d{1,3} \d{3} \d{3} \d{3}$/), - birthSurname: yup.string().required(), - birthPlace: yup.string().required(), - birthDate: yup.string().required(), - sex: yup.string().required(), + telephone: yup.string().required().matches(/^\+\d{1,3} \d{3} \d{3} \d{3}$/), + birthplace: yup.string().required(), + birthdate: yup.string().required(), + sex: yup.string(), address: yup.string().required(), - parentEmail: yup.string().email().required(), - parentTelephone: yup.string().required(), citizenship: yup.string().required(), - personalId: yup.string().required(), + personalIdNumber: yup.string().required(), study: yup.string().required(), - applicationId: yup.string().required() + parentName: yup.string(), + parentSurname: yup.string(), + parentTelephone: yup.string().required().matches(/^\+\d{1,3} \d{3} \d{3} \d{3}$/), + parentEmail: yup.string().email().required() }), - onSubmit: (values) => { - alert(JSON.stringify(values)); - } + + onSubmit: async (values) => { + console.log("submit") + // @ts-ignore // love javascript + delete values.undefined; + + values.birthdate = '2000-01-01' // TODO: reformat user typed date + + await fillDetails(values); + goto("/dashboard"); + }, }); + $: console.log($errors); + const isPageInvalid = (): boolean => { switch (pageIndex) { case 0: @@ -72,7 +83,7 @@ break; case 1: - if ($errors.birthSurname || $errors.birthPlace || $errors.birthDate || $errors.sex) { + if (/* $errors.birthdurname || */ $errors.birthplace || $errors.birthdate /* || $errors.sex */) { return true; } break; @@ -82,7 +93,12 @@ } break; case 3: - if ($errors.citizenship || $errors.personalId || $errors.study || $errors.applicationId) { + if ( + $errors.citizenship || + $errors.personalIdNumber || + $errors.study //|| + // $errors.applicationId + ) { return true; } break; @@ -141,19 +157,19 @@

- + /> --> -
+
{/if} {#if pageIndex === 2} @@ -234,7 +250,7 @@ placeholder="Občanství" />
- +
@@ -264,10 +280,9 @@ {/if} { - await handleSubmit(e); if (isPageInvalid()) return; if (pageIndex === pageCount) { - alert('should submit'); + await handleSubmit(e); } else { pagesFilled++; pageIndex++; diff --git a/frontend/src/stores/candidate.ts b/frontend/src/stores/candidate.ts index ed7f9f8..422eb8f 100644 --- a/frontend/src/stores/candidate.ts +++ b/frontend/src/stores/candidate.ts @@ -1,4 +1,4 @@ -import { apiFillDetails, apiLogin, apiLogout } from "../api/candidate"; +import { apiFetchDetails, apiFillDetails, apiLogin, apiLogout } from "../api/candidate"; import { writable } from "svelte/store"; export interface CandidateData { @@ -72,4 +72,14 @@ export async function fillDetails(data: CandidateData) { console.error(e); throw e; } +} + +export async function fetchDetails() { + try { + let res = await apiFetchDetails(); + candidateData.set(res); + } catch (e) { + console.error(e); + throw e; + } } \ No newline at end of file