All files / src/components/Merchants/MerchantPreview/hooks useResyncMSP.ts

93.33% Statements 14/15
0% Branches 0/2
66.66% Functions 2/3
93.33% Lines 14/15

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            42x 2191x   2191x 2191x                 2191x 2x 2x 2x 1x       1x 1x   1x   2x     2191x    
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { useState } from "react";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
 
export const useResyncMSP = (merchantId: number, legalEntityId?: number) => {
  const [isLoading, setIsLoading] = useState(false);
 
  const queryClient = useQueryClient();
  const { mutateAsync: ResyncMSPAsync } = useMutation(() => {
    return customInstance({
      url: legalEntityId
        ? `/merchants/${merchantId}/legal-entities/${legalEntityId}/msp`
        : `/merchants/${merchantId}/msp`,
      method: "PUT",
    });
  });
 
  const handleResyncMSP = async () => {
    setIsLoading(true);
    try {
      await ResyncMSPAsync();
      await queryClient.invalidateQueries([
        MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
        merchantId,
      ]);
      queryClient.invalidateQueries("get-merchant-msp-status");
      showMessage("Info", "Synchronization complete");
    } catch (error) {
      showMessage("Error", "We have some issues with the synchronization.");
    } finally {
      setIsLoading(false);
    }
  };
  return { handleResyncMSP, isLoading };
};