All files / src/components/ProfilePage/BusinessProfileSetup/hooks usePercentageUpdate.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 4/4
100% Lines 12/12

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        19x           62x     62x   62x 23x 23x 23x     62x 62x 16x       62x 7x        
import { useCalculatePercentage } from "@common/SignUp/useCalculatePercentage";
import { cloneDeep, isEqual } from "lodash";
import { useEffect, useRef } from "react";
 
const usePercentageUpdate = <TFormInputs extends object>(
  values: any,
  dirtyFields: any,
  schema: any,
  updateStatusBar: (v: number) => void,
) => {
  const { calculatePercentageNested } = useCalculatePercentage({
    isEdit: false,
  });
  const saveOnUnmount = useRef<TFormInputs>();
 
  const calcPercentage = async () => {
    saveOnUnmount.current = cloneDeep(values);
    const value = await calculatePercentageNested(schema, values);
    updateStatusBar(value);
  };
 
  useEffect(() => {
    if (!isEqual(values, saveOnUnmount.current)) {
      calcPercentage();
    }
  }, [values, dirtyFields]);
 
  useEffect(() => {
    calcPercentage();
  }, []);
};
 
export default usePercentageUpdate;