/
ForgotPasswordModal.js
127 lines (123 loc) · 3.57 KB
/
ForgotPasswordModal.js
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
120
121
122
123
124
125
126
127
import React from "react";
import Modal from "react-modal";
import { Formik } from "formik";
import "./Auth.scss";
import { sendResetEmail } from "../services/auth-service";
import { useAlert } from "react-alert";
const customStyles = {
content: {
top: "50%",
left: "50%",
right: "auto",
bottom: "auto",
marginRight: "-50%",
transform: "translate(-50%, -50%)",
width: "90%",
maxWidth: "500px"
}
};
Modal.setAppElement("#root");
const ForgotPasswordModal = ({
errorMessage,
modalIsOpen,
closeModal,
afterOpenModal,
setForgotError
}) => {
const alert = useAlert();
return (
<React.Fragment>
<Modal
isOpen={modalIsOpen}
onAfterOpen={afterOpenModal}
onRequestClose={closeModal}
style={customStyles}
contentLabel="Forgot Password Modal"
>
<h2>Forgot Password</h2>
<Formik
initialValues={{ email: "" }}
validate={values => {
let errors = {};
if (!values.email) {
errors.email = "Required";
} else if (
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email)
) {
errors.email = "Invalid email address";
}
return errors;
}}
validateOnChange="false"
validateOnBlur="false"
onSubmit={(values, { setSubmitting }) => {
const { email } = values;
sendResetEmail(email).then(result => {
if (
result ===
"Could not complete request. Please check the email you have entered and try again." ||
result === "There was an error. Please try again."
) {
alert.error(result);
setSubmitting(false);
} else {
alert.success(
`Reset successfuly sent to ${email}. Please check your spam folder if you do not see it in your inbox.`
);
closeModal();
setSubmitting(false);
}
});
setSubmitting(false);
}}
>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting
}) => (
<form
onSubmit={handleSubmit}
name="forgot-form"
aria-labelledby="forgot"
>
<label className="form-label" htmlFor="email">
Please enter your email:
</label>
<br />
<input
id="email"
name="email"
onChange={handleChange}
onBlur={handleBlur}
value={values.email}
className={
errors.email && touched.email
? "error login-input"
: "login-input"
}
/>
{errors.email && touched.email && (
<div className="input-feedback">{errors.email}</div>
)}
{errorMessage ? (
<div className="input-feedback">{errorMessage}</div>
) : null}
<button id="send-btn" type="submit" disabled={isSubmitting}>
Submit
</button>
<span className="intext-link" onClick={closeModal}>
Close
</span>
</form>
)}
</Formik>
</Modal>
</React.Fragment>
);
};
export default ForgotPasswordModal;