All files / src/hooks/enterprise-api useSecurity.tsx

0% Statements 0/19
0% Branches 0/6
0% Functions 0/6
0% Lines 0/19

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                                                                                                                                                                                                       
import React from "react";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { customInstance } from "@services/api";
import { AxiosError } from "axios";
 
import * as Yup from "yup";
import { showMessage } from "@common/Toast/ShowToast";
import { useGetCurrentMerchantId } from "@hooks/common";
import { VALIDATION_MESSAGES } from "@validation/messages";
 
type IFormInputs = {
  new_password: string;
  current_password: string;
  confirm_password: string;
};
 
export const useSecurity = () => {
  const { merchantId } = useGetCurrentMerchantId();
  const schema = Yup.object().shape({
    // Will change min() to match for current password when API
    current_password: Yup.string()
      .required(VALIDATION_MESSAGES.CURRENT_PASSWORD_REQUIRED)
      .min(4, "Current password is wrong"),
    new_password: Yup.string()
      .matches(
        /[a-z]/,
        "Password must contain at least one lowercase character",
      )
      .test({
        name: "validator-custom-password",
        test: function (value) {
          return (value || "").length < 8
            ? this.createError({
                message: `Enter ${8 - (value || "").length} more characters`,
                path: "new_password",
              })
            : true;
        },
      }),
    confirm_password: Yup.string()
      .required(VALIDATION_MESSAGES.PASSWORD_REQUIRED)
      .oneOf([Yup.ref("new_password")], "Passwords do not match"),
  });
 
  const defaultValues = {
    current_password: "",
    new_password: "",
    confirm_password: "",
  };
 
  const methods = useForm<IFormInputs>({
    resolver: yupResolver(schema),
    mode: "onChange",
    defaultValues,
  });
 
  const {
    reset,
    watch,
    getFieldState,
    formState: { errors },
  } = methods;
 
  const values = watch();
 
  const updatePassword = useMutation((data: any) => {
    return customInstance({
      url: `/users/${merchantId}`,
      method: "PATCH",
      data,
    });
  });
 
  const onSubmit = (data: IFormInputs) => {
    const { current_password, new_password } = data;
    updatePassword.mutate(
      {
        newPassword: new_password,
        currentPassword: current_password,
      },
      {
        onError: (error: unknown) => {
          const axiosError = error as AxiosError;
          const errorMessage = axiosError.response?.data;
          showMessage("Error", "Please verify the inserted data");
        },
        onSuccess: (res: any) => {
          // empty input fields
          reset();
          showMessage("Success", "Password has been changed");
        },
      },
    );
  };
 
  return { methods, values, errors, getFieldState, onSubmit };
};