All files / react-app/src/utils formOrigin.ts

92.85% Statements 13/14
81.25% Branches 13/16
100% Functions 1/1
92.85% Lines 13/14

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        73x   73x   73x   73x   73x                                 73x 6x   6x           6x 2x               4x 2x               2x        
import { Authority } from "shared-types/authority";
import { getDashboardTabForAuthority } from "./crumbs";
 
/** Constant key for accessing origin in query string. */
export const ORIGIN = "origin";
/** Constant key for `dashboard` origin. */
export const DASHBOARD_ORIGIN = "dashboard";
/** Constant key for `details` origin. */
export const DETAILS_ORIGIN = "details";
/** Constant key for `spa` origin. */
export const SPA_SUBMISSION_ORIGIN = "spas";
/** Constant key for `waivers` origin. */
export const WAIVER_SUBMISSION_ORIGIN = "waivers";
 
type GetFormOriginArgs = {
  id?: string;
  authority?: Authority;
};
 
type GetFormOrigin = (args?: GetFormOriginArgs) => {
  pathname: string;
  search?: string;
};
 
/** Get the form's origin pathname
 *
 * NOTE: `getFormOrigin` should _not_ be used within a component's lifecycle as it may result in stale data.
 * Instead, call within functions
 */
export const getFormOrigin: GetFormOrigin = ({ id, authority } = {}) => {
  const origin = new URLSearchParams(window.location.search).get(ORIGIN) ?? DASHBOARD_ORIGIN;
 
  Iif (origin === DETAILS_ORIGIN && id && authority) {
    return {
      pathname: `/${origin}/${encodeURIComponent(authority)}/${encodeURIComponent(id)}`,
    };
  }
 
  if (origin === DASHBOARD_ORIGIN && authority) {
    return {
      pathname: `/${origin}`,
      search: new URLSearchParams({
        tab: getDashboardTabForAuthority(authority),
      }).toString(),
    };
  }
 
  if (origin === SPA_SUBMISSION_ORIGIN || origin === WAIVER_SUBMISSION_ORIGIN) {
    return {
      pathname: `/${DASHBOARD_ORIGIN}`,
      search: new URLSearchParams({
        tab: origin,
      }).toString(),
    };
  }
 
  return {
    pathname: `/${origin}`,
  };
};