@@ -2,6 +2,8 @@ import * as Dialog from "@radix-ui/react-dialog";
2
2
3
3
import { Controller , useForm } from "react-hook-form" ;
4
4
import { useAuth } from "./AuthContext" ;
5
+ import { DDI } from "@/utils/DDI" ;
6
+ import { useEffect } from "react" ;
5
7
6
8
export const LoginForm = ( ) => {
7
9
const { isLogged, openLogin, user, login, isOpen } = useAuth ( ) ;
@@ -10,22 +12,35 @@ export const LoginForm = () => {
10
12
control,
11
13
handleSubmit,
12
14
formState : { isSubmitting } ,
15
+ watch,
16
+ reset,
13
17
} = useForm ( {
14
18
defaultValues : {
15
19
whatsapp : "" ,
20
+ country : "+55" ,
16
21
} ,
17
22
} ) ;
18
23
19
24
const onSubmit = async ( data : any ) => {
20
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1500 ) ) ;
21
- login ( { whatsapp : data . whatsapp } ) ;
25
+ try {
26
+ const whatsapp = [ data . country , data . whatsapp ] . join ( "" ) ;
27
+ login ( { whatsapp } ) ;
28
+ } catch ( error ) {
29
+ console . log ( error ) ;
30
+ }
22
31
} ;
32
+
33
+ useEffect ( ( ) => {
34
+ if ( ! isOpen ) {
35
+ reset ( ) ;
36
+ }
37
+ } , [ isOpen , reset ] ) ;
23
38
return (
24
39
< Dialog . Root
25
40
open = { isOpen }
26
41
onOpenChange = { ( state ) => {
27
42
if ( ! isSubmitting ) {
28
- openLogin ( state ) ;
43
+ openLogin ( { status : state } ) ;
29
44
}
30
45
} }
31
46
>
@@ -38,32 +53,68 @@ export const LoginForm = () => {
38
53
>
39
54
< form noValidate onSubmit = { handleSubmit ( onSubmit ) } >
40
55
< Dialog . Title className = "text-zinc-900 m-0 text-[17px] font-medium" >
41
- Acesso ao Sistema
56
+ Bem-vindo ao nosso gerador
42
57
</ Dialog . Title >
43
58
< Dialog . Description className = "text-zinc-600 mt-[10px] mb-5 text-[15px] leading-normal" >
44
- Bem-vindo ao nosso sistema! Para proporcionar uma experiência
45
- personalizada e segura, solicitamos seu nome e número de contato.
59
+ Para proporcionar uma experiência personalizada e segura,
60
+ solicitamos seu número de whatsapp{ " " }
61
+ < strong > apenas para te enviar a figurinha</ strong > .
46
62
</ Dialog . Description >
47
63
48
64
< Controller
49
65
control = { control }
50
66
name = "whatsapp"
51
- render = { ( props ) => (
52
- < fieldset className = "mb-[15px] flex items-center gap-5" >
53
- < label
54
- className = "text-violet11 w-[90px] text-right text-[15px]"
55
- htmlFor = { props . field . name }
56
- >
57
- Whatsapp
58
- </ label >
59
- < input
60
- id = { props . field . name }
61
- className = "text-violet11 shadow-violet7 focus:shadow-violet8 inline-flex h-[35px] w-full flex-1 items-center justify-center rounded-[4px] px-[10px] text-[15px] leading-none shadow-[0_0_0_1px] outline-none focus:shadow-[0_0_0_2px]"
62
- disabled = { ! user ?. isAuthenticated && isLogged }
63
- { ...props . field }
64
- />
65
- </ fieldset >
66
- ) }
67
+ render = { ( props ) => {
68
+ const country = watch ( "country" ) ;
69
+ const placeholder = DDI . find ( ( ddi ) => ddi . ddi === country ) ;
70
+ return (
71
+ < div >
72
+ < label
73
+ className = "block text-sm font-medium leading-6 text-gray-900"
74
+ htmlFor = { props . field . name }
75
+ >
76
+ Whatsapp
77
+ </ label >
78
+ < div className = "relative mt-2 rounded-md shadow-sm" >
79
+ < Controller
80
+ control = { control }
81
+ name = "country"
82
+ render = { ( selectProps ) => {
83
+ return (
84
+ < div className = "absolute inset-y-0 left-0 flex items-center" >
85
+ < label htmlFor = "country" className = "sr-only" >
86
+ Country
87
+ </ label >
88
+ < select
89
+ id = "country"
90
+ autoComplete = "country"
91
+ className = "h-full rounded-md border-0 bg-transparent py-0 pl-3 pr-7 text-gray-500 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm"
92
+ disabled = { ! user ?. isAuthenticated && isLogged }
93
+ { ...selectProps . field }
94
+ >
95
+ { DDI . map ( ( { ddi, sigla } ) => (
96
+ < option value = { ddi } key = { ddi } >
97
+ { sigla }
98
+ </ option >
99
+ ) ) }
100
+ </ select >
101
+ </ div >
102
+ ) ;
103
+ } }
104
+ />
105
+
106
+ < input
107
+ type = "text"
108
+ id = { props . field . name }
109
+ className = "block w-full rounded-md border-0 py-1.5 pl-20 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
110
+ placeholder = { placeholder ?. placeholder }
111
+ disabled = { ! user ?. isAuthenticated && isLogged }
112
+ { ...props . field }
113
+ />
114
+ </ div >
115
+ </ div >
116
+ ) ;
117
+ } }
67
118
/>
68
119
69
120
< span className = "text-xs text-zinc-400 leading-none" >
@@ -76,7 +127,7 @@ export const LoginForm = () => {
76
127
data-loading = { isSubmitting }
77
128
type = "button"
78
129
className = "inline-flex h-[35px] border items-center justify-center rounded-[4px] px-[15px] font-medium leading-none focus:shadow-[0_0_0_2px] focus:outline-none data-[loading=true]:cursor-not-allowed"
79
- onClick = { ( ) => openLogin ( false ) }
130
+ onClick = { ( ) => openLogin ( { status : false } ) }
80
131
disabled = { isSubmitting }
81
132
>
82
133
Voltar
@@ -87,7 +138,7 @@ export const LoginForm = () => {
87
138
className = "bg-indigo-600 text-white hover:bg-indigo-400 focus:shadow-green7 inline-flex h-[35px] items-center justify-center rounded-[4px] px-[15px] font-medium leading-none focus:shadow-[0_0_0_2px] focus:outline-none data-[loading=true]:cursor-not-allowed"
88
139
disabled = { isSubmitting }
89
140
>
90
- { isSubmitting ? "Enviando...." : "Cadastrar " }
141
+ { isSubmitting ? "Enviando...." : "Próximo " }
91
142
</ button >
92
143
</ div >
93
144
< Dialog . Close asChild >
0 commit comments