All files / src/components/Merchants/MerchantPreview/RiskProfile/hooks useRiskProfile.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 3/3
100% Lines 9/9

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                                  21x           21x 24x           21x         182x     24x 18x                 182x       182x              
import { customInstance } from "@services/api";
import { useState } from "react";
import { useQuery } from "react-query";
import { TMerchantRiskProfile } from "../types";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
 
type Props = {
  profileId: number;
  merchantId: number;
  // Optional gate: callers that only need the profile conditionally (e.g. the
  // RM001 override, which is behind a feature flag) can pass `false` to skip
  // the fetch entirely and avoid a request on every side panel render.
  enabled?: boolean;
};
 
type RiskProfileTabsType = "Activity" | "Triggers" | "Transactions";
 
const RiskProfileTabs = {
  ACTIVITY: "Activity" as RiskProfileTabsType,
  TRIGGERS: "Triggers" as RiskProfileTabsType,
  TRANSACTIONS: "Transactions" as RiskProfileTabsType,
};
 
const getMerchantRiskProfile = (id: number, merchantId: number) => {
  return customInstance({
    url: `/merchants/${merchantId}/risk/merchant-profiles/${id}`,
    method: "GET",
  });
};
 
export const useRiskProfile = ({
  profileId,
  merchantId,
  enabled = true,
}: Props) => {
  const { data, isLoading } = useQuery(
    [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.RISK_PROFILE, profileId, merchantId],
    async () => {
      const riskProfile = await getMerchantRiskProfile(profileId, merchantId);
      return riskProfile as TMerchantRiskProfile;
    },
    {
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      enabled: enabled && profileId !== undefined && !!profileId && !!merchantId,
    },
  );
 
  const [activeTab, setActiveTab] = useState<RiskProfileTabsType>(
    RiskProfileTabs.ACTIVITY,
  );
 
  return {
    data,
    isLoading,
    activeTab,
    setActiveTab,
  };
};