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
119 lines (111 loc) · 3.52 KB
/
ResetPasswordPage.js
File metadata and controls
119 lines (111 loc) · 3.52 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
import React from 'react'
import { t, Trans } from '@lingui/macro'
import { Box, Button, Heading, useToast } from '@chakra-ui/react'
import { object, ref, string } from 'yup'
import { Formik } from 'formik'
import { useHistory, useParams } from 'react-router-dom'
import { useMutation } from '@apollo/client'
import { PasswordConfirmation } from '../components/fields/PasswordConfirmation'
import { LoadingMessage } from '../components/LoadingMessage'
import { RESET_PASSWORD } from '../graphql/mutations'
export default function ResetPasswordPage() {
const history = useHistory()
const toast = useToast()
const { resetToken } = useParams()
const validationSchema = object().shape({
password: string()
.required(t`Password cannot be empty`)
.min(12, t`Password must be at least 12 characters long`),
confirmPassword: string()
.required(t`Password confirmation cannot be empty`)
.oneOf([ref('password')], t`Passwords must match`),
})
const [resetPassword, { loading }] = useMutation(RESET_PASSWORD, {
onError(error) {
toast({
title: error.message,
description: t`Unable to update password`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ resetPassword }) {
if (resetPassword.result.__typename === 'ResetPasswordResult') {
history.push('/sign-in')
toast({
title: t`Password Updated`,
description: t`You may now sign in with your new password`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else if (resetPassword.result.__typename === 'ResetPasswordError') {
toast({
title: t`Unable to reset your password, please try again.`,
description: resetPassword.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect resetPassword.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect resetPassword.result typename.')
}
},
})
if (loading) return <LoadingMessage />
return (
<Box px="4" mx="auto" overflow="hidden">
<Formik
validationSchema={validationSchema}
initialValues={{
password: '',
confirmPassword: '',
}}
onSubmit={async (values) => {
resetPassword({
variables: {
password: values.password,
confirmPassword: values.confirmPassword,
resetToken: resetToken,
},
})
}}
>
{({ 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} />
<Button
type="submit"
isLoading={isSubmitting}
id="submitBtn"
variant="primary"
mb="4"
>
<Trans>Change Password</Trans>
</Button>
</form>
)}
</Formik>
</Box>
)
}