add routes
This commit is contained in:
@@ -3,8 +3,15 @@ import { useRequest } from 'ahooks'
|
||||
import { z } from 'zod'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { AlertCircleIcon } from 'lucide-react'
|
||||
import { Link } from 'react-router'
|
||||
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import {
|
||||
Alert,
|
||||
AlertDescription,
|
||||
AlertTitle
|
||||
} from '@repo/ui/components/ui/alert'
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
@@ -17,7 +24,7 @@ import { Input } from '@repo/ui/components/ui/input'
|
||||
import { Spinner } from '@repo/ui/components/ui/spinner'
|
||||
import { cpf, type RegisterContextProps, type User } from './data'
|
||||
import { RegisterContext } from './data'
|
||||
import { use } from 'react'
|
||||
import { use, useState } from 'react'
|
||||
|
||||
const formSchema = z.object({
|
||||
cpf: cpf
|
||||
@@ -27,10 +34,11 @@ type Schema = z.infer<typeof formSchema>
|
||||
|
||||
export function Cpf() {
|
||||
const { setUser } = use(RegisterContext) as RegisterContextProps
|
||||
const [isOnboarded, setIsOnboarded] = useState<Boolean>(false)
|
||||
const form = useForm({
|
||||
resolver: zodResolver(formSchema)
|
||||
})
|
||||
const { control, handleSubmit, formState } = form
|
||||
const { control, handleSubmit, formState, setError } = form
|
||||
const { runAsync } = useRequest(
|
||||
async ({ cpf }) => {
|
||||
return await fetch(`/lookup?cpf=${cpf}`, {
|
||||
@@ -43,8 +51,15 @@ export function Cpf() {
|
||||
|
||||
const onSubmit = async ({ cpf }: Schema) => {
|
||||
const r = await runAsync({ cpf })
|
||||
const user = (await r.json()) as any
|
||||
setUser({ cpf, ...user })
|
||||
const data = (await r.json()) as any
|
||||
|
||||
if (r.ok) {
|
||||
setUser({ cpf, ...data })
|
||||
}
|
||||
|
||||
if (r.status === 409) {
|
||||
setIsOnboarded(true)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -52,6 +67,27 @@ export function Cpf() {
|
||||
<Form {...form}>
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<fieldset disabled={formState.isSubmitting} className="grid gap-6">
|
||||
{isOnboarded && (
|
||||
<Alert variant="destructive">
|
||||
<AlertCircleIcon />
|
||||
<AlertTitle>Você já está cadastrado.</AlertTitle>
|
||||
<AlertDescription>
|
||||
<p>
|
||||
Por favor, siga as instruções abaixo para acessar sua conta:
|
||||
</p>
|
||||
<ul className="list-disc text-sm mt-2.5 [&_a]:underline [&_a]:hover:no-underline">
|
||||
<li>
|
||||
Você lembra da sua senha? <Link to="/">Fazer login</Link>
|
||||
</li>
|
||||
<li>
|
||||
Esqueceu a senha?{' '}
|
||||
<Link to="/forgot">Recuperar minha senha</Link>
|
||||
</li>
|
||||
</ul>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<FormField
|
||||
control={control}
|
||||
name="cpf"
|
||||
|
||||
Reference in New Issue
Block a user