Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sidenavs not redering for lovell facility pages #226

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/lib/drupal/facilitySideNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const normalizeMenuItem = (item: MenuItem): SideNavItem => {
if (item.items && item.items.length > 0) {
item.items.forEach((i) => nestedItems.push(normalizeMenuItem(i)))
}

return {
description: item.description,
expanded: item.expanded,
label: item.title,
links: nestedItems,
url: { path: item.url },
lovellSection: item.field_menu_section || null,
}
}

Expand All @@ -26,7 +26,6 @@ const normalizeMenuData = (menu: Menu): SideNavData => {
menu.tree.forEach((item) => {
return links.push(normalizeMenuItem(item))
})

return {
name: menu.tree[0].title,
description: menu.tree[0].description,
Expand All @@ -43,7 +42,6 @@ export const buildSideNavDataFromMenu = (
menu: Menu
): SideNavMenu => {
const data = normalizeMenuData(menu)

return {
rootPath: `${entityPath}/`,
data,
Expand Down
8 changes: 7 additions & 1 deletion src/lib/drupal/lovell/staticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getLovellVariantOfUrl,
getOppositeChildVariant,
isLovellBifurcatedResource,
getLovellVariantOfMenu,
getLovellVariantOfBreadcrumbs,
} from './utils'

Expand Down Expand Up @@ -162,11 +163,16 @@ export async function getLovellStaticPropsResource(
): Promise<LovellStaticPropsResource<LovellFormattedResource>> {
// Lovell listing pages need Federal items merged
if (context.lovell.isLovellVariantPage && context.listing.isListingPage) {
return getLovellListingPageStaticPropsResource(
const resource = await getLovellListingPageStaticPropsResource(
resourceType as ListingResourceTypeType,
pathInfo,
context
)

return {
...resource,
menu: getLovellVariantOfMenu(resource.menu, context.lovell.variant),
}
}

// Other Lovell pages depend on base resource
Expand Down
124 changes: 124 additions & 0 deletions src/lib/drupal/lovell/tests/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,127 @@ export const newsStoryPartialResource = {
listing: 'listing',
entityId: 12345,
}

export const lovellSidenavData = {
rootPath: '/lovell-federal-health-care-va/stories/',
data: {
name: 'Lovell Federal health care - TRICARE',
description: null,
links: [
{
description: null,
expanded: false,
label: 'Lovell Federal health care',
links: [
{
description: null,
expanded: false,
label: 'SERVICES AND LOCATIONS',
links: [
{
description: null,
expanded: false,
label: 'Health services',
links: [
{
description: null,
expanded: false,
label: 'Returning service member care',
links: [],
url: {
path: '/lovell-federal-health-care-tricare/health-services/returning-service-member-care',
},
lovellSection: 'tricare',
},
],
url: {
path: '/lovell-federal-health-care-va/health-services',
},
lovellSection: 'va',
},
{
description: null,
expanded: false,
label: 'Health services',
links: [],
url: {
path: '/lovell-federal-health-care-tricare/health-services',
},
lovellSection: 'tricare',
},
{
description: null,
expanded: false,
label: 'Locations',
links: [
{
description: null,
expanded: false,
label: 'Captain James A. Lovell Federal Health Care Center',
links: [],
url: {
path: '/lovell-federal-health-care-va/locations/captain-james-a-lovell-federal-health-care-center',
},
lovellSection: 'va',
},
],
url: { path: '/lovell-federal-health-care-va/locations' },
lovellSection: 'va',
},
{
description: 'TRICARE locations',
expanded: false,
label: 'Locations',
links: [
{
description: null,
expanded: false,
label: 'Captain James A. Lovell Federal Health Care Center',
links: [],
url: {
path: '/lovell-federal-health-care-tricare/locations/captain-james-a-lovell-federal-health-care-center',
},
lovellSection: 'tricare',
},
],
url: {
path: '/lovell-federal-health-care-tricare/locations',
},
lovellSection: 'tricare',
},
],
url: { path: '' },
lovellSection: 'both',
},
{
description: null,
expanded: false,
label: 'NEWS AND EVENTS',
links: [
{
description: null,
expanded: false,
label: 'Events',
links: [],
url: { path: '/lovell-federal-health-care-va/events' },
lovellSection: 'va',
},
{
description: null,
expanded: false,
label: 'Events',
links: [],
url: { path: '/lovell-federal-health-care-tricare/events' },
lovellSection: 'tricare',
},
],
url: { path: '' },
lovellSection: 'both',
},
],
url: { path: '/lovell-federal-health-care' },
lovellSection: 'both',
},
],
},
}
54 changes: 54 additions & 0 deletions src/lib/drupal/lovell/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isLovellChildVariantSlug,
getOppositeChildVariant,
isLovellBifurcatedResource,
getLovellVariantOfMenu,
getLovellVariantOfUrl,
getLovellVariantOfTitle,
getLovellVariantOfBreadcrumbs,
Expand All @@ -29,6 +30,7 @@ import {
lovellVaResource,
otherResource,
newsStoryPartialResource,
lovellSidenavData,
} from './mockData'

describe('isLovellResourceType', () => {
Expand Down Expand Up @@ -398,3 +400,55 @@ describe('isLovellBifurcatedResource', () => {
expect(result).toBe(false)
})
})

describe('getLovellVariantOfMenu', () => {
it('should filter out menus with non-matching lovellSection', () => {
const result = getLovellVariantOfMenu(lovellSidenavData, 'va')

// Check if the TRICARE specific items are filtered out
expect(
result.data.links[0].links[1].links.find(
(link) => link.lovellSection === 'tricare'
)
).toBeUndefined()
})

it('should not filter out menus with lovellSection set to "both"', () => {
const result = getLovellVariantOfMenu(lovellSidenavData, 'va')

// Check if items with lovellSection set to "both" are still there
expect(result.data.links[0].lovellSection).toEqual('both')
expect(result.data.links[0].links[0].lovellSection).toEqual('both')
})

it('should keep VA specific items when variant is "va"', () => {
const result = getLovellVariantOfMenu(lovellSidenavData, 'va')

// Check if VA specific items are still there
expect(result.data.links[0].links[0].links[0].lovellSection).toEqual('va')

// Assert against the 'Locations' sub-submenu item
expect(result.data.links[0].links[0].links[1].label).toEqual('Locations')
expect(result.data.links[0].links[0].links[1].lovellSection).toEqual('va')
})

it('should filter out VA specific items when variant is "tricare"', () => {
const result = getLovellVariantOfMenu(lovellSidenavData, 'tricare')

// Check if VA specific items are filtered out
expect(
result.data.links[0].links[0].links.find(
(link) => link.lovellSection === 'va'
)
).toBeUndefined()
})

it('should keep TRICARE specific items when variant is "tricare"', () => {
const result = getLovellVariantOfMenu(lovellSidenavData, 'tricare')

// Check if TRICARE specific items are still there
expect(result.data.links[0].links[0].links[1].lovellSection).toEqual(
'tricare'
)
})
})
40 changes: 40 additions & 0 deletions src/lib/drupal/lovell/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { StaticPathResourceType } from '@/types/index'
import { FormattedResource } from '@/data/queries'
import { ResourceTypeType } from '@/lib/constants/resourceTypes'
import { slugToPath } from '@/lib/utils/slug'
import { SideNavItem, SideNavMenu } from '@/types/index'
import { BreadcrumbItem } from '@/types/dataTypes/drupal/field_type'

export function isLovellResourceType(resourceType: ResourceTypeType): boolean {
Expand Down Expand Up @@ -214,3 +215,42 @@ export function isLovellBifurcatedResource(
isLovellFederalResource(resource as LovellBifurcatedFormattedResource)
)
}

// This filters out two lovell-specific menu items that aren't used in side navs.
function processLovellMenuData(menu: SideNavMenu) {
if (menu && menu.data && menu.data.links) {
menu.data.links = menu.data.links.filter(
(link) => link.links && link.links.length > 0
)
}
return menu
}

export function getLovellVariantOfMenu(
menu: SideNavMenu,
variant: LovellVariant
) {
menu = processLovellMenuData(menu)
// Pass only the 'links' property to filterMenu.
const filteredLinks = filterMenu(menu.data.links, variant)
return {
...menu,
data: {
...menu.data,
links: filteredLinks,
},
}
}

// Recursive function to filter out links based on current Lovell Variant
function filterMenu(links: SideNavItem[], variant: LovellVariant) {
return links
.filter(
(menuItem) =>
menuItem.lovellSection === variant || menuItem.lovellSection === 'both'
)
.map((menuItem) => ({
...menuItem,
links: menuItem.links ? filterMenu(menuItem.links, variant) : undefined,
}))
}
1 change: 1 addition & 0 deletions src/types/dataTypes/drupal/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface MenuItem {
expanded: boolean
enabled: boolean
items?: Tree
field_menu_section?: string
}

type Tree = ReadonlyArray<MenuItem>
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export type SideNavItem = {
label: string
links: SideNavItem[]
url: { path: string }
lovellSection?: string
}

export type SideNavData = {
Expand Down
Loading