forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetPasswordPage.js
More file actions
105 lines (98 loc) · 2.93 KB
/
ResetPasswordPage.js
File metadata and controls
105 lines (98 loc) · 2.93 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
import React from 'react'
import { Trans, t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Heading, Box, useToast } from '@chakra-ui/core'
import PasswordConfirmation from './PasswordConfirmation'
import { object, string, ref } from 'yup'
import { Formik } from 'formik'
import { useHistory, useParams } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { UPDATE_PASSWORD } from './graphql/mutations'
import { TrackerButton } from './TrackerButton'
export default function ResetPasswordPage() {
const { i18n } = useLingui()
const history = useHistory()
const toast = useToast()
const { resetToken } = useParams()
const validationSchema = object().shape({
password: string()
.required(i18n._(t`Password cannot be empty`))
.min(12, i18n._(t`Password must be at least 12 characters long`)),
confirmPassword: string()
.required(i18n._(t`Password confirmation cannot be empty`))
.oneOf([ref('password')], i18n._(t`Passwords must match`)),
})
const [updatePassword, { loading, error }] = useMutation(UPDATE_PASSWORD, {
onError(error) {
toast({
title: error.message,
description: i18n._(t`Unable to update password`),
status: 'error',
duration: 9000,
isClosable: true,
})
},
onCompleted() {
history.push('/sign-in')
toast({
title: i18n._(t`Password Updated`),
description: i18n._(t`You may now sign in with your new 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">
<Formik
validationSchema={validationSchema}
initialValues={{
password: '',
confirmPassword: '',
}}
onSubmit={async values => {
updatePassword({
variables: {
input: {
resetToken: resetToken,
password: values.password,
confirmPassword: values.confirmPassword,
},
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form
onSubmit={handleSubmit}
role="form"
aria-label="form"
name="form"
>
<Heading as="h1" fontSize="2xl" mb="6" textAlign="center">
<Trans>Enter and confirm your new password.</Trans>
</Heading>
<PasswordConfirmation mb="4" spacing="4" />
<TrackerButton
type="submit"
isLoading={isSubmitting}
id="submitBtn"
variant="primary"
mb="4"
>
<Trans>Change Password</Trans>
</TrackerButton>
</form>
)}
</Formik>
</Box>
)
}