forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserPage.js
More file actions
122 lines (105 loc) · 3.26 KB
/
UserPage.js
File metadata and controls
122 lines (105 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { useState } from 'react'
import { string } from 'prop-types'
import { Divider, Icon, SimpleGrid, Stack, useToast } from '@chakra-ui/core'
import { useMutation, useQuery } from '@apollo/client'
import { QUERY_CURRENT_USER } from './graphql/queries'
import { t, Trans } from '@lingui/macro'
import EditableUserLanguage from './EditableUserLanguage'
import EditableUserDisplayName from './EditableUserDisplayName'
import EditableUserEmail from './EditableUserEmail'
import EditableUserPassword from './EditableUserPassword'
import { LoadingMessage } from './LoadingMessage'
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
import EditableUserTFAMethod from './EditableUserTFAMethod'
import EditableUserPhoneNumber from './EditableUserPhoneNumber'
import { TrackerButton } from './TrackerButton'
import { SEND_EMAIL_VERIFICATION } from './graphql/mutations'
export default function UserPage() {
const toast = useToast()
const [emailSent, setEmailSent] = useState(false)
const [sendEmailVerification, { error }] = useMutation(
SEND_EMAIL_VERIFICATION,
{
onError() {
toast({
title: error.message,
description: t`Unable to send verification email`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted() {
toast({
title: t`Email successfully sent`,
description: t`Check your associated Tracker email for the verification link`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
setEmailSent(true)
},
},
)
const {
loading: queryUserLoading,
error: queryUserError,
data: queryUserData,
} = useQuery(QUERY_CURRENT_USER, {})
if (queryUserLoading) {
return (
<LoadingMessage>
<Trans>Account Settings</Trans>
</LoadingMessage>
)
}
if (queryUserError) {
return <ErrorFallbackMessage error={queryUserError} />
}
const {
displayName,
userName,
preferredLang,
phoneNumber,
tfaSendMethod,
emailValidated,
phoneValidated,
} = queryUserData?.userPage
return (
<SimpleGrid columns={{ md: 1, lg: 2 }} width="100%">
<Stack py={25} px="4">
<EditableUserDisplayName detailValue={displayName} />
<Divider />
<EditableUserEmail detailValue={userName} />
<Divider />
<EditableUserPassword />
<Divider />
<EditableUserLanguage currentLang={preferredLang} />
</Stack>
<Stack p={25} spacing={4}>
<EditableUserPhoneNumber detailValue={phoneNumber} />
<Divider />
<EditableUserTFAMethod
currentTFAMethod={tfaSendMethod}
emailValidated={emailValidated}
phoneValidated={phoneValidated}
/>
{!emailValidated && (
<TrackerButton
variant="primary"
onClick={() => {
sendEmailVerification({ variables: { userName: userName } })
}}
disabled={emailSent}
>
<Icon name="email" />
<Trans>Verify Email</Trans>
</TrackerButton>
)}
</Stack>
</SimpleGrid>
)
}
UserPage.propTypes = { userName: string }