All files / react/components/Header HeaderUI.jsx

4.25% Statements 2/47
0% Branches 0/24
0% Functions 0/17
4.76% Lines 2/42

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                            2x                                                                                                                                                                                                                                                                             2x                            
import { useState, useEffect } from "react";
import PropTypes from "prop-types";
import HeaderAccountMenu from "./HeaderAccountMenu.jsx";
import HeaderCancel from "./HeaderCancel.jsx";
import HeaderContainer from "./HeaderContainer";
import HeaderMenuItem from "./HeaderMenuItem";
import HeaderMobileButton from "./HeaderMobileButton";
import ImpersonatorBanner from "./ImpersonatorBanner";
import PreviewBanner from "./PreviewBanner.jsx";
import HeaderSearchBar from "../HeaderSearchBar";
import HelpIcon from "./HelpIcon";
import { HeaderStateProvider, useWindowWidth } from "./hooks";
import defaultContent from "./default-content.json";
 
const HeaderUI = ({
  handleCancel = () => {},
  includeSkipToSidebar = false,
  isDevPre = false,
  showCancelButton = false,
  skipToContentId,
  RouterLink = null,
  isIESupportPage = false,
}) => {
  const [isMobileMenuExpanded, setIsMobileMenuExpanded] = useState(false);
  const [headerContent, setHeaderContent] = useState({});
  const windowWidth = useWindowWidth();
 
  const mapContentToRows = (config) => {
    // Append transposed array to content
    return config?.map((section) => {
      // Only header items with multiple columns need to be transposed
      // Single column menus' tab order will remain the same
      if (section.columns?.length > 1) {
        const { columns } = section;
        return {
          ...section,
          rows: columns[0].map((_, colIndex) =>
            columns.map((row) => row[colIndex]),
          ),
        };
      }
      return { ...section, rows: section.columns };
    });
  };
 
  // execute on init render
  useEffect(() => {
    let cancelRequest = false;
    const { origin } = window.location;
    let navMenuItems;
    async function getHeaderConfig() {
      try {
        const response = await fetch(`${origin}/config/header`);
        const data = await response.json();
        navMenuItems = data.data;
      } catch (error) {
        navMenuItems = { content: defaultContent };
      }
      if (!cancelRequest) setHeaderContent(navMenuItems);
    }
    getHeaderConfig();
    return () => {
      cancelRequest = true;
    };
  }, []);
 
  const content = mapContentToRows(headerContent?.content);
  const sharedHeaderProps = {
    skipToContentId,
    includeSkipToSidebar,
    showSearchBar: headerContent.showSearchBar,
  };
 
  const displayCancelContent = () => (
    <HeaderContainer {...sharedHeaderProps} showCancelButton>
      <HeaderCancel handleCancel={handleCancel} />
    </HeaderContainer>
  );
 
  const displayDevPreContent = () => (
    <HeaderContainer
      {...sharedHeaderProps}
      isIESupportPage={isIESupportPage}
      isDevPre={isDevPre}
    >
      <nav
        aria-label="Primary navigation"
        hidden={!isMobileMenuExpanded && windowWidth < 768}
      >
        <ul className="navigation-menu">
          <HeaderAccountMenu isDevPre />
        </ul>
      </nav>
    </HeaderContainer>
  );
 
  const displayHeaderContent = () => (
    <>
      <HeaderContainer {...sharedHeaderProps} isIESupportPage={isIESupportPage}>
        <HelpIcon />
        {headerContent.showSearchBar === "true" && windowWidth < 768 ? (
          <div style={{ width: "100%" }}>
            <HeaderSearchBar />
          </div>
        ) : null}
        <HeaderMobileButton
          handleClick={() => setIsMobileMenuExpanded(!isMobileMenuExpanded)}
          isMobileMenuExpanded={isMobileMenuExpanded}
        />
        <nav
          aria-label="Primary navigation"
          hidden={!isMobileMenuExpanded && windowWidth < 768}
        >
          <ul className="navigation-menu navigation-new-style">
            {content.map((header) => (
              <HeaderMenuItem
                handleClick={() => {
                  setIsMobileMenuExpanded(!isMobileMenuExpanded);
                }}
                isMobileMenuExpanded={isMobileMenuExpanded}
                key={header.name}
                {...header}
              />
            ))}
            <HeaderAccountMenu
              handleClick={() => setIsMobileMenuExpanded(!isMobileMenuExpanded)}
            />
          </ul>
        </nav>
      </HeaderContainer>
      <ImpersonatorBanner />
      <PreviewBanner />
    </>
  );
 
  const displayContent = () => {
    if (showCancelButton) return displayCancelContent();
    if (isDevPre) return displayDevPreContent();
    if (content?.length) return displayHeaderContent();
    return null;
  };
 
  return (
    <HeaderStateProvider RouterLink={RouterLink} headerContent={headerContent}>
      {displayContent()}
    </HeaderStateProvider>
  );
};
 
HeaderUI.propTypes = {
  handleCancel: PropTypes.func,
  includeSkipToSidebar: PropTypes.bool,
  isDevPre: PropTypes.bool,
  performanceYear: PropTypes.number,
  showCancelButton: PropTypes.bool,
  showPhysicianCompareLink: PropTypes.bool,
  showFacilityBasedPreviewLink: PropTypes.bool,
  skipToContentId: PropTypes.string,
  RouterLink: PropTypes.func,
  isIESupportPage: PropTypes.bool,
};
 
export default HeaderUI;