All files / components/Footer Footer.server.ts

0% Statements 0/107
0% Branches 0/1
0% Functions 0/1
0% Lines 0/107

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                                                                                                                                                                                                                       
import gql from "graphql-tag";
import { print as printQuery } from "graphql";

import type { ContentfulClient } from "$lib/services/server/contentful";
import { footerNavTestContent } from "./__tests__/FooterTestContent";

import type { FooterNavQuery } from "./$queries.generated";
import type { NavLinkType, NavMenuType } from "$lib/components/Header/Nav/types";
import type { PageMetadataMap } from "$lib/loadPageMetadataMap";

const footerNavQuery = gql`
  query FooterNav($preview: Boolean = false) {
    menuCollection(limit: 1, where: { menuType: "Footer Menu" }, preview: $preview) {
      items {
        childrenCollection(limit: 5) {
          items {
            ... on Menu {
              __typename
              sys {
                id
              }
              title
              menuType
              childrenCollection(limit: 10) {
                items {
                  ... on MenuItem {
                    __typename
                    sys {
                      id
                    }
                    title
                    internalLink {
                      ... on PageMetadata {
                        sys {
                          # eslint-disable-next-line @graphql-eslint/selection-set-depth
                          id
                        }
                      }
                    }
                    externalLink
                  }
                }
              }
            }
          }
        }
      }
    }
  }
`;

export const loadFooterNav = async ({
  pageMetadataMap,
  contentfulClient,
}: {
  pageMetadataMap: PageMetadataMap;
  contentfulClient?: ContentfulClient;
}): Promise<NavMenuType[]> => {
  if (!contentfulClient) return footerNavTestContent;
  const footerMenu: NavMenuType[] = [];
  const data = await contentfulClient.fetch<FooterNavQuery>(printQuery(footerNavQuery));
  if (data?.menuCollection?.items?.length === 1) {
    const subMenus = data.menuCollection.items[0]?.childrenCollection?.items;
    if (subMenus) {
      subMenus.forEach((subMenu) => {
        const footerSubMenu: NavLinkType[] = [];
        if (
          subMenu &&
          subMenu.__typename === "Menu" &&
          subMenu.menuType === "Footer SubMenu" &&
          subMenu.title
        ) {
          const subMenuItems = subMenu.childrenCollection?.items;
          if (subMenuItems) {
            subMenuItems.forEach((item) => {
              if (
                item &&
                item.__typename === "MenuItem" &&
                item.title &&
                (item.internalLink || item.externalLink)
              ) {
                let link;
                if (item.internalLink?.sys?.id) {
                  link = pageMetadataMap.get(item.internalLink.sys.id)?.url;
                } else if (item.externalLink) {
                  link = item.externalLink;
                }
                link = link ?? "/";
                footerSubMenu.push({
                  id: item.sys.id,
                  name: item.title,
                  link,
                });
              }
            });
          }
          footerMenu.push({
            id: subMenu.sys.id,
            name: subMenu.title,
            children: footerSubMenu,
          });
        }
      });
    }
  }
  return footerMenu;
};