All files / src/features/Merchants/MerchantSidePanel/UnderwritingTasks/hooks useTaskModalData.tsx

78.57% Statements 11/14
54.16% Branches 13/24
66.66% Functions 2/3
76.92% Lines 10/13

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                          8x                 85x   85x                   85x                                 85x   85x 33x                                         85x                   85x                     85x            
import { useMerchantDataPreview } from "@components/Merchants/MerchantPreview/hooks/useMerchantDataPreview";
import { useQuery } from "react-query";
import {
  GENERAL_STALE_TIME_15,
  UNDERWRITING_CHALLENGE_API_KEYS,
} from "@features/Merchants/MerchantSidePanel/constants";
import { customInstance } from "@services/api";
import { useTaskActions } from "@features/Merchants/MerchantSidePanel/ApprovalFlowPanel/hooks/useTaskActions";
import { useMemo } from "react";
import { getTaskModalArgs } from "@features/Merchants/MerchantSidePanel/ApprovalFlowPanel/utils";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { isEmpty } from "lodash";
 
export const useTaskModalData = ({
  merchantID = 0,
  taskID = 0,
  enabled = false,
}: {
  merchantID?: number;
  taskID?: number;
  enabled?: boolean;
}) => {
  const { isNewConversationsEnabled } = useGetFeatureFlagValues();
  const { data, merchant, isLoading, isMerchantFileLoading } =
    useMerchantDataPreview({
      id: merchantID,
      isMerchantFileOpen: enabled && !!merchantID && !!taskID,
      enabled: enabled && !!merchantID && !!taskID,
    });
 
  const {
    data: taskData,
    isLoading: isTaskLoading,
    error,
  } = useQuery(
    [...UNDERWRITING_CHALLENGE_API_KEYS.GET_SINGLE_TASK, merchantID, taskID],
    async () => {
      const response = await customInstance({
        url: `/merchants/${merchantID}/tasks/${taskID}`,
      });
      return response;
    },
    {
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      enabled: Boolean(merchantID && taskID) && enabled,
      staleTime: GENERAL_STALE_TIME_15,
      retry: 1,
    },
  );
 
  const isProvider = merchant?.type === "enterprise";
 
  const usedAPIData: any = useMemo(() => {
    if (!taskData || !data) return {};
    else
      Ereturn {
        isKYBView: ["ready_for_kyb", "back_to_kyb"].includes(
          data?.status?.statusName,
        ),
        ...getTaskModalArgs({
          merchantData: data,
          taskData,
          isNewConversationsEnabled,
          isProvider,
        }),
      };
  }, [taskData, data]);
 
  const {
    handleComplete,
    handleReadyForKYB,
    handleReadyForUnderwriting,
    handleInProgress,
    handleUpdateAttention,
  } = useTaskActions(
    merchantID,
    taskID,
    taskData?.status,
    taskData?.slug,
    taskData?.name,
    isEmpty(usedAPIData) ? {} : usedAPIData.merchantData,
    taskData?.requestAttention,
  );
 
  const modalData = {
    ...(usedAPIData || {}),
    actions: {
      handleComplete,
      handleReadyForKYB,
      handleReadyForUnderwriting,
      handleInProgress,
      handleUpdateAttention,
    },
  };
 
  return {
    modalData,
    isError: !!error,
    isLoading: isLoading || isMerchantFileLoading || isTaskLoading,
  };
};