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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 2x | import React from "react";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm, SubmitHandler } from "react-hook-form";
// components
import { useParams, useSearchParams } from "react-router-dom";
import { customInstance } from "../../services/api";
import { useMutation } from "react-query";
import { showMessage } from "@common/Toast/ShowToast";
import { useMediaQuery, useTheme } from "@mui/material";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import { PASSWORD_MIN_CHARS } from "@constants/constants";
import { useNavigate } from "react-router-dom";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import useHandleLogin from "./useHandleLogin";
import { addPasswordValidationRules } from "@validation/utils";
import { VALIDATION_MESSAGES } from "@validation/messages";
import {
isAccountClosedError,
showAccountClosedPopup,
} from "./accountClosedPopup";
type IFormInputs = {
password: string;
confirmPassword: string;
termsConditions: boolean;
};
export const useChangePassword = (isToS?: boolean) => {
const theme = useTheme();
const { isRecaptchaFF } = useGetFeatureFlagValues();
const [searchParams] = useSearchParams();
const user = useAppSelector(selectUser);
const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
const navigate = useNavigate();
const { id } = useParams();
const [alert, setAlert] = React.useState("");
const schema = Yup.object().shape({
password: addPasswordValidationRules(
Yup.string()
.required(VALIDATION_MESSAGES.PASSWORD_REQUIRED)
.test({
name: "validator-custom-password",
test: function (value) {
return (value || "").length < PASSWORD_MIN_CHARS
? this.createError({
message: `Enter ${
PASSWORD_MIN_CHARS - (value || "").length
} more characters`,
path: "password",
})
: true;
},
})
),
confirmPassword: Yup.string().required(VALIDATION_MESSAGES.CONFIRM_PASSWORD_REQUIRED),
termsConditions: Yup.boolean().when([], {
is: () => isToS,
then: Yup.boolean().oneOf(
[true],
VALIDATION_MESSAGES.TERMS_REQUIRED,
),
}),
});
const defaultValues = {
password: "",
confirmPassword: "",
termsConditions: true,
};
const methods = useForm<IFormInputs>({
resolver: yupResolver(schema),
mode: "onChange",
defaultValues,
});
const changePwdMutation = useMutation(
(data: { password: string; hasAcceptedTC?: boolean }) => {
return customInstance({
url: `password-resets/${id}`,
method: "POST",
data,
withCredentials: false,
});
},
);
const { isLoading } = changePwdMutation;
const {
onHandleSubmit,
isLoading: isLoadingLogin,
tmpStep2,
handleVerify,
error2FA,
is2FALoading,
getRecaptchaToken,
signinMutationIsLoading,
onGoBack,
} = useHandleLogin({
email: searchParams.get("email") || "",
});
React.useEffect(() => {
if (methods.formState.isDirty) {
setAlert("");
}
}, [methods.formState.isDirty]);
const handleLogin = async (data: {
captchaToken?: string;
email: string;
password: string;
termsConditions?: boolean;
}) => {
if (!data.email || !data.password || isLoadingLogin) return;
//
onHandleSubmit(data);
};
const handleSubmitFn = (data: any) => {
const payload = { ...data, hasAcceptedTC: true };
if (isRecaptchaFF && process.env.VITE_RECAPTCHA_SITE_KEY) {
return getRecaptchaToken((captchaToken?: string) =>
handleLogin({ ...payload, captchaToken }),
);
} else return handleLogin(payload);
};
const onSubmit: SubmitHandler<IFormInputs> = (data) => {
const customData = {
password: data.password,
...(isToS && {
hasAcceptedTC: data.termsConditions,
}),
};
changePwdMutation.mutate(customData, {
onSuccess: async (data, variables, context) => {
showMessage("Success", "Password set successfully", isDesktop);
const email = searchParams.get("email") || "";
if (!email) {
return navigate("/login");
}
const payload = {
password: variables?.password,
termsConditions: variables?.hasAcceptedTC,
email,
};
if (!user?.email) {
return handleSubmitFn(payload);
} else {
navigate("/");
}
},
onError: (error: any) => {
if (isAccountClosedError(error)) {
showAccountClosedPopup();
return;
}
const usedPasswordMessage =
"The new password must not match the previous five.";
if (error.response?.data?.message === usedPasswordMessage) {
setAlert(usedPasswordMessage);
methods.reset(data);
return;
}
showMessage(
"Error",
error?.response?.data?.message ||
"The provided password reset request is invalid and/or expired",
isDesktop,
);
},
});
};
return {
methods,
onSubmit,
isDisabled: isLoading || signinMutationIsLoading,
alert,
tmpStep2,
setTmpStep2: (emai: any) => navigate("/Login"),
handleSubmitFn,
handleVerify,
onGoBack,
error2FA,
is2FALoading,
isLoadingLogin,
};
};
|