7
7
FormLabel ,
8
8
HStack ,
9
9
Input ,
10
+ Spinner ,
10
11
Text ,
11
12
VStack ,
12
13
} from "@chakra-ui/react" ;
@@ -67,18 +68,27 @@ const Login = (): React.ReactElement => {
67
68
const { authenticatedUser, setAuthenticatedUser } = useContext ( AuthContext ) ;
68
69
const [ email , setEmail ] = useState ( "" ) ;
69
70
const [ password , setPassword ] = useState ( "" ) ;
70
- const [ error , setError ] = useState ( false ) ;
71
+ const [ errorMessage , setErrorMessage ] = useState < string | undefined > ( undefined ) ;
72
+
73
+ const [ login ,
74
+ { loading : loginLoading , error : loginError } ,
75
+ ] = useMutation < { login : LoginData } > ( LOGIN ) ;
71
76
72
- const [ login ] = useMutation < { login : LoginData } > ( LOGIN ) ;
73
77
74
78
const onLogInClick = async ( ) => {
75
79
let user : AuthenticatedUser | null = null ;
76
80
try {
77
81
user = await authAPIClient . login ( email , password , "" , login ) ;
78
- setError ( false ) ;
82
+ setErrorMessage ( undefined ) ;
79
83
} catch ( e : unknown ) {
80
- logPossibleGraphQLError ( e as ApolloError , setAuthenticatedUser ) ;
81
- setError ( true ) ;
84
+ const errorCasted = e as ApolloError
85
+ logPossibleGraphQLError ( errorCasted , setAuthenticatedUser ) ;
86
+ if ( ( errorCasted ?. message ?? "" ) . indexOf ( "Failed to sign-in" ) !== - 1 ) {
87
+ setErrorMessage ( "Invalid email or password, please try again!" )
88
+ }
89
+ else {
90
+ setErrorMessage ( "An unexpected error occurred, please try again!" )
91
+ }
82
92
}
83
93
setAuthenticatedUser ( user ) ;
84
94
} ;
@@ -116,14 +126,14 @@ const Login = (): React.ReactElement => {
116
126
>
117
127
Log in to account
118
128
</ Text >
119
- { error ? (
129
+ { errorMessage ? (
120
130
< Text
121
131
pb = { 5 }
122
132
textAlign = "center"
123
133
variant = { { base : "mobile-caption" , md : "desktop-caption" } }
124
134
color = "secondary.critical"
125
135
>
126
- The email or password you entered is incorrect. Please try again.
136
+ { errorMessage }
127
137
</ Text >
128
138
) : (
129
139
< Text
@@ -136,7 +146,7 @@ const Login = (): React.ReactElement => {
136
146
) }
137
147
< Flex width = "100%" justifyContent = "flexStart" flexDirection = "column" >
138
148
< Box >
139
- < FormControl pb = { 5 } isRequired isInvalid = { error } >
149
+ < FormControl pb = { 5 } isRequired isInvalid = { errorMessage !== undefined } >
140
150
< FormLabel
141
151
variant = { {
142
152
base : "mobile-form-label-bold" ,
@@ -154,7 +164,7 @@ const Login = (): React.ReactElement => {
154
164
</ FormControl >
155
165
</ Box >
156
166
< Box >
157
- < FormControl pb = { 2 } isRequired isInvalid = { error } >
167
+ < FormControl pb = { 2 } isRequired isInvalid = { errorMessage !== undefined } >
158
168
< FormLabel
159
169
variant = { {
160
170
base : "mobile-form-label-bold" ,
@@ -188,6 +198,7 @@ const Login = (): React.ReactElement => {
188
198
pt = { 1 }
189
199
pb = { 1 }
190
200
backgroundColor = "primary.blue"
201
+ disabled = { loginLoading }
191
202
>
192
203
< Text
193
204
variant = { {
@@ -196,7 +207,17 @@ const Login = (): React.ReactElement => {
196
207
} }
197
208
color = "text.white"
198
209
>
199
- Log in
210
+ { loginLoading ? (
211
+ < Spinner
212
+ thickness = "4px"
213
+ speed = "0.65s"
214
+ emptyColor = "gray.200"
215
+ color = "primary.green"
216
+ size = "lg"
217
+ />
218
+ ) : (
219
+ "Login"
220
+ ) }
200
221
</ Text >
201
222
</ Button >
202
223
< HStack >
0 commit comments