From 6df5f554f58f55987e1608730fd3936f86f21bc8 Mon Sep 17 00:00:00 2001 From: anajoyceamorim Date: Wed, 26 Jun 2024 13:09:57 -0300 Subject: [PATCH] tratamento de erro(login) --- projeto/client/actions/cadastro.ts | 15 +++++++++++++++ projeto/client/actions/login.ts | 15 +++++++++++++++ .../client/components/auth/cadastro-form.tsx | 9 +++++++-- projeto/client/components/auth/header.tsx | 2 +- projeto/client/components/auth/login-form.tsx | 17 ++++++++++++++++- 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 projeto/client/actions/cadastro.ts create mode 100644 projeto/client/actions/login.ts diff --git a/projeto/client/actions/cadastro.ts b/projeto/client/actions/cadastro.ts new file mode 100644 index 0000000..8d7c053 --- /dev/null +++ b/projeto/client/actions/cadastro.ts @@ -0,0 +1,15 @@ +"use server"; + +import * as z from "zod"; + +import { CadastroSchema } from "@/schemas"; + +export const register = async (values: z.infer) => { + const validatedFields = CadastroSchema.safeParse(values); + + if(!validatedFields.success) { + return { error: "Campos inválidos!" }; + } + + return { success: "Bem vindo!" }; +}; diff --git a/projeto/client/actions/login.ts b/projeto/client/actions/login.ts new file mode 100644 index 0000000..87e0714 --- /dev/null +++ b/projeto/client/actions/login.ts @@ -0,0 +1,15 @@ +"use server"; + +import * as z from "zod"; + +import { LoginSchema } from "@/schemas"; + +export const login = async (values: z.infer) => { + const validatedFields = LoginSchema.safeParse(values); + + if(!validatedFields.success) { + return { error: "Campos inválidos!" }; + } + + return { success: "Bem vindo!" }; +}; \ No newline at end of file diff --git a/projeto/client/components/auth/cadastro-form.tsx b/projeto/client/components/auth/cadastro-form.tsx index e12e662..162c6d9 100644 --- a/projeto/client/components/auth/cadastro-form.tsx +++ b/projeto/client/components/auth/cadastro-form.tsx @@ -2,6 +2,7 @@ import * as z from "zod"; +import { useState, useTransition } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; @@ -15,13 +16,13 @@ import { FormLabel, FormMessage, } from "@/components/ui/form"; -import React from "react"; + import Select from "react-select"; import { CardWrapper } from "./card-register" import { Button } from "@/components/ui/button"; import { FormError } from "@/components/form-error"; import { FormSuccess } from "@/components/form-sucess"; -import { Label } from "../ui/label"; +import { cadastro } from "@/actions/cadastro"; const TimeSelect = [ { value: "America_Mineiro", label: "America Mineiro" }, @@ -49,6 +50,10 @@ const TimeSelect = [ export const CadastroForm = () => { + const [error, setError] = useState(""); + const [success, setSuccess] = useState(""); + const [isPending, startTransition] = useTransition(); + const form = useForm>({ resolver: zodResolver(CadastroSchema), defaultValues: { diff --git a/projeto/client/components/auth/header.tsx b/projeto/client/components/auth/header.tsx index 2da001e..6e75f05 100644 --- a/projeto/client/components/auth/header.tsx +++ b/projeto/client/components/auth/header.tsx @@ -18,7 +18,7 @@ export const Header = ({
Login Icon

- Onde é o Jogo? + Onde é o Jogo?

{label} diff --git a/projeto/client/components/auth/login-form.tsx b/projeto/client/components/auth/login-form.tsx index 50ad647..02b4d8a 100644 --- a/projeto/client/components/auth/login-form.tsx +++ b/projeto/client/components/auth/login-form.tsx @@ -2,6 +2,7 @@ import * as z from "zod"; +import { useState, useTransition } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; @@ -20,8 +21,13 @@ import { CardWrapper } from "./card-wrapper"; import { Button } from "@/components/ui/button"; import { FormError } from "@/components/form-error"; import { FormSuccess } from "@/components/form-sucess"; +import { login } from "@/actions/login"; export const LoginForm = () => { + const [error, setError] = useState(""); + const [success, setSuccess] = useState(""); + const [isPending, startTransition] = useTransition(); + const form = useForm>({ resolver: zodResolver(LoginSchema), defaultValues: { @@ -31,7 +37,16 @@ export const LoginForm = () => { }); const onSubmit = (values: z.infer) => { - console.log(values); + setError(""); + setSuccess(""); + + startTransition(() => { + login(values) + .then((data) => { + setError(data.error); + setSuccess(data.success); + }) + }); } return (