@@ -8,7 +8,13 @@ import {
8
8
import React , { FormEvent , useCallback , useEffect , useState } from 'react' ;
9
9
import { useSettings } from '../helpers/AppSettings' ;
10
10
import { Button } from './Button' ;
11
- import { nameRegex , register , useServerURL , useStore } from '@tomic/react' ;
11
+ import {
12
+ addPublicKey ,
13
+ nameRegex ,
14
+ register as createRegistration ,
15
+ useServerURL ,
16
+ useStore ,
17
+ } from '@tomic/react' ;
12
18
import Field from './forms/Field' ;
13
19
import { InputWrapper , InputStyled } from './forms/InputStyles' ;
14
20
import { Row } from './Row' ;
@@ -20,6 +26,16 @@ interface RegisterSignInProps {
20
26
redirect ?: string ;
21
27
}
22
28
29
+ /** What is currently showing */
30
+ enum PageStateOpts {
31
+ none ,
32
+ signIn ,
33
+ register ,
34
+ reset ,
35
+ mailSentRegistration ,
36
+ mailSentAddPubkey ,
37
+ }
38
+
23
39
/**
24
40
* Two buttons: Register / Sign in.
25
41
* Opens a Dialog / Modal with the appropriate form.
@@ -29,7 +45,8 @@ export function RegisterSignIn({
29
45
} : React . PropsWithChildren < RegisterSignInProps > ) : JSX . Element {
30
46
const { dialogProps, show, close } = useDialog ( ) ;
31
47
const { agent } = useSettings ( ) ;
32
- const [ isRegistering , setRegister ] = useState ( true ) ;
48
+ const [ pageState , setPageState ] = useState < PageStateOpts > ( PageStateOpts . none ) ;
49
+ const [ email , setEmail ] = useState ( '' ) ;
33
50
34
51
if ( agent ) {
35
52
return < > { children } </ > ;
@@ -39,7 +56,7 @@ export function RegisterSignIn({
39
56
< Row >
40
57
< Button
41
58
onClick = { ( ) => {
42
- setRegister ( true ) ;
59
+ setPageState ( PageStateOpts . register ) ;
43
60
show ( ) ;
44
61
} }
45
62
>
@@ -48,27 +65,116 @@ export function RegisterSignIn({
48
65
< Button
49
66
subtle
50
67
onClick = { ( ) => {
51
- setRegister ( false ) ;
68
+ setPageState ( PageStateOpts . signIn ) ;
52
69
show ( ) ;
53
70
} }
54
71
>
55
72
Sign In
56
73
</ Button >
57
74
</ Row >
58
75
< Dialog { ...dialogProps } >
59
- { isRegistering ? < Register close = { close } /> : < SignIn /> }
76
+ { pageState === PageStateOpts . register && (
77
+ < Register
78
+ setPageState = { setPageState }
79
+ email = { email }
80
+ setEmail = { setEmail }
81
+ />
82
+ ) }
83
+ { pageState === PageStateOpts . signIn && (
84
+ < SignIn setPageState = { setPageState } />
85
+ ) }
86
+ { pageState === PageStateOpts . reset && (
87
+ < Reset
88
+ email = { email }
89
+ setEmail = { setEmail }
90
+ setPageState = { setPageState }
91
+ />
92
+ ) }
93
+ { pageState === PageStateOpts . mailSentRegistration && (
94
+ < MailSentConfirm
95
+ email = { email }
96
+ close = { close }
97
+ message = { 'Your account will be created when you open that link.' }
98
+ />
99
+ ) }
100
+ { pageState === PageStateOpts . mailSentAddPubkey && (
101
+ < MailSentConfirm
102
+ email = { email }
103
+ close = { close }
104
+ message = { 'Click that link to create a new PassPhrase.' }
105
+ />
106
+ ) }
60
107
</ Dialog >
61
108
</ >
62
109
) ;
63
110
}
64
111
65
- function Register ( { close } ) {
112
+ function Reset ( { email, setEmail, setPageState } ) {
113
+ const store = useStore ( ) ;
114
+ const [ err , setErr ] = useState < Error | undefined > ( undefined ) ;
115
+
116
+ const handleRequestReset = useCallback ( async ( ) => {
117
+ try {
118
+ await addPublicKey ( store , email ) ;
119
+ setPageState ( PageStateOpts . mailSentAddPubkey ) ;
120
+ } catch ( e ) {
121
+ setErr ( e ) ;
122
+ }
123
+ } , [ email ] ) ;
124
+
125
+ return (
126
+ < >
127
+ < DialogTitle >
128
+ < h1 > Reset your PassKey</ h1 >
129
+ </ DialogTitle >
130
+ < DialogContent >
131
+ < p >
132
+ {
133
+ "Lost it? No worries, we'll send a link that let's you create a new one."
134
+ }
135
+ </ p >
136
+ < EmailField
137
+ email = { email }
138
+ setEmail = { ( e : any ) => {
139
+ setErr ( undefined ) ;
140
+ setEmail ( e ) ;
141
+ } }
142
+ />
143
+ { err && < ErrorLook > { err . message } </ ErrorLook > }
144
+ </ DialogContent >
145
+ < DialogActions >
146
+ < Button onClick = { handleRequestReset } > Send me</ Button >
147
+ </ DialogActions >
148
+ </ >
149
+ ) ;
150
+ }
151
+
152
+ function MailSentConfirm ( { email, close, message } ) {
153
+ return (
154
+ < >
155
+ < DialogTitle >
156
+ < h1 > Go to your email inbox</ h1 >
157
+ </ DialogTitle >
158
+ < DialogContent >
159
+ < p >
160
+ { "We've sent a confirmation link to " }
161
+ < strong > { email } </ strong >
162
+ { '.' }
163
+ </ p >
164
+ < p > { message } </ p >
165
+ </ DialogContent >
166
+ < DialogActions >
167
+ < Button onClick = { close } > { "Ok, I'll open my mailbox!" } </ Button >
168
+ </ DialogActions >
169
+ </ >
170
+ ) ;
171
+ }
172
+
173
+ function Register ( { setPageState, email, setEmail } ) {
66
174
const [ name , setName ] = useState ( '' ) ;
67
- const [ email , setEmail ] = useState ( '' ) ;
68
175
const [ serverUrlStr ] = useServerURL ( ) ;
69
176
const [ nameErr , setErr ] = useState < Error | undefined > ( undefined ) ;
70
177
const store = useStore ( ) ;
71
- const [ mailSent , setMailSent ] = useState ( false ) ;
72
178
73
179
const serverUrl = new URL ( serverUrlStr ) ;
74
180
serverUrl . host = `${ name } .${ serverUrl . host } ` ;
@@ -93,36 +199,15 @@ function Register({ close }) {
93
199
}
94
200
95
201
try {
96
- await register ( store , name , email ) ;
97
- setMailSent ( true ) ;
202
+ await createRegistration ( store , name , email ) ;
203
+ setPageState ( PageStateOpts . mailSentRegistration ) ;
98
204
} catch ( er ) {
99
205
setErr ( er ) ;
100
206
}
101
207
} ,
102
208
[ name , email ] ,
103
209
) ;
104
210
105
- if ( mailSent ) {
106
- return (
107
- < >
108
- < DialogTitle >
109
- < h1 > Go to your email inbox</ h1 >
110
- </ DialogTitle >
111
- < DialogContent >
112
- < p >
113
- { "We've sent a confirmation link to " }
114
- < strong > { email } </ strong >
115
- { '.' }
116
- </ p >
117
- < p > Your account will be created when you open that link.</ p >
118
- </ DialogContent >
119
- < DialogActions >
120
- < Button onClick = { close } > Ok, I will!</ Button >
121
- </ DialogActions >
122
- </ >
123
- ) ;
124
- }
125
-
126
211
return (
127
212
< >
128
213
< DialogTitle >
@@ -147,48 +232,63 @@ function Register({ close }) {
147
232
/>
148
233
</ InputWrapper >
149
234
</ Field >
150
- < Field label = 'E-mail' >
151
- < InputWrapper >
152
- < InputStyled
153
- autoFocus = { true }
154
- type = { 'email' }
155
- required
156
- value = { email }
157
- onChange = { e => {
158
- setEmail ( e . target . value ) ;
159
- } }
160
- />
161
- </ InputWrapper >
162
- </ Field >
235
+ < EmailField email = { email } setEmail = { setEmail } />
163
236
{ name && nameErr && < ErrorLook > { nameErr . message } </ ErrorLook > }
164
237
</ form >
165
238
</ DialogContent >
166
239
< DialogActions >
240
+ < Button subtle onClick = { ( ) => setPageState ( PageStateOpts . signIn ) } >
241
+ Sign in
242
+ </ Button >
167
243
< Button
168
244
type = 'submit'
169
245
form = 'register-form'
170
246
disabled = { ! name || ! ! nameErr }
171
247
onClick = { handleSubmit }
172
248
>
173
- Create
249
+ Save
174
250
</ Button >
175
251
</ DialogActions >
176
252
</ >
177
253
) ;
178
254
}
179
255
180
- function SignIn ( ) {
256
+ function SignIn ( { setPageState } ) {
181
257
return (
182
258
< >
183
259
< DialogTitle >
184
- < h1 > Sign in </ h1 >
260
+ < h1 > Sign in</ h1 >
185
261
</ DialogTitle >
186
262
< DialogContent >
187
263
< SettingsAgent />
188
264
</ DialogContent >
189
265
< DialogActions >
190
- < p > Lost your passphrase?</ p >
266
+ < Button subtle onClick = { ( ) => setPageState ( PageStateOpts . register ) } >
267
+ Register
268
+ </ Button >
269
+ < Button subtle onClick = { ( ) => setPageState ( PageStateOpts . reset ) } >
270
+ I lost my passphrase
271
+ </ Button >
191
272
</ DialogActions >
192
273
</ >
193
274
) ;
194
275
}
276
+
277
+ function EmailField ( { setEmail, email } ) {
278
+ return (
279
+ < Field label = 'E-mail' >
280
+ < InputWrapper >
281
+ < InputStyled
282
+ // This is not properly working atm
283
+ autoFocus
284
+ type = { 'email' }
285
+ required
286
+ value = { email }
287
+ onChange = { e => {
288
+ setEmail ( e . target . value ) ;
289
+ } }
290
+ />
291
+ </ InputWrapper >
292
+ </ Field >
293
+ ) ;
294
+ }
0 commit comments