1
1
import { confirmEmail , useStore } from '@tomic/react' ;
2
2
import * as React from 'react' ;
3
3
import { useState } from 'react' ;
4
- import { CodeBlock } from '../components/CodeBlock' ;
4
+ import toast from 'react-hot-toast' ;
5
+ import { Button } from '../components/Button' ;
6
+ import { CodeBlockStyled } from '../components/CodeBlock' ;
5
7
import { ContainerNarrow } from '../components/Containers' ;
6
8
import { isDev } from '../config' ;
7
9
import { useSettings } from '../helpers/AppSettings' ;
8
- import { handleError } from '../helpers/handlers' ;
9
10
import {
10
11
useCurrentSubject ,
11
12
useSubjectParam ,
@@ -19,10 +20,13 @@ const ConfirmEmail: React.FunctionComponent = () => {
19
20
const [ secret , setSecret ] = useState ( '' ) ;
20
21
const store = useStore ( ) ;
21
22
const [ token ] = useSubjectParam ( 'token' ) ;
22
- const { agent , setAgent } = useSettings ( ) ;
23
+ const { setAgent } = useSettings ( ) ;
23
24
const [ destinationToGo , setDestination ] = useState < string > ( ) ;
25
+ const [ err , setErr ] = useState < Error | undefined > ( undefined ) ;
26
+ const [ triedConfirm , setTriedConfirm ] = useState ( false ) ;
24
27
25
- const handleConfirm = async ( ) => {
28
+ const handleConfirm = React . useCallback ( async ( ) => {
29
+ setTriedConfirm ( true ) ;
26
30
let tokenUrl = subject as string ;
27
31
28
32
if ( isDev ( ) ) {
@@ -37,37 +41,68 @@ const ConfirmEmail: React.FunctionComponent = () => {
37
41
store ,
38
42
tokenUrl ,
39
43
) ;
40
- setAgent ( newAgent ) ;
41
44
setSecret ( newAgent . buildSecret ( ) ) ;
42
45
setDestination ( destination ) ;
46
+ setAgent ( newAgent ) ;
47
+ toast . success ( 'Email confirmed!' ) ;
43
48
} catch ( e ) {
44
- handleError ( e ) ;
49
+ setErr ( e ) ;
50
+ }
51
+ } , [ subject ] ) ;
52
+
53
+ if ( ! triedConfirm && subject ) {
54
+ handleConfirm ( ) ;
55
+ }
56
+
57
+ if ( err ) {
58
+ if ( err . message . includes ( 'expired' ) ) {
59
+ return (
60
+ < ContainerNarrow >
61
+ The link has expired. Request a new one by Registering again.
62
+ </ ContainerNarrow >
63
+ ) ;
45
64
}
46
- } ;
47
65
48
- if ( ! agent ) {
49
- return (
50
- < ContainerNarrow >
51
- < button onClick = { handleConfirm } > confirm</ button >
52
- </ ContainerNarrow >
53
- ) ;
66
+ return < ContainerNarrow > { err ?. message } </ ContainerNarrow > ;
67
+ }
68
+
69
+ if ( secret ) {
70
+ return < SavePassphrase secret = { secret } destination = { destinationToGo } /> ;
71
+ }
72
+
73
+ return < ContainerNarrow > Verifying token...</ ContainerNarrow > ;
74
+ } ;
75
+
76
+ function SavePassphrase ( { secret, destination } ) {
77
+ const [ copied , setCopied ] = useState ( false ) ;
78
+
79
+ function copyToClipboard ( ) {
80
+ setCopied ( secret ) ;
81
+ navigator . clipboard . writeText ( secret || '' ) ;
82
+ toast . success ( 'Copied to clipboard' ) ;
54
83
}
55
84
56
85
return (
57
86
< ContainerNarrow >
58
- < h1 > Save your Passphrase </ h1 >
87
+ < h1 > Mail confirmed, please save your passphrase </ h1 >
59
88
< p >
60
89
Your Passphrase is like your password. Never share it with anyone. Use a
61
- password manager to store it securely. You will need this to log in
62
- next!
90
+ password manager like{ ' ' }
91
+ < a href = 'https://bitwarden.com/' target = '_blank' rel = 'noreferrer' >
92
+ BitWarden
93
+ </ a > { ' ' }
94
+ to store it securely.
63
95
</ p >
64
- < CodeBlock content = { secret } wrapContent />
65
- { /* <Button onClick={handleGoToDestination}>Continue here</Button> */ }
66
- < a href = { destinationToGo } target = '_blank' rel = 'noreferrer' >
67
- Open my new Drive!
68
- </ a >
96
+ < CodeBlockStyled wrapContent > { secret } </ CodeBlockStyled >
97
+ { copied ? (
98
+ < a href = { destination } target = '_blank' rel = 'noreferrer' >
99
+ { "I've saved my PassPhrase, open my new Drive!" }
100
+ </ a >
101
+ ) : (
102
+ < Button onClick = { copyToClipboard } > Copy Passphrase</ Button >
103
+ ) }
69
104
</ ContainerNarrow >
70
105
) ;
71
- } ;
106
+ }
72
107
73
108
export default ConfirmEmail ;
0 commit comments