All files / src/features/Merchants/MerchantsTable/hooks useWatchList.tsx

32.25% Statements 10/31
0% Branches 0/4
16.66% Functions 1/6
32.25% Lines 10/31

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                        33x 4003x 4003x 4003x   4003x                       4003x                                       4003x                               4003x       4003x       4003x        
import { showMessage } from "@common/Toast";
import { useUpdateMerchantsLists } from "@components/Merchants/MerchantPreview/hooks/useGetTeamMembers";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
  QKEY_LIST_MERCHANTS,
} from "@constants/queryKeys";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { customInstance } from "@services/api";
import { checkPortals } from "@utils/routing";
import { useQueryClient } from "react-query";
 
const useWatchlist = (queryKey?: string) => {
  const { updateData, revertChange } = useUpdateMerchantsLists(queryKey);
  const queryClient = useQueryClient();
  const { isEnterpriseTable } = checkPortals();
 
  const handleInvalidation = (id: number) => {
    queryClient.invalidateQueries(QKEY_LIST_ENTERPRISE_STATS);
    queryClient.invalidateQueries([
      MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
      id,
    ]);
    if (!isEnterpriseTable) {
      queryClient.invalidateQueries(QKEY_LIST_MERCHANTS);
      queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
    }
  };
 
  const editMerchantWatchlist = async (
    id: number,
    name: string,
    add = true,
  ) => {
    try {
      await queryClient.cancelQueries(queryKey);
      updateData(id, { watchlist: add }, { updateMerchantPreview: true });
      await customInstance({
        url: `/merchants/${id}/underwriting`,
        method: "PATCH",
        data: { watchlist: add },
      });
      handleInvalidation(id);
    } catch (error) {
      revertChange();
      showMessage("Error", "Whoops.. an error occured. Please try again");
    }
  };
 
  const handleMerchantWatchlist = async (id: number, add = true) => {
    try {
      await queryClient.cancelQueries(queryKey);
      updateData(id, { watchlist: add }, { updateMerchantPreview: true });
      await customInstance({
        url: `/merchants/${id}/watchlist`,
        method: "POST",
        data: { watchlist: add },
      });
      handleInvalidation(id);
    } catch (error) {
      revertChange();
      showMessage("Error", "Whoops.. an error occured. Please try again");
    }
  };
 
  const addMerchantToWatchlist = (merchantId: number, name: string) => {
    handleMerchantWatchlist(merchantId);
  };
 
  const removeMerchantFromWatchlist = (merchantId: number, name?: string) => {
    handleMerchantWatchlist(merchantId, false);
  };
 
  return { addMerchantToWatchlist, removeMerchantFromWatchlist };
};
 
export default useWatchlist;