All files / src/features/Merchants/MerchantSidePanel/Modals/MatchReport/hooks useSubmitMatchReport.tsx

82.14% Statements 23/28
52.94% Branches 9/17
87.5% Functions 7/8
85.18% Lines 23/27

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                                      4x       28x 28x 28x   28x         1x             1x 1x             1x     28x       1x 1x 1x 1x         28x   1x                                 1x     1x 1x             28x 1x 1x             28x              
import {
  ReportFormFields,
  UseSubmitMatchReportType,
  ReportType,
} from "../types";
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast";
import { useUploadFiles } from "@hooks/upload-api/uploadHooks";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { isEmpty } from "lodash";
 
type MutationRequestParams = {
  merchantID: number;
  files: ReportFormFields["files"];
  data: Omit<ReportFormFields, "files">;
};
 
const useSubmitMatchReport = ({
  merchantID,
  onSubmitSuccess,
}: UseSubmitMatchReportType) => {
  const queryClient = useQueryClient();
  const { isDesktopView } = useCustomTheme();
  const { handleUpload: handleUploadEdit, isLoading } = useUploadFiles();
 
  const uploadFiles = async (
    merchantID: number,
    reportID: number,
    files: any,
  ) => {
    const uploadRes: string[] | "upload_failed" = await handleUploadEdit({
      list: files,
      attachmentType: "underwriting_match_report",
      merchantId: merchantID,
      resourceID: reportID,
    });
 
    if (uploadRes === "upload_failed")
      showMessage("Error", "Evidence upload failed.", isDesktopView);
    else Eif (uploadRes)
      queryClient.invalidateQueries([
        MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_FILES,
        merchantID,
      ]);
 
    onSubmitSuccess();
  };
 
  const handleEvidenceUpload = (
    responseData: ReportType,
    params: MutationRequestParams,
  ) => {
    const { merchantID, files = [] } = params;
    Eif (responseData && files.length) {
      uploadFiles(merchantID, responseData.ID, files);
      return;
    }
    onSubmitSuccess();
  };
 
  const createMatchReportMutation = useMutation(
    ({ data, merchantID }: MutationRequestParams) => {
      return customInstance({
        url: `/merchants/${merchantID}/match-reports`,
        method: "POST",
        data,
      });
    },
    {
      onSuccess: handleEvidenceUpload,
      onError: (error: any) => {
        showMessage(
          "Error",
          error?.message || error?.response?.status,
          true,
          "Something went wrong...",
        );
      },
      onSettled: (data) => {
        queryClient.setQueryData(
          [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_MATCH, merchantID],
          (oldData: any) => {
            Iif (isEmpty(oldData) || !oldData) return [data];
            return [data, ...oldData];
          },
        );
      },
    },
  );
 
  const handleSubmit = async (formData: ReportFormFields) => {
    const { files, ...postData } = formData;
    createMatchReportMutation.mutate({
      merchantID,
      files: files,
      data: postData,
    });
  };
 
  return {
    handleSubmit,
    isSubmitting: createMatchReportMutation.isLoading || isLoading,
  };
};
 
export default useSubmitMatchReport;