forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForgotPasswordPage.js
More file actions
112 lines (105 loc) · 3.12 KB
/
ForgotPasswordPage.js
File metadata and controls
112 lines (105 loc) · 3.12 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
import React from 'react'
import { Trans, t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Stack, Button, Box, useToast, Heading } from '@chakra-ui/core'
import EmailField from './EmailField'
import { object, string } from 'yup'
import { Formik } from 'formik'
import { Link as RouteLink, useHistory } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { SEND_PASSWORD_RESET_LINK } from './graphql/mutations'
import { TrackerButton } from './TrackerButton'
export default function ForgotPasswordPage() {
const { i18n } = useLingui()
const toast = useToast()
const history = useHistory()
const validationSchema = object().shape({
email: string()
.required(i18n._(t`Email cannot be empty`))
.email(i18n._(t`Invalid email`)),
})
const [sendPasswordResetLink, { loading, error }] = useMutation(
SEND_PASSWORD_RESET_LINK,
{
onError(error) {
toast({
title: error.message,
description: i18n._(t`Unable to send password reset link to email.`),
status: 'error',
duration: 9000,
isClosable: true,
})
},
onCompleted() {
history.push('/')
// Display a welcome message
toast({
title: i18n._(t`Email Sent`),
description: i18n._(
t`An email was sent with a link to reset your password`,
),
status: 'success',
duration: 9000,
isClosable: true,
})
},
},
)
if (loading)
return (
<p>
<Trans>Loading...</Trans>
</p>
)
if (error) return <p>{String(error)}</p>
return (
<Box px="8" mx="auto" overflow="hidden" w={['100%', '60%']}>
<Formik
validationSchema={validationSchema}
initialValues={{ email: '' }}
onSubmit={async values => {
sendPasswordResetLink({
variables: { userName: values.email },
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form
onSubmit={handleSubmit}
role="form"
aria-label="form"
name="form"
>
<Heading as="h1" fontSize="2xl" mb="6" textAlign="center">
<Trans>
Enter your user account's verified email address and we will
send you a password reset link.
</Trans>
</Heading>
<EmailField name="email" mb="4" />
<Stack spacing={4} isInline justifyContent="space-between" mb="4">
<TrackerButton
type="submit"
id="submitBtn"
isLoading={isSubmitting}
variant="primary"
>
<Trans>Submit</Trans>
</TrackerButton>
<Button
as={RouteLink}
to="/sign-in"
color="primary"
bg="transparent"
borderColor="primary"
borderWidth="1px"
>
<Trans>Back</Trans>
</Button>
</Stack>
</form>
)}
</Formik>
</Box>
)
}