All files / src/sections/PayBuilder/hooks useStockLabel.ts

80% Statements 8/10
63.63% Branches 7/11
100% Functions 2/2
80% Lines 8/10

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                    13x           233x   233x   46x               46x         46x 16x     30x                
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useMemo } from "react";
 
interface UseStockLabelProps {
  in_stock?: number | null;
  hideInventory?: boolean;
  hideSoldOut?: boolean;
  showAvailableStock?: boolean;
}
 
export const useStockLabel = ({
  in_stock,
  hideInventory,
  showAvailableStock = false,
}: UseStockLabelProps) => {
 
  const isOutOfStock = in_stock === 0;
 
  return useMemo(() => {
    // we check boolean hideInventory to not affect older products
    Iif (
      hideInventory === true &&
      !isOutOfStock
    ) {
      return "";
    }
 
    // Out of stock
    Iif (isOutOfStock) {
      return "Sold Out";
    }
 
    // Show available stock if allowed
    if (showAvailableStock && in_stock) {
      return `${in_stock} available`;
    }
 
    return "";
  }, [
    in_stock,
    hideInventory,
    showAvailableStock,
 
  ]);
};