1
1
import React , { useContext } from "react" ;
2
2
import { Redirect , useHistory } from "react-router-dom" ;
3
- import { Box , Button , Flex , Input , Text } from "@chakra-ui/react" ;
3
+ import {
4
+ Box ,
5
+ Button ,
6
+ Flex ,
7
+ FormControl ,
8
+ FormErrorMessage ,
9
+ Input ,
10
+ Text
11
+ } from "@chakra-ui/react" ;
4
12
import authAPIClient from "../../APIClients/AuthAPIClient" ;
5
13
import { HOME_PAGE , LOGIN_PAGE } from "../../constants/Routes" ;
6
14
import AuthContext from "../../contexts/AuthContext" ;
@@ -18,6 +26,8 @@ type SignupProps = {
18
26
setPassword : ( password : string ) => void ;
19
27
toggle : boolean ;
20
28
setToggle : ( toggle : boolean ) => void ;
29
+ emailError : boolean ;
30
+ setEmailError : ( emailError : boolean ) => void ;
21
31
} ;
22
32
23
33
const Signup = ( {
@@ -31,10 +41,28 @@ const Signup = ({
31
41
setPassword,
32
42
toggle,
33
43
setToggle,
44
+ emailError,
45
+ setEmailError
34
46
} : SignupProps ) : React . ReactElement => {
35
47
const { authenticatedUser, setAuthenticatedUser } = useContext ( AuthContext ) ;
36
48
const history = useHistory ( ) ;
37
49
50
+ const handleEmailChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
51
+ const inputValue = e . target . value as string ;
52
+ const emailRegex = / ^ [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , } $ / ;
53
+ if ( emailRegex . test ( inputValue ) ) {
54
+ setEmailError ( false )
55
+ } else {
56
+ setEmailError ( true )
57
+ }
58
+ setEmail ( inputValue )
59
+ } ;
60
+
61
+ const handlePasswordChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
62
+ const inputValue = e . target . value as string ;
63
+ setPassword ( inputValue )
64
+ }
65
+
38
66
const onSignupClick = async ( ) => {
39
67
const isInvited = await commonApiClient . isUserInvited ( email ) ;
40
68
if ( isInvited ) {
@@ -56,14 +84,11 @@ const Signup = ({
56
84
setAuthenticatedUser ( authUser ) ;
57
85
}
58
86
}
59
- } else {
60
- // TODO: make this alert better and also differentiate between
61
- // when a user is not invited and when a user's account already exists
62
- // eslint-disable-next-line no-alert
63
- window . alert ( "user not invited" ) ;
64
87
}
65
88
} ;
66
89
90
+ const isCreateAccountBtnDisabled = ( ) => emailError || email === '' || password === '' || firstName === '' || lastName === ''
91
+
67
92
const onLogInClick = ( ) => {
68
93
history . push ( LOGIN_PAGE ) ;
69
94
} ;
@@ -76,74 +101,54 @@ const Signup = ({
76
101
return (
77
102
< Flex h = "100vh" >
78
103
< Box w = "47%" >
79
- < Flex
80
- display = "flex"
81
- alignItems = "flex-start"
82
- position = "absolute"
83
- top = "17.5%"
84
- left = "6%"
85
- w = "100%"
86
- >
87
- < Text variant = "login" position = "absolute" >
88
- Sign Up
89
- </ Text >
90
- </ Flex >
91
- < Flex
92
- h = "40%"
93
- w = "36%"
94
- top = "28%"
95
- left = "6%"
96
- direction = "column"
97
- position = "absolute"
98
- justifyContent = "space-between"
104
+ < Flex
105
+ marginTop = "172px"
106
+ display = "flex"
107
+ align = "center"
108
+ justify = "center"
99
109
>
100
- < Box >
110
+ < Flex
111
+ width = "76%"
112
+ align = "flex-start"
113
+ direction = "column"
114
+ gap = "28px"
115
+ >
116
+ < Text variant = "login" paddingBottom = "12px" >
117
+ Sign Up
118
+ </ Text >
101
119
< Input
102
120
variant = "login"
103
- position = "absolute"
104
121
placeholder = "Your first name"
105
122
value = { firstName }
106
123
onChange = { ( event ) => setFirstName ( event . target . value ) }
107
124
/>
108
- </ Box >
109
- < Box >
110
125
< Input
111
126
variant = "login"
112
- position = "absolute"
113
127
placeholder = "Your last name"
114
128
value = { lastName }
115
129
onChange = { ( event ) => setLastName ( event . target . value ) }
116
130
/>
117
- </ Box >
118
- < Box >
119
- < Input
120
- variant = "login"
121
- position = "absolute"
122
- placeholder = "Your email"
123
- value = { email }
124
- onChange = { ( event ) => setEmail ( event . target . value ) }
125
- />
126
- </ Box >
127
- < Box >
128
- < Input
129
- variant = "login"
130
- type = "password"
131
- position = "absolute"
132
- placeholder = "Your password"
133
- value = { password }
134
- onChange = { ( event ) => setPassword ( event . target . value ) }
135
- />
136
- </ Box >
137
- < Box >
131
+ < FormControl isRequired isInvalid = { emailError } >
132
+ < Input
133
+ variant = "login"
134
+ placeholder = "Your email"
135
+ value = { email }
136
+ onChange = { handleEmailChange }
137
+ />
138
+ < FormErrorMessage > Please enter a valid email.</ FormErrorMessage >
139
+ </ FormControl >
140
+ < FormControl isRequired >
141
+ < Input
142
+ variant = "login"
143
+ type = "password"
144
+ placeholder = "Your password"
145
+ value = { password }
146
+ onChange = { handlePasswordChange }
147
+ />
148
+ </ FormControl >
138
149
< Button
139
150
variant = "login"
140
- position = "absolute"
141
- disabled = {
142
- email === "" ||
143
- password === "" ||
144
- firstName === "" ||
145
- lastName === ""
146
- }
151
+ disabled = { isCreateAccountBtnDisabled ( ) }
147
152
_hover = {
148
153
email && password && firstName && lastName
149
154
? {
@@ -157,22 +162,18 @@ const Signup = ({
157
162
>
158
163
Create Account
159
164
</ Button >
160
- </ Box >
161
- </ Flex >
162
- < Flex
163
- top = "80%"
164
- left = "6%"
165
- width = "100%"
166
- direction = "row"
167
- position = "absolute"
168
- alignContent = "center"
169
- >
170
- < Text variant = "loginSecondary" paddingRight = "1.1%" >
171
- Already have an account?
172
- </ Text >
173
- < Text variant = "loginTertiary" onClick = { onLogInClick } >
174
- Log In Now
175
- </ Text >
165
+ < Flex
166
+ paddingTop = "29px"
167
+ alignContent = "center"
168
+ >
169
+ < Text variant = "loginSecondary" paddingRight = "17px" >
170
+ Already have an account?
171
+ </ Text >
172
+ < Text variant = "loginTertiary" onClick = { onLogInClick } >
173
+ Log In Now
174
+ </ Text >
175
+ </ Flex >
176
+ </ Flex >
176
177
</ Flex >
177
178
</ Box >
178
179
< Box flex = "1" bg = "teal.400" >
0 commit comments