All files / src/componentsV2/SideMenu/hooks useAvailableBalance.ts

76.92% Statements 10/13
50% Branches 3/6
100% Functions 3/3
76.92% Lines 10/13

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              11x   11x   11x 3x                 11x   11x 3x 3x       3x     11x        
import { useAppDispatch } from "@redux/hooks";
import { updatePermissions } from "@redux/slices/app";
import { useMemo, useRef } from "react";
import { useGetStats } from "@services/api/products/transactions";
import { StatsType } from "@services/api/api.constant";
 
export function useAvailableBalance() {
  const dispatch = useAppDispatch();
 
  const refValue = useRef(0);
 
  const onError = (err: any) => {
    Iif (err.not_authorized) {
      dispatch(
        updatePermissions({
          merchant_stats: true,
        }),
      );
    }
  };
 
  const { data: stats } = useGetStats(undefined, StatsType.BASE, onError);
 
  const value = useMemo(() => {
    let val = refValue.current;
    Iif (stats) {
      refValue.current = stats.availableBalance / 100;
      val = stats.availableBalance / 100;
    }
    return val;
  }, [stats]);
 
  return {
    availableBalance: !stats ? "-" : value,
  };
}