Source

apis/Login.js

import { useMutation, useQuery } from "@tanstack/react-query";
import { toast } from "react-toastify";
import Axios from "../utils/ApiConfiguration";
/**
 * Custom hook to request user login.
 *
 * This hook sends a login request to the server using the provided user data.
 *
 * @param {Object} data - The user login data.
 * @param {string} data.username - The username of the user.
 * @param {string} data.password - The password of the user.
 * @returns {Object} The mutation result object for login, with success and error handling.
 * @throws {Error} If there's an issue with the login request.
 *
 * @example
 * // Usage of useRequestLogin hook
 * const { mutate, isLoading, isError } = useRequestLogin();
 * const handleLogin = async () => {
 *   const loginData = { username: "username_here", password: "password_here" };
 *   try {
 *     await mutate(loginData);
 *     // Handle successful login
 *   } catch (error) {
 *     // Handle login error
 *   }
 * };
 */
export const useRequestLogin = () =>
  useMutation((data) => Axios.post("auth/login/", data), {
    onError: (err) => {
      toast.error("Unable to login with provided credentials.");
    },
  });
/**
 * Custom hook to change user password.
 *
 * This hook sends a password change request to the server using the provided data.
 *
 * @param {Object} data - The password change data.
 * @param {string} data.old_password - The old password of the user.
 * @param {string} data.new_password - The new password to set.
 * @param {string} data.confirm_password - The confirmation of the new password.
 * @param {string} data.token - The user's authentication token.
 * @returns {Object} The mutation result object for changing password, with success and error handling.
 * @throws {Error} If there's an issue with the password change request.
 *
 * @example
 * // Usage of useChangePassword hook
 * const { mutate, isLoading, isError } = useChangePassword();
 * const handleChangePassword = async () => {
 *   const passwordData = {
 *     old_password: "old_password_here",
 *     new_password: "new_password_here",
 *     confirm_password: "new_password_here",
 *     token: "user_token_here",
 *   };
 *   try {
 *     await mutate(passwordData);
 *     // Handle successful password change
 *   } catch (error) {
 *     // Handle password change error
 *   }
 * };
 */
export const useChangePassword = () =>
  useMutation(
    (data) =>
      Axios.put("auth/change_password/", data, {
        headers: {
          Authorization: `Token ${data.token}`,
        },
      }),
    {
      onSuccess: (res) => {
        toast.success("Password changed successfully");
      },
      onError: (err) => {
        const errors = Object.values(err?.response?.data?.errors || {});
        toast.error(Object.values(errors[0])[0]);
      },
    }
  );
/**
 * Custom hook to update the user's password for the profile section.
 *
 * This hook sends a password update request to the server.
 *
 * @param {Object} data - Password update data.
 * @param {string} data.new_password - User's new password.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with the password update request.
 *
 * @example
 * // Usage of useUpdatePassword hook
 * const { mutate, isLoading, error } = useUpdatePassword();
 * const handleUpdatePassword = async (newPassword) => {
 *   try {
 *     await mutate({ new_password: newPassword });
 *     // Handle successful password update
 *   } catch (error) {
 *     // Handle password update error
 *   }
 * };
 */
// update password for profile section
export const useUpdatePassword = () =>
  useMutation((data) => Axios.put("auth/change_password/", data), {
    onSuccess: (res) => {
      toast.success("Password updated successfully");
    },
    onError: (err) => {
      const errors = Object.values(err?.response?.data?.errors || {});
      toast.error(Object.values(errors[0])[0]);
    },
  });
/**
 * Custom hook to request an OTP for a user's phone number.
 *
 * This hook sends an OTP request to the server for phone number verification.
 *
 * @param {Object} data - OTP request data.
 * @param {string} data.phone_number - User's phone number.
 * @param {string} data.token - User's authentication token.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with the OTP request.
 *
 * @example
 * // Usage of useGetPhoneOtp hook
 * const { mutate, isLoading, error } = useGetPhoneOtp();
 * const handleGetPhoneOtp = async (phoneNumber, token) => {
 *   try {
 *     await mutate({ phone_number: phoneNumber, token });
 *     // Handle successful OTP request
 *   } catch (error) {
 *     // Handle OTP request error
 *   }
 * };
 */
export const useGetPhoneOtp = () =>
  useMutation(
    (data) =>
      Axios.patch("auth/phone_number/", data, {
        headers: {
          Authorization: `Token ${data.token}`,
        },
      }),
    {
      onError: (err) => {
        toast.error(err?.response?.data?.message);
      },
    }
  );
/**
 * Custom hook to update the user's email address.
 *
 * This hook sends an email update request to the server.
 *
 * @param {Object} data - Email update data.
 * @param {string} data.email - User's new email address.
 * @param {string} data.token - User's authentication token.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with the email update request.
 *
 * @example
 * // Usage of useUpdateEmail hook
 * const { mutate, isLoading, error } = useUpdateEmail();
 * const handleUpdateEmail = async (newEmail, token) => {
 *   try {
 *     await mutate({ email: newEmail, token });
 *     // Handle successful email update
 *   } catch (error) {
 *     // Handle email update error
 *   }
 * };
 */
export const useUpdateEmail = () =>
  useMutation(
    (data) =>
      Axios.put("auth/email/", data, {
        headers: {
          Authorization: `Token ${data.token}`,
        },
      }),
    {
      onError: (err) => {
        let temp = Object.values(err?.response?.data?.errors?.email || {});
        toast.error(temp[0] || err?.response?.data?.message);
      },
    }
  );
/**
 * Custom hook to verify OTP for the first-time user.
 *
 * This hook sends an OTP verification request to the server.
 *
 * @param {Object} data - OTP verification data.
 * @param {string} data.otp - OTP code for verification.
 * @param {string} data.token - User's authentication token.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with the OTP verification request.
 *
 * @example
 * // Usage of useVerifyOtpFirstUser hook
 * const { mutate, isLoading, error } = useVerifyOtpFirstUser();
 * const handleVerifyOtp = async (otp, token) => {
 *   try {
 *     await mutate({ otp, token });
 *     // Handle successful OTP verification
 *   } catch (error) {
 *     // Handle OTP verification error
 *   }
 * };
 */
export const useVerifyOtpFirstUser = () =>
  useMutation(
    (data) =>
      Axios.put(
        "auth/verify/",
        { otp: data.otp },
        {
          headers: {
            Authorization: `Token ${data.token}`,
          },
        }
      ),
    {
      onError: (err) => {
        let temp = Object.values(err?.response?.data?.errors || {});
        toast.error(temp[0]);
      },
    }
  );