All files / src/shared/GiveFilter/hooks useFormattedFilters.ts

100% Statements 14/14
100% Branches 13/13
100% Functions 5/5
100% Lines 13/13

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                          263x 713x 1064x   713x   713x 713x         713x               263x           22x 5x       22x     22x     22x          
import { useAppSelector } from "@redux/hooks";
import { selectFiltersObject } from "@redux/slices/dynamicFilterSlice";
import {
  CLOSED_MERCHANT_STATUS,
  customAmountArrayKey,
  endpointNames,
  urlFilterFormat,
} from "@shared/GiveFilter/constants";
import {
  countNonEmptyValues,
  formatArrayFilter,
} from "@shared/GiveFilter/utils";
 
export const useFormattedFilters = ({ queryKey }: { queryKey: string }) => {
  const filterObject = useAppSelector((state) =>
    selectFiltersObject(state, queryKey),
  ) as any;
  const filterKey = urlFilterFormat as keyof typeof filterObject;
  //we deconstruct the watchlist bc is passed as a top-level query param, not nested inside the filter param
  const { watchlist, ...objWithoutWatchlist } = filterObject || {};
  const filterConst = countNonEmptyValues(filterObject, [
    customAmountArrayKey,
    urlFilterFormat,
  ]);
 
  return {
    formattedFilterString: (objWithoutWatchlist?.[filterKey] as string) || "",
    filterObject,
    filterConst,
    watchlistFilter: watchlist,
  };
};
 
export const getFormattedMerchantStateName = (
  stateNames: string[],
  { excludeClosed }: { excludeClosed?: boolean } = {},
) => {
  // A closed selection can outlive the portal it was made in: the filter slice
  // is only reset on LOGOUT, not on an account switch.
  const states = excludeClosed
    ? stateNames?.filter((item) => item !== CLOSED_MERCHANT_STATUS)
    : stateNames;
 
  const RFRStateName =
    states?.filter((item) => item === "ready_for_review") || [];
 
  const sponsorStateNames =
    RFRStateName?.length > 0 ? ["sponsor_mid_issue"] : [];
 
  // stateNames can be undefined
  return formatArrayFilter({
    array: states ? [...states, ...sponsorStateNames] : states,
    key: endpointNames?.stateName,
  });
};