FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

34 line
1.0 KiB

  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { STOCK_TAKE_QTY_GAP_WARN_PERCENT } from "./stockTakeQtyGapWarning";
  4. import {
  5. fetchStockTakeQtyGapWarnPercent,
  6. STOCK_TAKE_QTY_GAP_SETTING_EVENT,
  7. } from "./qtyGapWarnSettingClient";
  8. /** Warning threshold from settings (`STOCK_TAKE.qtyGapWarnPercent`), default 50. */
  9. export function useStockTakeQtyGapWarnPercent(): number {
  10. const [percent, setPercent] = useState(STOCK_TAKE_QTY_GAP_WARN_PERCENT);
  11. useEffect(() => {
  12. let cancelled = false;
  13. fetchStockTakeQtyGapWarnPercent()
  14. .then((n) => {
  15. if (!cancelled) setPercent(n);
  16. })
  17. .catch(() => {});
  18. const onChange = (event: Event) => {
  19. const next = (event as CustomEvent<number>).detail;
  20. if (Number.isFinite(next)) setPercent(next);
  21. };
  22. window.addEventListener(STOCK_TAKE_QTY_GAP_SETTING_EVENT, onChange);
  23. return () => {
  24. cancelled = true;
  25. window.removeEventListener(STOCK_TAKE_QTY_GAP_SETTING_EVENT, onChange);
  26. };
  27. }, []);
  28. return percent;
  29. }