All files / src/components/Merchants/CreateMerchant/hooks useUpdateMerchantInfos.tsx

0% Statements 0/18
0% Branches 0/10
0% Functions 0/5
0% Lines 0/18

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                                                                                                                                                                                           
import { showMessage } from "@common/Toast";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useMediaQuery } from "@mui/material";
import { customInstance } from "@services/api";
import theme from "@theme/index";
import { convertPhoneNumber } from "@utils/date.helpers";
import { SubmitHandler } from "react-hook-form";
import { useMutation, useQueryClient } from "react-query";
import { parseAmountValue } from "../utils/functions";
import { QKEY_GET_MERCHANT_BY_ID } from "@constants/queryKeys";
import { encodeString } from "@utils/index";
 
type IFormInputs = {
  merchant_name: string;
  category: string;
  merchant_slug: string;
  avatar_url: string | File;
  billing_descriptor: string;
  customer_service_phone_number: string;
  public_description: string;
  website_url: string;
  estimated_annual_revenue: string;
  average_ticket_amount: string;
  high_ticket_amount: string;
  is_outside_usa: boolean;
  countriesOutside: string;
  parent_slug: string;
  msaRefundPolicy: string;
};
 
export const useUpdateMerchantInfos = ({ action }: { action: () => void }) => {
  const { merchantId } = useGetCurrentMerchantId();
  const queryClient = useQueryClient();
  const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
 
  const updateMerchantMutation = useMutation((data: any) => {
    return customInstance({
      url: `/merchants/${merchantId}`,
      method: "PATCH",
      data,
    });
  });
 
  const onSubmit: SubmitHandler<IFormInputs> = async (data) => {
    const slugBuilder =
      "https://" + data.parent_slug + ".com/" + data.merchant_slug;
    const customData = {
      description: encodeString(data.public_description),
      websiteURL: data.website_url
        ? "https://" + data.website_url
        : slugBuilder,
      servicePhoneNumber: data.customer_service_phone_number
        ? convertPhoneNumber(data.customer_service_phone_number)
        : null,
      annualCreditCardSalesVolume:
        parseAmountValue(data.estimated_annual_revenue) * 100,
      averageTicketAmount: parseAmountValue(data.average_ticket_amount) * 100,
      highTicketAmount: parseAmountValue(data.high_ticket_amount) * 100,
      serviceCountriesOutUSCanada: data.is_outside_usa,
      countriesServicedOutside: data.is_outside_usa
        ? data.countriesOutside
        : "",
    };
 
    updateMerchantMutation.mutate(customData, {
      onError: (error: unknown) => {
        if ((error as any)?.response?.data.input) {
          showMessage(
            "Error",
            (error as any)?.response?.data.input[0].message,
            isDesktop,
          );
          return;
        }
        if ((error as any)?.response?.data?.message) {
          showMessage(
            "Error",
            (error as any)?.response?.data?.message,
            isDesktop,
          );
        }
      },
      onSuccess: async (res: any) => {
        action();
        queryClient.invalidateQueries(QKEY_GET_MERCHANT_BY_ID);
      },
    });
  };
 
  return {
    onSubmit,
  };
};