-
Notifications
You must be signed in to change notification settings - Fork 69
/
forgotpassword.tsx
89 lines (84 loc) · 2.44 KB
/
forgotpassword.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
import { ApolloError, useMutation } from '@apollo/client'
import React, { useState } from 'react'
import { Formik, Form, Field } from 'formik'
import RESET_PASSWORD from '../graphql/queries/resetPassword'
import { resetPasswordValidation } from '../helpers/formValidation'
import Input from '../components/Input'
import Layout from '../components/Layout'
import Card from '../components/Card'
const initialValues = {
userOrEmail: ''
}
export const ResetPassword: React.FC = () => {
const [error, setError] = useState<null | ApolloError>(null)
const [reqPwReset, { data }] = useMutation(RESET_PASSWORD, {
onError: setError
})
const handleSubmit = async ({ userOrEmail }: { userOrEmail: string }) => {
try {
await reqPwReset({ variables: { userOrEmail } })
} catch {} // catch error that's thrown by default from mutation
}
if (data) {
return (
<Card title="Password reset instructions sent" type="success">
<p>
You will receive an email containing a link to reset your password.
</p>
</Card>
)
}
if (error) {
return (
<Card title="Username or Email does not exist">
<button
className="btn btn-primary btn-lg btn-block mb-3"
onClick={() => setError(null)}
data-testid="back"
>
Go Back
</button>
</Card>
)
}
return (
<Card title="Reset your password">
<p className="mb-5">
Type in your email or username below and we’ll send you an email
with instructions on how to reset your password
</p>
<Formik
validateOnBlur
initialValues={initialValues}
validationSchema={resetPasswordValidation}
onSubmit={handleSubmit}
>
<Form data-testid="form">
<div className="form-group">
<Field
name="userOrEmail"
placeholder="Username or Email"
data-testid="userOrEmail"
type="text"
as={Input}
autoFocus
/>
<button
className="btn btn-primary btn-lg btn-block mb-3"
type="submit"
data-testid="submit"
>
Send Reset Email
</button>
</div>
</Form>
</Formik>
</Card>
)
}
export const ResetPasswordContainer = () => (
<Layout title="Reset password">
<ResetPassword />
</Layout>
)
export default ResetPasswordContainer