import { PatternFormat } from 'react-number-format' 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, FormField, FormItem, FormLabel, FormMessage } from '@repo/ui/components/ui/form' 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, useState } from 'react' const formSchema = z.object({ cpf: cpf }) type Schema = z.infer export function Cpf() { const { setUser } = use(RegisterContext) as RegisterContextProps const [isOnboarded, setIsOnboarded] = useState(false) const form = useForm({ resolver: zodResolver(formSchema) }) const { control, handleSubmit, formState, setError } = form const { runAsync } = useRequest( async ({ cpf }) => { return await fetch(`/lookup?cpf=${cpf}`, { method: 'GET', headers: new Headers({ 'Content-Type': 'application/json' }) }) }, { manual: true } ) const onSubmit = async ({ cpf }: Schema) => { const r = await runAsync({ cpf }) const data = (await r.json()) as any if (r.ok) { setUser({ cpf, ...data }) } if (r.status === 409) { setIsOnboarded(true) } } return ( <>
{isOnboarded && ( Você já está cadastrado.

Por favor, siga as instruções abaixo para acessar sua conta:

  • Você lembra da sua senha? Fazer login
  • Esqueceu a senha?{' '} Recuperar minha senha
)} ( CPF { onChange(value) }} {...props} /> )} />
) }