-
Notifications
You must be signed in to change notification settings - Fork 69
/
[token].tsx
101 lines (90 loc) · 2.69 KB
/
[token].tsx
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
import { useMutation } from '@apollo/client'
import React from 'react'
import { useRouter } from 'next/router'
import { Formik, Form, Field } from 'formik'
import UPDATE_PASSWORD from '../../graphql/queries/updatePassword'
import NavLink from '../../components/NavLink'
import Input from '../../components/Input'
import { confirmPasswordValidation } from '../../helpers/formValidation'
import Layout from '../../components/Layout'
import Card from '../../components/Card'
const initialValues = {
password: '',
confirmPassword: ''
}
const ConfirmSuccess: React.FC = () => (
<Card type="success" title="Password has been set!">
<a className="btn btn-primary btn-lg mb-3" role="button" href="/curriculum">
Continue to dashboard
</a>
</Card>
)
const ExpiredToken: React.FC = () => (
<Card type="fail" title="Link has expired.">
<NavLink path="/forgotpassword" className="btn btn-primary">
Request a new password reset
</NavLink>
</Card>
)
export const ResetPassword: React.FC = () => {
const router = useRouter()
const [changePw, { data, error }] = useMutation(UPDATE_PASSWORD)
const handleSubmit = async ({ password }: typeof initialValues) => {
try {
await changePw({
variables: {
token: router.query.token,
password
}
})
} catch {} // catch error thrown by default from apollo mutations
}
if (data && data.changePw.success) {
return <ConfirmSuccess />
}
if (error) return <ExpiredToken />
return (
<Card title="Enter new password">
<p className="mb-4">Your password should contain minimum 6 characters.</p>
<Formik
validateOnBlur
initialValues={initialValues}
validationSchema={confirmPasswordValidation}
onSubmit={handleSubmit}
>
<Form data-testid="form">
<div className="form-group">
<Field
name="password"
placeholder="Enter new password"
data-testid="password"
type="password"
as={Input}
autoFocus
/>
<Field
name="confirmPassword"
placeholder="Re-enter new password"
data-testid="confirmPassword"
type="password"
as={Input}
/>
<button
className="btn btn-primary btn-lg btn-block mb-3"
type="submit"
data-testid="submit"
>
Reset Password
</button>
</div>
</Form>
</Formik>
</Card>
)
}
export const ResetPasswordContainer = () => (
<Layout title="Confirm">
<ResetPassword />
</Layout>
)
export default ResetPasswordContainer