All files / src/features/MerchantPortal/Customer/hooks useStopRecurrence.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 5/5
100% Lines 14/14

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              1x       3x 3x 3x   3x         3x 1x       1x 1x                   1x   1x   1x         1x         3x    
import { useQueryClient } from "react-query";
import { useUpdateRecurring } from "services/api/products/transactions";
import { useGetCurrentMerchantId } from "hooks/common";
import { useModal } from "@ebay/nice-modal-react";
import { showMessage } from "components/common/Toast";
import { QKEY_LIST_CUSTOMERS } from "constants/queryKeys";
 
export const useStopRecurrence = (
  orderRecurringItemID: string,
  merchantAccId?: number,
) => {
  const client = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
  const { remove } = useModal();
 
  const { mutateAsync, isLoading: isLoadingUpdate } = useUpdateRecurring(
    orderRecurringItemID,
    merchantAccId || merchantId,
  );
 
  const stopRecurrence = () => {
    mutateAsync(
      { isCancelled: true },
      {
        onSuccess: () => {
          const queryCache = client.getQueryCache();
          const queryKeysToInvalidate = [
            ...queryCache.findAll("get-all-transactions"),
            ...queryCache.findAll("get-fundraiser-transactions-by-id"),
            ...queryCache.findAll("get-events-transactions-by-id"),
            ...queryCache.findAll("get-invoices-transactions-by-id"),
            ...queryCache.findAll("get-memberships-transactions-by-id"),
            ...queryCache.findAll("get-sweepstakes-transactions-by-id"),
            ...queryCache.findAll(QKEY_LIST_CUSTOMERS),
            ...queryCache.findAll("get-transaction-history"),
            ...queryCache.findAll("customer"),
          ].map((query) => query.queryKey);
 
          queryKeysToInvalidate.forEach((key) => client.invalidateQueries(key));
 
          showMessage(
            "Success",
            "Your recurring payments have been successfully canceled.",
            false,
          );
          remove();
        },
      },
    );
  };
  return { stopRecurrence, isLoadingUpdate };
};