All files / src/sections/PayBuilder/components/hooks/usePayBuilder index.ts

72.97% Statements 54/74
41.79% Branches 28/67
89.47% Functions 17/19
74.62% Lines 50/67

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                              246x 246x   150x               161x           161x 161x   161x 161x 60x     161x   161x   161x 131x         161x 90x       161x 90x     161x 60x 60x       161x                 102x         102x 98x 98x 8x 8x   8x     90x       161x   1x                           1x                           1x                             161x 161x 161x 161x   161x 161x   161x                     531x 171x 133x 133x       360x               360x                                           360x           398x     161x                       63x            
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { isEmpty, isEqual } from "lodash";
import { useEffect, useMemo, useRef, useState } from "react";
import { generateStepsArray, isEventNextDisabled } from "./utils";
import useCheckFormType from "../useCheckFormType";
import { FormType } from "@sections/PayBuilder/utils";
import { useLocation, useParams } from "react-router-dom";
import { useAppSelector } from "@redux/hooks";
import { selectPaymentFormID } from "@redux/slices/products";
 
type TStepsArray = Array<{
  label: string;
  id: string;
}>;
function findIdx(stepsArray: TStepsArray, lastStepCompleted: any) {
  const item = stepsArray.find((x) => x.id === lastStepCompleted);
  const index = stepsArray.findIndex((element) => element.id === item?.id);
 
  return index;
}
 
export default function usePayBuilder({
  isLaunchVisible,
}: {
  isLaunchVisible?: boolean;
}) {
  const { formType } = useCheckFormType();
 
  const {
    steps: stepsArray,
    formStep,
    previewStep,
  } = generateStepsArray(isLaunchVisible, formType);
  const screenSizes = useCustomThemeV2();
  const [lastStepCompleted, setLastStepCompleted] =
    useState("product_creation");
  const [activeStepIndex, setActiveStepIndex] = useState(() => {
    return Math.max(findIdx(stepsArray, lastStepCompleted), 0);
  });
 
  const screenSizesRef = useRef(screenSizes);
 
  const [screenStates, setScreenStates] = useState(screenSizes);
 
  useEffect(() => {
    Eif (isEqual(screenSizes, screenSizesRef.current)) return;
    screenSizesRef.current = screenSizes;
    setScreenStates(screenSizes);
  }, [screenSizes]);
 
  const isCompleted = useMemo(
    () => findIdx(stepsArray, lastStepCompleted),
    [lastStepCompleted],
  );
 
  useEffect(() => {
    goToStep({ index: isCompleted });
  }, [lastStepCompleted]);
 
  useEffect(() => {
    return () => {
      setLastStepCompleted("product_creation");
    };
  }, []);
 
  const goToStep = ({
    index,
    offSet,
    free,
  }: {
    index?: number;
    offSet?: 1 | -1 | number;
    free?: boolean;
  }) => {
    setActiveStepIndex((current) => {
      //yuo cannot jump between step if the about step is not completed by adding a heading
      //isCompleted represents the index of the last step that was completed
      //a step is considered completed if the user pressed Next button
      //if the user jumps back to an already completed step and do changes, and press next, do not set it as last step completed.
      if (index !== undefined && free) return index;
      Iif (index !== undefined && index < isCompleted) return index;
      if (offSet !== undefined) {
        Eif (offSet === 1 && activeStepIndex >= isCompleted) {
          setLastStepCompleted(stepsArray[current + offSet]?.id);
        }
        return current + offSet;
      }
 
      return current;
    });
  };
 
  const manageScreenActions = {
    setDeskTopView: () =>
      setScreenStates({
        isMobileView: false,
        isTabletView: false,
        isDesktopView: true,
        isMobileDevice: false,
        isSmallDesktop: true,
        isNarrowView: false,
        isWideView: false,
        isMediumView: false,
        isLargeTabletView: false,
        isLargeView: false,
        isMediumDesktop: false,
      }),
    setTabletView: () =>
      setScreenStates({
        isMobileView: false,
        isTabletView: true,
        isDesktopView: false,
        isMobileDevice: false,
        isSmallDesktop: false,
        isNarrowView: false,
        isWideView: false,
        isMediumView: false,
        isLargeTabletView: false,
        isLargeView: false,
        isMediumDesktop: false,
      }),
    setMobileView: () =>
      setScreenStates({
        isMobileView: true,
        isTabletView: false,
        isDesktopView: false,
        isMobileDevice: true,
        isSmallDesktop: false,
        isNarrowView: false,
        isWideView: false,
        isMediumView: false,
        isLargeTabletView: false,
        isLargeView: false,
        isMediumDesktop: false,
      }),
  };
 
  const { id, editID } = useParams();
  const location = useLocation();
  const peekedFormId = useAppSelector(selectPaymentFormID);
  const duplicateInvoiceId = location?.state?.duplicateInvoiceId;
  const editFormId =
    id || location.state?.id || peekedFormId || editID || duplicateInvoiceId;
  const isEdit = !!editFormId;
 
  const isNextDisabled = ({
    values,
    errors,
    isValid,
    isLoading,
  }: {
    values: any;
    errors: any;
    isValid: boolean;
    isLoading: boolean;
  }) => {
    if (formType === FormType.EVENTS) {
      if (activeStepIndex === 0) {
        const dateAndLocation = isEventNextDisabled({ values, errors, isEdit });
        return (
          !values.About?.heading || !!errors.About?.heading || dateAndLocation
        );
      }
    } else Iif (formType === FormType.FUNDRAISERS) {
      if (activeStepIndex === 0)
        return !values.About?.heading || !!errors.About?.heading;
      if (activeStepIndex === 1) return false;
      if (!values?.Donations?.allowCustomAmount) return false;
      if (activeStepIndex === 2)
        return !isEmpty(errors?.Donations?.optionalAmounts);
      return !isValid || isLoading || !isEmpty(errors);
    } else Iif (formType === FormType.SWEEPSTAKE) {
      if (activeStepIndex === 2) {
        return (
          isLoading ||
          !isValid ||
          !!errors.Entries?.optionalAmounts?.find?.(
            (x: any) => x?.ref.name === "Entries.optionalAmounts.0",
          ) ||
          !!errors.Entries?.optionalAmounts?.find?.(
            (x: any) => x?.ref.name === "Entries.optionalAmounts.1",
          )
        );
      } else {
        return (
          !values.About?.heading ||
          !!errors.About?.heading ||
          !values.About?.sweepstakeEndAtDate ||
          !!errors.About?.sweepstakeEndAtDate ||
          !values.About?.sweepstakeEndAtTime ||
          !!errors.About?.sweepstakeEndAtTime
        );
      }
    } else Iif (formType === FormType.INVOICES) {
      if (activeStepIndex === 0) {
        const customerId = values.About.customerId;
        return isLoading || !customerId || !isValid || isEmpty(values.Items);
      }
    }
    return isLoading || !isValid;
  };
 
  return {
    formStep,
    previewStep: previewStep[activeStepIndex],
    stepsArray,
    activeStepIndex,
    activeItem: stepsArray[activeStepIndex],
    goToStep,
    isLastStep: activeStepIndex === stepsArray.length - 1,
    lastStepCompletedIndex: isCompleted,
    screenStates,
    manageScreenActions,
    setLastStepCompleted: (index: number) => {
      setLastStepCompleted(stepsArray[index]?.id);
    },
    lastStepCompleted,
    isNextDisabled,
  };
}