@@ -38,10 +38,16 @@ import { motion } from 'framer-motion'
3838import { Eye , EyeOff } from 'lucide-react'
3939import { useState } from 'react'
4040import { containerVariants , cardVariants } from '@/shared/utils/animations'
41+ import { useCreateUserMutation } from '@/shared/hooks/use-user'
42+ import { AxiosError } from 'axios'
43+ import { useNavigate } from 'react-router-dom'
4144
4245export function SignUpPage ( ) {
4346 const [ showPassword , setShowPassword ] = useState ( false )
4447 const [ showConfirmPassword , setShowConfirmPassword ] = useState ( false )
48+ const navigate = useNavigate ( )
49+
50+ const { mutateAsync : createUser , isPending } = useCreateUserMutation ( )
4551
4652 const form = useForm < SignUpFormData > ( {
4753 resolver : zodResolver ( signUpFormSchema ) ,
@@ -73,9 +79,39 @@ export function SignUpPage() {
7379 onChange ( formattedValue )
7480 }
7581
76- function onSubmit ( values : SignUpFormData ) {
77- console . log ( values )
78- toast . success ( 'Conta criada com sucesso!' )
82+ async function onSubmit ( values : SignUpFormData ) {
83+ try {
84+ const isIndividual = values . documentType === 'individual'
85+
86+ // Convert birthDate to seconds since epoch if provided
87+ const birthDateSeconds = values . birthDate
88+ ? Math . floor ( values . birthDate . getTime ( ) / 1000 )
89+ : undefined
90+
91+ await createUser ( {
92+ name : values . name ,
93+ email : values . email ,
94+ password : values . password ,
95+ cellphone : values . phone ,
96+ personType : isIndividual ? 'PF' : 'PJ' ,
97+ cpfCnpj : values . document . replace ( / \D / g, '' ) ,
98+ birthDate : birthDateSeconds ,
99+ plan : 'Bronze'
100+ } )
101+
102+ form . reset ( )
103+ toast . success (
104+ 'Conta criada! Confirme seu cadastro pelo link enviado para o seu e-mail. Você será redirecionado para a página de login.' ,
105+ { duration : 10000 }
106+ )
107+ setTimeout ( ( ) => {
108+ navigate ( '/login' , { replace : true } )
109+ } , 10000 )
110+ } catch ( err ) {
111+ const errorMessage =
112+ ( ( err as AxiosError ) . response ?. data as string ) || 'Erro ao fazer login'
113+ toast . error ( errorMessage )
114+ }
79115 }
80116
81117 return (
@@ -326,7 +362,9 @@ export function SignUpPage() {
326362 />
327363 </ CardContent >
328364 < CardFooter className = "justify-center" >
329- < Button type = "submit" > Criar conta</ Button >
365+ < Button type = "submit" disabled = { isPending } >
366+ { isPending ? 'Criando...' : 'Criar conta' }
367+ </ Button >
330368 </ CardFooter >
331369 </ Card >
332370 </ motion . div >
0 commit comments