Skip to content

Commit

Permalink
feat: support new client
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Aug 26, 2024
1 parent 81eb124 commit 996b863
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 61 deletions.
1 change: 1 addition & 0 deletions packages/shopper-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@elasticpath/js-sdk": "5.0.0",
"@epcc-sdk/sdks-shopper": "workspace:*",
"tslib": "^2.6.2"
},
"publishConfig": {
Expand Down
108 changes: 79 additions & 29 deletions packages/shopper-common/src/navigation/build-navigation.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,108 @@
import type { Hierarchy, ElasticPath } from "@elasticpath/js-sdk"
import {
getHierarchies,
getHierarchyChildren,
getHierarchyNodes,
} from "./services/hierarchy"
import type { Client, Hierarchy } from "@epcc-sdk/sdks-shopper"
import { ISchema, NavigationNode } from "./navigation-types"
import {
getByContextAllHierarchies,
getByContextHierarchyChildNodes,
getByContextHierarchyNodes,
} from "@epcc-sdk/sdks-shopper"

export async function buildSiteNavigation(
client: ElasticPath,
client?: Client,
): Promise<NavigationNode[]> {
// Fetch hierarchies to be used as top level nav
const hierarchies = await getHierarchies(client)
return constructTree(hierarchies, client)
const hierarchiesResponse = await getByContextAllHierarchies({
client,
body: undefined,
query: {
"page[limit]": 100,
"page[offset]": 0,
},
})

if (!hierarchiesResponse.response.ok) {
throw new Error("Failed to fetch hierarchies")
}

const hierarchies = hierarchiesResponse.data?.data

return hierarchies ? constructTree(hierarchies, client) : []
}

/**
* Construct hierarchy tree, limited to 5 hierarchies at the top level
*/
function constructTree(
hierarchies: Hierarchy[],
client: ElasticPath,
client?: Client,
): Promise<NavigationNode[]> {
const tree = hierarchies
.slice(0, 4)
.map((hierarchy) =>
createNode({
name: hierarchy.attributes.name,
id: hierarchy.id,
slug: hierarchy.attributes.slug,
name: hierarchy.attributes?.name!,
id: hierarchy.id!,
slug: hierarchy.attributes?.slug,
}),
)
.map(async (hierarchy) => {
// Fetch first-level nav ('parent nodes') - the direct children of each hierarchy
const directChildren = await getHierarchyChildren(hierarchy.id, client)
const directChildrenResponse = await getByContextHierarchyChildNodes({
client,
body: undefined,
path: {
hierarchy_id: hierarchy.id,
},
query: {
"page[limit]": 100,
"page[offset]": 0,
},
})

if (!directChildrenResponse.response.ok) {
throw new Error("Failed to fetch hierarchy children")
}

const directChildren = directChildrenResponse.data?.data ?? []

// Fetch all nodes in each hierarchy (i.e. all 'child nodes' belonging to a hierarchy)
const allNodes = await getHierarchyNodes(hierarchy.id, client)
const allNodesResponse = await getByContextHierarchyNodes({
client,
body: undefined,
path: {
hierarchy_id: hierarchy.id,
},
query: {
"page[limit]": 100,
"page[offset]": 0,
},
})

if (!allNodesResponse.response.ok) {
throw new Error("Failed to fetch hierarchy nodes")
}

const allNodes = allNodesResponse.data?.data ?? []

// Build 2nd level by finding all 'child nodes' belonging to each first level featured-nodes
const directs = directChildren.slice(0, 4).map((child) => {
const children: ISchema[] = allNodes
.filter((node) => node?.relationships?.parent.data.id === child.id)
.map((node) =>
createNode({
name: node.attributes.name,
id: node.id,
slug: node.attributes.slug,
hrefBase: `${hierarchy.href}/${child.attributes.slug}`,
}),
)
const directs = directChildren?.slice(0, 4).map((child) => {
const children: ISchema[] =
allNodes
?.filter(
(node) => node?.relationships?.parent?.data.id === child.id,
)
.map((node) =>
createNode({
name: node.attributes?.name!,
id: node.id!,
slug: node.attributes?.slug,
hrefBase: `${hierarchy.href}/${child.attributes?.slug}`,
}),
) ?? []

return createNode({
name: child.attributes.name,
id: child.id,
slug: child.attributes.slug,
name: child.attributes?.name!,
id: child.id!,
slug: child.attributes?.slug,
hrefBase: hierarchy.href,
children,
})
Expand Down
30 changes: 0 additions & 30 deletions packages/shopper-common/src/navigation/services/hierarchy.ts

This file was deleted.

23 changes: 21 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 996b863

Please sign in to comment.