Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 16x 16x 16x 16x 16x 2x 2x 16x 16x 16x 16x 2x 16x 2x 2x 2x 1x 1x 1x 1x 1x 16x 2x 16x 8x 8x 16x | import React from "react";
// form
import { showMessage } from "@common/Toast/ShowToast";
import { isValidEmail } from "@validation/regex";
import NiceModal from "@ebay/nice-modal-react";
import { yupResolver } from "@hookform/resolvers/yup";
import { useMasquerade } from "@hooks/enterprise-api/merchants";
import { SECURITY_CHECK_MODAL } from "modals/modal_names";
import { SubmitHandler, useForm } from "react-hook-form";
import { useMutation } from "react-query";
import * as Yup from "yup";
import { customInstance } from "../../services/api";
import { VALIDATION_MESSAGES } from "@validation/messages";
import {
isAccountClosedError,
showAccountClosedPopup,
} from "./accountClosedPopup";
type IFormInputs = {
email: string;
};
export const useForgotPassword = () => {
const [exist, setExist] = React.useState(false);
const [error, setError] = React.useState(false);
const [previousEmail, setPreviousEmail] = React.useState("");
const { clearMasquerade } = useMasquerade();
const schema = Yup.object().shape({
email: Yup.string()
.email(VALIDATION_MESSAGES.INVALID_EMAIL)
.required(VALIDATION_MESSAGES.EMAIL_REQUIRED)
.test(
"is-valid-email",
VALIDATION_MESSAGES.INVALID_EMAIL,
(value: string | undefined) => {
Iif (!value) {
return true; // allow empty values
}
return isValidEmail(value);
},
),
});
const defaultValues = {
email: "",
};
const methods = useForm<IFormInputs>({
resolver: yupResolver(schema),
defaultValues,
});
const { watch } = methods;
const resetPasswordMutation = useMutation((data: { email: string }) => {
return customInstance({
url: "password-resets",
method: "POST",
data: { email: data.email },
});
});
const onSubmit: SubmitHandler<IFormInputs> = (data) => {
clearMasquerade();
setPreviousEmail(data.email);
resetPasswordMutation.mutate(
{ email: data.email },
{
onError: (error: any) => {
setExist(false);
Iif (isAccountClosedError(error)) {
setError(false);
showAccountClosedPopup();
return;
}
setError(true);
},
onSuccess: () => {
setExist(true);
showMessage("Success", "Check your email to reset password");
},
},
);
};
const handleClickReset = () => {
NiceModal.show(SECURITY_CHECK_MODAL, {
variant: "forgotPassword",
onConfirm: methods.handleSubmit(onSubmit),
});
};
React.useEffect(() => {
Iif (error) setError(false);
Iif (exist) setExist(false);
}, [watch("email")]);
return {
methods,
onSubmit,
exist,
error,
previousEmail,
isLoading: resetPasswordMutation.isLoading,
handleClickReset,
};
};
|