import { useMutation, useQuery } from "@tanstack/react-query";
import { toast } from "react-toastify";
import Axios from "../utils/ApiConfiguration";
import axios from "axios";
import baseUrl from "../array/base/config";
/**
* Custom hook to request a password reset email.
*
* This function makes an API call to request a password reset email and sends an OTP code to the user's email address.
*
* @param {object} data - The data for requesting a password reset.
* @param {string} data.email - The email address for password reset.
*
* @returns {import("@tanstack/react-query").UseMutationResult} The mutation result object.
*
* @throws {Error} If the API call fails or encounters an error.
*
* @example
* const { mutate, isLoading, isError } = useForgetPassword();
* const resetData = { email: "user@example.com" };
*
* const handlePasswordReset = async () => {
* try {
* await mutate(resetData);
* // Display success message
* } catch (error) {
* // Display error message
* }
* };
*/
export const useForgetPassword = () =>
useMutation(
(data) => Axios.post("auth/forgot_password_email/", { ...data }),
{
onSuccess: (res) => {
toast.success("Otp code has been sent to your email.");
},
onError: (err) => {
toast.error(err?.response?.data?.message || "Something went wrong");
},
}
);
/**
* Custom hook to verify an OTP code.
*
* This function makes an API call to verify an OTP code for password reset.
*
* @param {object} data - The data for OTP verification.
* @param {string} data.otpCode - The OTP code to verify.
*
* @returns {import("@tanstack/react-query").UseMutationResult} The mutation result object.
*
* @throws {Error} If the API call fails or encounters an error.
*
* @example
* const { mutate, isLoading, isError } = useVerifyOtp();
* const otpData = { otpCode: "123456" };
*
* const handleOtpVerification = async () => {
* try {
* await mutate(otpData);
* // Display success message
* } catch (error) {
* // Display error message
* }
* };
*/
export const useVerifyOtp = () =>
useMutation((data) => Axios.post("auth/otp_verification/", { ...data }), {
onSuccess: (res) => {
toast.success("Otp code verified");
},
onError: (err) => {
console.log(err);
toast.error(err?.response?.data?.detail || "Something went wrong");
},
});
/**
* Custom hook to change the user's password.
*
* This function makes an API call to change the user's password using an OTP code.
*
* @param {object} data - The data for changing the password.
* @param {string} data.token - The authentication token.
* @param {object} data.data - The new password data.
* @param {string} data.data.newPassword - The new password.
* @param {string} data.data.confirmPassword - The confirmed new password.
*
* @returns {import("@tanstack/react-query").UseMutationResult} The mutation result object.
*
* @throws {Error} If the API call fails or encounters an error.
*
* @example
* const { mutate, isLoading, isError } = useChangePassword();
* const changePasswordData = {
* token: "yourAuthToken",
* data: {
* newPassword: "newPassword123",
* confirmPassword: "newPassword123",
* },
* };
*
* const handleChangePassword = async () => {
* try {
* await mutate(changePasswordData);
* // Display success message
* } catch (error) {
* // Display error message
* }
* };
*/
export const useChangePassword = () =>
useMutation(
async (data) =>
await axios({
method: "post",
url: `${baseUrl}/api/auth/password/change/`,
headers: {
Authorization: `Token ${data.token}`,
},
data: { ...data.data },
}),
{
onSuccess: (res) => {
toast.success("You have successfully changed your password.");
},
onError: (err) => {
const errors = Object.values(err?.response?.data?.errors);
toast.error(errors[0][0] || "Something went wrong");
},
}
);
Source