All files / src/features/Minibuilders/PaymentFormMinibuilder useCreateCampaignFn.ts

30% Statements 3/10
0% Branches 0/2
0% Functions 0/2
30% Lines 3/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 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                                              1x                               1x                             1x                                                                        
import NiceModal from "@ebay/nice-modal-react";
import { PUBLIC_FORM_PATHS } from "@routes/paths";
import { NavigateOptions, useNavigate } from "react-router-dom";
interface TBaseState<T> {
  prevUrl: string;
  id: null | string | number;
  campaignName: T;
}
 
type TCampaignLocationState = {
  // Here you can add typings for location states
  fundraisers: unknown;
  standard: unknown;
  //...
};
 
type TStateMap = {
  [K in keyof typeof canCampaignUseNewBuilder]: TBaseState<K> &
    (K extends keyof TCampaignLocationState
      ? TCampaignLocationState[K]
      : unknown);
};
 
const canCampaignUseNewBuilder = {
  // enabled campaigns
  fundraiser: true,
  event: true,
  standard: true,
  product: true,
  membership: true,
  // not enabled campaigns
  invoice: true,
  sweepstake: true,
  // "manage-money": false,
  "payment-forms": false,
  customers: false,
} as const;
 
// This is because BE sends us the types in singular but in the app we have them in plural in a lot of places
export const campaignMapper = {
  event: "event",
  fundraiser: "fundraiser",
  standard: "standard",
  invoice: "invoice",
  membership: "membership",
  sweepstake: "sweepstake",
  product: "product",
} as const;
 
export type CampaignMapperValues =
  (typeof campaignMapper)[keyof typeof campaignMapper];
 
export type CampaignTypeParam = keyof typeof canCampaignUseNewBuilder;
 
export const useCreateCampaignFn = () => {
  const navigate = useNavigate();
 
  const createCampaignFn = <T extends CampaignTypeParam = CampaignTypeParam>(
    MODAL_ID: string,
    campaignName: T,
    state?: TStateMap[T],
  ): void => {
    if (
      !canCampaignUseNewBuilder[campaignName] 
    ) {
      NiceModal.show(MODAL_ID);
      return;
    }
 
    navigate(
      {
        pathname: `/${PUBLIC_FORM_PATHS.PAY_PRODUCT_BUILDER}`,
        search: `type=${campaignName}`,
      },
      {
        state: {
          prevUrl: location.pathname,
          id: null,
          campaignName,
          ...state,
        } as TStateMap[T],
      } as NavigateOptions,
    );
  };
 
  return {
    createCampaignFn,
    canCampaignUseNewBuilder,
  };
};