1- import React , { useState , useContext } from 'react' ;
1+ import React , { useState , useContext , useEffect } from 'react' ;
22import styled from 'styled-components' ;
33
44import {
@@ -90,6 +90,22 @@ const SignInAgainButton = styled(Button)`
9090 }
9191` ;
9292
93+ const InfoWrapper = styled . div `
94+ padding: 5px;
95+ height: 100%;
96+ display: flex;
97+ margin: auto;
98+ align-items: center;
99+ justify-content: center;
100+ flex-direction: column;
101+ ` ;
102+
103+ const InfoMessage = styled . div `
104+ color: #5A5A6F;
105+ font-size: 16px;
106+ font-weight: 600;
107+ ` ;
108+
93109const InputWrapper = styled . div `
94110 margin-bottom: 20px;
95111 width: 100%;
@@ -140,11 +156,11 @@ function SignInModal({ onCloseRequest }: SignInModalProps) {
140156 const [ email , setEmail ] = useState ( '' ) ;
141157 const [ error , setError ] = useState ( '' ) ;
142158
143- const auth = useContext ( AuthContext ) ;
159+ const authInfo = useContext ( AuthContext ) ;
144160
145- const isLoading = auth . state === AuthState . SigningInUser ;
161+ const isLoading = authInfo . state === AuthState . SigningInUser ;
146162
147- const isSignedIn = auth . state === AuthState . UserAndMetadataLoaded ;
163+ const isSignedIn = authInfo . state === AuthState . UserAndMetadataLoaded ;
148164
149165 function handleEmailInputChange ( e : any ) {
150166 setEmail ( e . target . value ) ;
@@ -166,6 +182,7 @@ function SignInModal({ onCloseRequest }: SignInModalProps) {
166182
167183 try {
168184 await signIn ( email ) ;
185+ setError ( '' ) ;
169186 trackSignInFinished ( ) ;
170187 } catch ( error ) {
171188 console . error ( error . message ) ;
@@ -180,90 +197,116 @@ function SignInModal({ onCloseRequest }: SignInModalProps) {
180197 }
181198
182199 function handleEmailInputOnKeyDown ( e : any ) {
183- if ( e . key === 'Enter' ) handleSignInButtonClick ( ) ;
200+ if ( e . key === 'Enter' ) {
201+ e . preventDefault ( ) ;
202+ handleSignInButtonClick ( ) ;
203+ }
184204 }
185205
186206 function handleContinueIntoAppButtonClick ( ) {
187207 trackContinueIntoAppButtonClicked ( ) ;
188208 onCloseRequest ?.( ) ;
189209 }
190210
211+ useEffect ( ( ) => {
212+ if ( authInfo . isReconnecting ) {
213+ cancelSignIn ( ) ;
214+ }
215+ } , [
216+ authInfo . isReconnecting ,
217+ ] ) ;
218+
191219 return (
192220 < StyledModal
193221 onCloseRequest = { handleCloseRequest }
194222 >
195- { ! isSignedIn && ! isLoading &&
223+
224+ { authInfo . isReconnecting
225+ &&
226+ < InfoWrapper >
227+ < InfoMessage > Contacting Devbook servers failed.</ InfoMessage >
228+ < InfoMessage > Reconnecting...</ InfoMessage >
229+ </ InfoWrapper >
230+ }
231+
232+ { ! authInfo . isReconnecting &&
196233 < >
197- < Title >
198- Sign in with your email
234+ { ! isSignedIn
235+ && ! isLoading
236+ &&
237+ < >
238+ < Title >
239+ Sign in with your email
199240 </ Title >
200241
201- < Description >
202- Click on the sign-in button to receive an email with a sign-in link.
242+ < Description >
243+ Click on the sign-in button to receive an email with a sign-in link.
203244 </ Description >
204245
205- < InputWrapper >
206- < InputTitle > EMAIL</ InputTitle >
207- < EmailInput
208- autoFocus
209- 210- value = { email }
211- onChange = { handleEmailInputChange }
212- onKeyDown = { handleEmailInputOnKeyDown }
213- />
214- </ InputWrapper >
215-
216- { error &&
217- < Error >
218- { error }
219- </ Error >
220- }
221- < SignInButton
222- onClick = { handleSignInButtonClick }
223- >
224- Sign in to Devbook
246+ < InputWrapper >
247+ < InputTitle > EMAIL</ InputTitle >
248+ < EmailInput
249+ autoFocus
250+ 251+ value = { email }
252+ onChange = { handleEmailInputChange }
253+ onKeyDown = { handleEmailInputOnKeyDown }
254+ />
255+ </ InputWrapper >
256+
257+ { error &&
258+ < Error >
259+ { error }
260+ </ Error >
261+ }
262+ < SignInButton
263+ onClick = { handleSignInButtonClick }
264+ >
265+ Sign in to Devbook
225266 </ SignInButton >
226- </ >
227- }
267+ </ >
268+ }
228269
229- { ! isSignedIn
230- && isLoading
231- && ! error
232- &&
233- < >
234- < Title >
235- Check your email
270+ { ! isSignedIn
271+ && isLoading
272+ && ! error
273+ &&
274+ < >
275+ < Title >
276+ Check your email
236277 </ Title >
237278
238- < Description >
239- We just sent an email with the sign-in link to the following email address:
279+ < Description >
280+ We just sent an email with the sign-in link to the following email address:
240281 < br />
241- < strong > { email } </ strong >
242- </ Description >
282+ < strong > { email } </ strong >
283+ </ Description >
243284
244- < Description >
245- Click on the link and you'll be signed-in in a few seconds...
285+ < Description >
286+ Click on the link and you'll be signed-in in a few seconds...
246287 </ Description >
247288
248- < Loader />
289+ < Loader />
249290
250- < SignInAgainButton
251- onClick = { handleSignInAgainButtonClick }
252- >
253- Sign in again
291+ < SignInAgainButton
292+ onClick = { handleSignInAgainButtonClick }
293+ >
294+ Sign in again
254295 </ SignInAgainButton >
255- </ >
256- }
296+ </ >
297+ }
257298
258- { isSignedIn &&
259- < >
260- < Title >
261- You are signed in!
299+ { isSignedIn &&
300+ < >
301+ < Title >
302+ You are signed in!
262303 </ Title >
263- < CheckIcon />
264- < ContinueIntoAppButton onClick = { handleContinueIntoAppButtonClick } >
265- Continue into the app
304+ < CheckIcon />
305+ < ContinueIntoAppButton onClick = { handleContinueIntoAppButtonClick } >
306+ Continue into the app
266307 </ ContinueIntoAppButton >
308+ </ >
309+ }
267310 </ >
268311 }
269312 </ StyledModal >
0 commit comments