From 391a48a45e85a6c03a251320aaa6f38cf4803a2e Mon Sep 17 00:00:00 2001 From: EETagent Date: Tue, 6 Dec 2022 19:09:42 +0100 Subject: [PATCH] feat: add types for form errors --- .../(authenticated)/register/+page.svelte | 145 +++++++++++------- 1 file changed, 86 insertions(+), 59 deletions(-) diff --git a/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte b/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte index b9216f7..7d1d74d 100644 --- a/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte +++ b/frontend/src/routes/(candidate)/(authenticated)/register/+page.svelte @@ -34,7 +34,7 @@ address: '', citizenship: '', personalIdNumber: '', - study: '' + study: '', }, parents: [ { @@ -42,47 +42,52 @@ surname: '', email: '', telephone: '' - }, + } ] }; + const formValidationSchema = yup.object().shape({ + candidate: 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}$/), + birthplace: yup.string().required(), + birthdate: yup + .string() + .required() + .matches(/^([0-3]?[0-9])\.([1-9]|1[0-2])\.[0-9]{4}$/), + sex: yup.string(), + address: yup.string().required(), + citizenship: yup.string().required(), + personalIdNumber: yup.string().required(), + study: yup.string().required() + }), + parents: yup + .array() + .of( + yup.object().shape({ + name: yup.string().required(), + surname: yup.string().required(), + email: yup.string().email().required(), + telephone: yup + .string() + .required() + .matches(/^\+\d{1,3} \d{3} \d{3} \d{3}$/) + }) + ) + .required() + }); + const { form, errors, handleSubmit, handleChange } = createForm({ initialValues: formInitialValues, - validationSchema: yup.object().shape({ - candidate: 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}$/), - birthplace: yup.string().required(), - birthdate: yup - .string() - .required() - .matches(/^([0-3]?[0-9])\.([1-9]|1[0-2])\.[0-9]{4}$/), - sex: yup.string(), - address: yup.string().required(), - citizenship: yup.string().required(), - personalIdNumber: yup.string().required(), - study: yup.string().required() - }).required(), - parents: yup.array().of( - yup.object().shape({ - name: yup.string().required(), - surname: yup.string().required(), - email: yup.string().email().required(), - telephone: yup - .string() - .required() - .matches(/^\+\d{1,3} \d{3} \d{3} \d{3}$/), - }) - ).required() - }), + validationSchema: formValidationSchema, onSubmit: async (values: CandidateData) => { - console.log("page count: " + pageIndex); + console.log('page count: ' + pageIndex); console.log(values.candidate); console.log(values.parents); console.log(values); @@ -92,10 +97,10 @@ // @ts-ignore // love javascript delete values.undefined; // convert birthdate from dd.mm.yyyy to yyyy-mm-dd - let birthdate_formttted = values.candidate.birthdate! - .split('.') + let birthdate_formttted = values.candidate + .birthdate!.split('.') .map((x) => x.padStart(2, '0')) - .reverse() + .reverse() .join('-'); values.candidate.birthdate = birthdate_formttted; @@ -109,34 +114,56 @@ } }); - $: typedErrors = errors as Writable; + type FormErrorType = { + [K in keyof typeof formInitialValues]: typeof formInitialValues[K] extends Record< + string, + unknown + > + ? { + [K2 in keyof typeof formInitialValues[K]]: string; + } + : typeof formInitialValues[K] extends Array> + ? Array<{ [K3 in keyof typeof formInitialValues[K][number]]: string }> + : string; + }; + + // TODO: https://github.com/tjinauyeung/svelte-forms-lib/issues/171!! (Zatím tenhle mega typ) + $: typedErrors = errors as unknown as Writable; const isPageInvalid = (): boolean => { switch (pageIndex) { case 0: - if ($typedErrors["candidate"]["name"] || $typedErrors["candidate"]["email"] || $typedErrors["candidate"]["telephone"]) { + if ( + $typedErrors['candidate']['name'] || + $typedErrors['candidate']['email'] || + $typedErrors['candidate']['telephone'] + ) { return true; } break; case 1: if ( - /* $typedErrors.birthdurname || */ $typedErrors["candidate"]["birthplace"] || - $typedErrors["candidate"]["birthdate"] /* || $typedErrors.sex */ + /* $typedErrors.birthdurname || */ $typedErrors['candidate']['birthplace'] || + $typedErrors['candidate']['birthdate'] /* || $typedErrors.sex */ ) { return true; } break; case 2: - if ($typedErrors["candidate"]["address"] || $typedErrors["parents"][0]["email"] || $typedErrors["parents"][0]["telephone"]) { + if ( + $typedErrors['candidate']['address'] || + $typedErrors['parents'][0]['email'] || + $typedErrors['parents'][0]['telephone'] + ) { return true; } break; case 3: if ( - $typedErrors["candidate"]["citizenship"] || - $typedErrors["candidate"]["personalIdNumber"] || - $typedErrors["candidate"]["study"] //|| + $typedErrors['candidate']['citizenship'] || + $typedErrors['candidate']['personalIdNumber'] || + $typedErrors['candidate']['study'] //|| // $typedErrors.applicationId ) { return true; @@ -164,7 +191,7 @@