From 699555ae497c43316b3047ffa3afcf7980216b57 Mon Sep 17 00:00:00 2001 From: Anthony Mackensen Date: Thu, 25 Apr 2024 15:30:53 +0000 Subject: [PATCH 1/4] build: install zod --- package.json | 3 ++- yarn.lock | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f62ec9bc..31dcf27e 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "react-dom": "^18.2.0", "sanity": "^3.20.1", "styled-components": "^6.1.1", - "swiper": "^11.0.5" + "swiper": "^11.0.5", + "zod": "^3.23.4" }, "devDependencies": { "@storybook/addon-a11y": "^7.5.3", diff --git a/yarn.lock b/yarn.lock index 310b78d9..9fb9201c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14021,6 +14021,7 @@ __metadata: tailwindcss: ^3.3.5 tsc-files: ^1.1.4 typescript: ^5.3.2 + zod: ^3.23.4 languageName: unknown linkType: soft @@ -20156,3 +20157,10 @@ __metadata: checksum: 4a73da856738b0634700b52f4ab3fe0bf0a532bea6820ad962d0bda0163d2d5525df4859f89a7238e204a378384e12551985049790c1894c3ac191866e85887f languageName: node linkType: hard + +"zod@npm:^3.23.4": + version: 3.23.4 + resolution: "zod@npm:3.23.4" + checksum: 58f6e298c51d9ae01a1b1a1692ac7f00774b466d9a287a1ff8d61ff1fbe0ae9b0f050ae1cf1a8f71e4c6ccd0333a3cc340f339360fab5f5046cc954d10525a54 + languageName: node + linkType: hard From 98a0b580b051e4816de8ed3c25b969597fded000 Mon Sep 17 00:00:00 2001 From: Anthony Mackensen Date: Thu, 25 Apr 2024 15:31:51 +0000 Subject: [PATCH 2/4] feat: add loan-request form validation schemas --- .../form-schema-validations.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 components/LoanRequestForm/form-schema-validations.ts diff --git a/components/LoanRequestForm/form-schema-validations.ts b/components/LoanRequestForm/form-schema-validations.ts new file mode 100644 index 00000000..0a9df980 --- /dev/null +++ b/components/LoanRequestForm/form-schema-validations.ts @@ -0,0 +1,33 @@ +import { z } from "zod"; + +const customErrorMap: z.ZodErrorMap = (issue, ctx) => { + if (issue.code === z.ZodIssueCode.too_small) { + return { + message: "Este campo es requerido.", + }; + } + return { message: ctx.defaultError }; +}; + +z.setErrorMap(customErrorMap); + +export const loanReqFormSchema = z.object({ + fullname: z.string().min(1), + dni: z.string().min(1), + calle: z.string().min(1), + altura: z.string().min(1), + depto: z.string().min(1), + localidad: z.string().min(1), + postal: z.string().min(1), + area: z.string().min(1), + phone: z.string().min(1), + email: z.string().min(1).email(), + "child-link": z.string().min(1), + "child-fullname": z.string().min(1), + "child-dni": z.string().min(1), +}); + +export type LoanReqFormSchema = z.infer; +export type LoanReqFormSchemaErrors = z.inferFlattenedErrors< + typeof loanReqFormSchema +>; From fb4df4b2552c194aec0cdcadbb7b6002288d89b7 Mon Sep 17 00:00:00 2001 From: Anthony Mackensen Date: Thu, 25 Apr 2024 15:32:49 +0000 Subject: [PATCH 3/4] feat: add loan-request form action --- components/LoanRequestForm/form-action.ts | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 components/LoanRequestForm/form-action.ts diff --git a/components/LoanRequestForm/form-action.ts b/components/LoanRequestForm/form-action.ts new file mode 100644 index 00000000..b64c0197 --- /dev/null +++ b/components/LoanRequestForm/form-action.ts @@ -0,0 +1,25 @@ +"use server"; + +import { + loanReqFormSchema, + type LoanReqFormSchemaErrors, +} from "./form-schema-validations"; + +type ErrorMessages = LoanReqFormSchemaErrors["fieldErrors"]; + +export async function loanReqFormAction( + previousState: ErrorMessages, + formData: FormData +): Promise { + "use server"; + + const result = loanReqFormSchema.safeParse(Object.fromEntries(formData)); + + if (!result.success) { + return result.error.flatten().fieldErrors; + } + // TODO: Handle form data + result.data; + + return {}; +} From 61e3346dbac592ff4cac6ceca1a61f2b02c1f0bf Mon Sep 17 00:00:00 2001 From: Anthony Mackensen Date: Thu, 25 Apr 2024 16:08:45 +0000 Subject: [PATCH 4/4] feat: add loan-request form server side validations --- .../LoanRequestForm/LoanRequestForm.tsx | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/components/LoanRequestForm/LoanRequestForm.tsx b/components/LoanRequestForm/LoanRequestForm.tsx index 5e97d7d2..77066317 100644 --- a/components/LoanRequestForm/LoanRequestForm.tsx +++ b/components/LoanRequestForm/LoanRequestForm.tsx @@ -1,10 +1,16 @@ +"use client"; + import { Button } from "components/Button/Button"; import TextArea from "components/TextArea/TextArea"; import Input from "components/Input/Input"; import React from "react"; import { Link } from "components/Link/Link"; +import { useFormState } from "react-dom"; +import { loanReqFormAction } from "./form-action"; export const LoanRequestForm = () => { + const [errorMsg, formAction] = useFormState(loanReqFormAction, {}); + return (
@@ -20,7 +26,7 @@ export const LoanRequestForm = () => {

-
+

Paso 1 de 2:
Completá los datos del formulario. @@ -30,142 +36,152 @@ export const LoanRequestForm = () => { Datos del adulto responsable

Datos del niño o niña