1
+ import { useApolloClient , useMutation } from "@apollo/react-hooks" ;
1
2
import { NextPage } from "next" ;
2
3
import React , { useRef , useState } from "react" ;
3
4
import Button from "../components/button" ;
4
5
import { Container , Section } from "../components/global-style" ;
5
6
import Layout from "../components/layout" ;
6
7
import Input from "../components/input" ;
7
8
import { Column } from "../components/column" ;
9
+ import { withApollo } from "../lib/apollo" ;
10
+ import gql from "graphql-tag" ;
11
+
12
+ type User = {
13
+ id : string ;
14
+ email : string ;
15
+ role : Role ;
16
+ } ;
17
+
18
+ enum Role {
19
+ ADMINISTRATOR = "ADMINISTRATOR" ,
20
+ COLLABORATOR = "COLLABORATOR"
21
+ }
22
+
23
+ type UserInput = {
24
+ email : string ;
25
+ password : string ;
26
+ role : Role ;
27
+ } ;
8
28
9
29
const Signup : NextPage = ( ) => {
10
30
var emailRegex = / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " .+ " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ;
11
31
const email = useRef < HTMLInputElement > ( null ) ;
12
32
const pass = useRef < HTMLInputElement > ( null ) ;
13
33
const confirmpass = useRef < HTMLInputElement > ( null ) ;
14
34
const [ validEmail , setValidEmail ] = useState ( false ) ;
35
+ const [ passValue , setPassValue ] = useState ( "" ) ;
36
+ const [ emailValue , setEmailValue ] = useState ( "" ) ;
15
37
const [ validPass , setValidPass ] = useState ( false ) ;
38
+ const [ emailErrorMessage , setEmailError ] = useState ( "" ) ;
39
+ const [ passErrorMessage , setpassError ] = useState ( "" ) ;
40
+ const [ createUser ] = useMutation < { createUser : User } , { input : UserInput } > (
41
+ gql `
42
+ mutation CreateUser($input: UserInput!) {
43
+ createUser(input: $input) {
44
+ id
45
+ }
46
+ }
47
+ `
48
+ ) ;
16
49
17
50
return (
18
51
< >
@@ -23,7 +56,7 @@ const Signup: NextPage = () => {
23
56
< Column >
24
57
< div >
25
58
< Input
26
- onChange = { onEmailChange }
59
+ onChange = { validateEmail }
27
60
type = "text"
28
61
placeholder = "Email"
29
62
ref = { email }
@@ -34,7 +67,7 @@ const Signup: NextPage = () => {
34
67
</ div >
35
68
< div >
36
69
< Input
37
- onChange = { onPassChange }
70
+ onChange = { validatePass }
38
71
type = "password"
39
72
placeholder = "Senha"
40
73
ref = { pass }
@@ -45,7 +78,7 @@ const Signup: NextPage = () => {
45
78
</ div >
46
79
< div >
47
80
< Input
48
- onChange = { onPassChange }
81
+ onChange = { validatePass }
49
82
type = "password"
50
83
placeholder = "Confirmar senha"
51
84
ref = { confirmpass }
@@ -59,40 +92,86 @@ const Signup: NextPage = () => {
59
92
</ Button >
60
93
</ Column >
61
94
</ form >
95
+ { renderError ( ) }
62
96
</ Container >
63
97
</ Section >
64
98
</ Layout >
65
99
</ >
66
100
) ;
67
101
68
- function onEmailChange ( ) {
102
+ function renderError ( ) {
103
+ return (
104
+ < div >
105
+ < b > { emailErrorMessage } </ b >
106
+ < br > </ br >
107
+ < b > { passErrorMessage } </ b >
108
+ </ div >
109
+ ) ;
110
+ }
111
+
112
+ function validateEmail ( ) {
69
113
if ( email . current !== null ) {
70
114
if ( emailRegex . test ( String ( email . current . value ) . toLocaleLowerCase ( ) ) ) {
115
+ setEmailError ( "" ) ;
116
+ setEmailValue ( email . current . value ) ;
71
117
setValidEmail ( true ) ;
72
118
} else {
119
+ setEmailValue ( "" ) ;
120
+ setEmailError ( "Email inválido" ) ;
73
121
setValidEmail ( false ) ;
74
122
}
75
123
}
76
124
}
77
125
78
- function onPassChange ( ) {
79
- if ( confirmpass . current !== null && pass . current !== null ) {
80
- if (
81
- confirmpass . current . value === pass . current . value &&
82
- pass . current . value . length > 5
83
- ) {
126
+ function validatePass ( ) {
127
+ if (
128
+ confirmpass . current !== null &&
129
+ pass . current !== null &&
130
+ validateMinimumPassSize ( )
131
+ ) {
132
+ if ( confirmpass . current . value === pass . current . value ) {
133
+ setpassError ( "" ) ;
134
+ setPassValue ( confirmpass . current . value ) ;
84
135
setValidPass ( true ) ;
85
136
} else {
137
+ setPassValue ( "" ) ;
138
+ setpassError ( "As senhas não coincidem" ) ;
86
139
setValidPass ( false ) ;
87
140
}
88
141
}
89
142
}
90
- function sendForm ( event : React . MouseEvent < HTMLButtonElement > ) {
143
+
144
+ function validateMinimumPassSize ( ) {
145
+ if ( confirmpass . current !== null ) {
146
+ if ( confirmpass . current . value . length < 5 ) {
147
+ setpassError ( "A senha deve ter ao menos 6 caracteres" ) ;
148
+ return false ;
149
+ } else {
150
+ setpassError ( "" ) ;
151
+ return true ;
152
+ }
153
+ }
154
+ }
155
+ async function sendForm ( event : React . MouseEvent < HTMLButtonElement > ) {
91
156
event . preventDefault ( ) ;
157
+ validatePass ( ) ;
158
+ validateEmail ( ) ;
159
+ validateMinimumPassSize ( ) ;
160
+
92
161
if ( validEmail && validPass ) {
93
162
console . log ( "Pass" ) ;
163
+ const result = await createUser ( {
164
+ variables : {
165
+ input : {
166
+ email : emailValue ,
167
+ password : passValue ,
168
+ role : Role . COLLABORATOR
169
+ }
170
+ }
171
+ } ) ;
172
+ console . log ( result ) ;
94
173
}
95
174
}
96
175
} ;
97
176
98
- export default Signup ;
177
+ export default withApollo ( Signup ) ;
0 commit comments