All files / src/hooks/enterprise-api/account useUpdateMerchant.tsx

46% Statements 23/50
20% Branches 5/25
41.66% Functions 5/12
48.88% Lines 22/45

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                              4x 85x 85x   85x 85x   85x 1x             85x                                           85x 1x           1x         85x                                                       85x             1x 1x 1x 1x 1x 1x         85x     85x                 4x                        
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { QKEY_GET_MERCHANT_BY_ID } from "@constants/queryKeys";
import { useUploadCustomerFile } from "@hooks/upload-api/useUploadCustomerFile";
import { getMerchantErrorMessage } from "@components/Merchants/CreateMerchantPanel/helpers";
import { useState } from "react";
 
type TUpdateMerchantOptions = {
  onSuccess?: (res: any) => void;
  onError?: VoidFunction;
  isDelete?: boolean;
};
 
const useUpdateMerchant = (accID: number) => {
  const [isUpdatingMerchant, setUpdatingMerchantStatus] = useState(false);
  const { handlePostImage } = useUploadCustomerFile();
 
  const queryClient = useQueryClient();
  const { isDesktopView } = useCustomTheme();
 
  const updateMerchantMutation = useMutation((payload: any) => {
    return customInstance({
      url: `/merchants/${accID}`,
      method: "PATCH",
      data: payload,
    });
  });
 
  const APIErrorHandler = (error: unknown) => {
    if ((error as any)?.response?.data.input) {
      const input = (error as any)?.response?.data.input[0];
 
      showMessage(
        "Error",
        getMerchantErrorMessage(input?.param, input?.code, input?.message) ||
          `${getErrorParamName(input.param)} is not valid`,
        isDesktopView,
      );
      return;
    }
    if ((error as any)?.response?.data?.message) {
      showMessage(
        "Error",
        (error as any)?.response?.data?.message,
        isDesktopView,
      );
    }
    setUpdatingMerchantStatus(false);
  };
 
  const updateMerchant = (payload: any, options: TUpdateMerchantOptions) => {
    updateMerchantMutation.mutate(payload, {
      onError: (error: unknown) => {
        APIErrorHandler(error);
        if (options?.onError) options.onError();
      },
      onSuccess: (res: any) => {
        Eif (options?.onSuccess) options.onSuccess(res);
      },
    });
  };
 
  const updateMerchantImage = (
    url: string | null,
    options: TUpdateMerchantOptions,
  ) => {
    updateMerchantMutation.mutate(
      { imageURL: url },
      {
        onError: APIErrorHandler,
        onSuccess: (res: any) => {
          setUpdatingMerchantStatus(false);
          queryClient.setQueriesData(
            [QKEY_GET_MERCHANT_BY_ID, accID],
            (oldData) => {
              if (!oldData) return oldData;
              return {
                ...oldData,
                imageURL: url,
              };
            },
          );
          if (options?.onSuccess) options?.onSuccess(res);
          if (options?.isDelete) {
            showMessage("Success", "Profile image deleted");
          }
        },
      },
    );
  };
  const uploadImage = async ({
    file,
    onSuccess,
  }: {
    file: File;
    onSuccess: (res: any) => void;
  }) => {
    Iif (!file) return;
    setUpdatingMerchantStatus(true);
    const url = await handlePostImage({ file });
    Eif (!url) {
      setUpdatingMerchantStatus(false);
      return;
    }
    updateMerchantImage(url, { onSuccess });
  };
 
  const removeImage = (options: TUpdateMerchantOptions) =>
    updateMerchantImage(null, options);
 
  return {
    updateMerchant,
    uploadImage,
    removeImage,
    isLoading: updateMerchantMutation.isLoading,
    isUpdatingMerchant: updateMerchantMutation.isLoading || isUpdatingMerchant,
  };
};
 
const getErrorParamName = (param: string) => {
  switch (param) {
    case "imageURL":
      return "Avatar image";
    case "billingDescriptor":
      return "Billing Descriptor";
    default:
      return "Input";
  }
};
 
export default useUpdateMerchant;