All files / src/components/VirtualList/hooks useBlockedTransactions.tsx

66.66% Statements 10/15
64.7% Branches 11/17
75% Functions 3/4
71.42% Lines 10/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                7x 346x   346x     20x               346x 68x   18x       18x       346x                     346x        
import { useAppDispatch } from "@redux/hooks";
import { setHasUnseenBlockedTransactions } from "@redux/slices/transactions";
import { getLastBlockedTransaction } from "@services/api/products/transactions";
import { useQuery } from "react-query";
import { QKEY_BLOCKED_TRANSACTIONS } from "@constants/queryKeys";
import { DEFAULT_QUERY_CONFIG } from "@components/VirtualList/hooks/queries";
import { useEffect } from "react";
 
const useCheckLastUnseen = (merchantId: number, enabled = false) => {
  const dispatch = useAppDispatch();
 
  const { data, isLoading } = useQuery(
    [QKEY_BLOCKED_TRANSACTIONS, merchantId],
    async () => {
      return await getLastBlockedTransaction();
    },
    {
      ...DEFAULT_QUERY_CONFIG,
      enabled: Boolean(enabled && merchantId),
    },
  );
 
  useEffect(() => {
    if (!isLoading && enabled) {
      const hasNewThx =
        data?.total > 0 &&
        data?.data?.[0] &&
        data.data[0].firstCheckedAt === null &&
        data.data[0].isBlocked;
      dispatch(setHasUnseenBlockedTransactions(hasNewThx));
    }
  }, [data, isLoading, enabled]);
 
  const setUnseenTransactions = async () => {
    if (!merchantId) return;
    const data = await getLastBlockedTransaction();
    const hasNewThx =
      data?.total > 0 &&
      data?.data?.[0] &&
      data.data[0].firstCheckedAt === null &&
      data.data[0].isBlocked;
    dispatch(setHasUnseenBlockedTransactions(hasNewThx));
  };
 
  return { setUnseenTransactions };
};
 
export default useCheckLastUnseen;