Skip to content

Commit 171326f

Browse files
committed
CMS-45730 Use getPath, getItems to get links for navigation
1 parent 4f04b54 commit 171326f

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

templates/alloy-template/src/app/[...slug]/page.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ export default async function Page({ params }: Props) {
2020
const path = `/${slug.join('/')}/`;
2121
const c = await client.getContentByPath(path);
2222

23-
const children = (await client.getItems(path)) ?? [];
24-
const ancestors = (await client.getPath(path)) ?? [];
25-
2623
if (c.length === 0) {
2724
notFound();
2825
}
2926

3027
return (
3128
<>
32-
<Header currentPath={{ children, ancestors }} />
29+
{/** Passing down client and currentPath to Footer to fetch dynamic links */}
30+
<Header client={client} currentPath={path} />
3331
<div className="container mx-auto p-10">
3432
<OptimizelyComponent opti={c[0]} />
3533
</div>
36-
<Footer />
34+
<Footer client={client} currentPath={path} />
3735
</>
3836
);
3937
}

templates/alloy-template/src/components/base/Footer.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphClient } from '@optimizely/cms-sdk';
2+
13
interface FooterLink {
24
label: string;
35
href: string;
@@ -9,8 +11,9 @@ interface FooterSection {
911
}
1012

1113
interface FooterProps {
14+
client: GraphClient;
15+
currentPath: string;
1216
sections?: FooterSection[];
13-
currentPath?: string;
1417
}
1518

1619
const defaultSections: FooterSection[] = [
@@ -48,7 +51,29 @@ const defaultSections: FooterSection[] = [
4851
},
4952
];
5053

51-
function Footer({ sections = defaultSections, currentPath }: FooterProps) {
54+
async function Footer({
55+
client,
56+
currentPath,
57+
sections = defaultSections,
58+
}: FooterProps) {
59+
const allLinks = await Promise.all([
60+
client.getItems('/en/'),
61+
client.getItems('/en/about-us'),
62+
]);
63+
64+
// Flatten the array of arrays and map to FooterLink format
65+
const footerLinks = allLinks.flat().map((ancestor: any) => ({
66+
key: ancestor._metadata?.key,
67+
label: ancestor._metadata?.displayName,
68+
href: ancestor._metadata?.url?.hierarchical,
69+
}));
70+
71+
console.info(
72+
'Footer footerLinks:',
73+
currentPath,
74+
JSON.stringify(footerLinks, null, 2)
75+
);
76+
5277
return (
5378
<footer className="bg-gray-800 text-white">
5479
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">

templates/alloy-template/src/components/base/Header.tsx

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
import Link from 'next/link';
2-
3-
interface NavigationItem {
4-
label: string;
5-
href: string;
6-
}
2+
import { GraphClient } from '@optimizely/cms-sdk';
73

84
interface HeaderProps {
9-
currentPath: any;
10-
navigationItems?: NavigationItem[];
5+
client: GraphClient;
6+
currentPath: string;
117
logoText?: string;
128
}
139

14-
const defaultNavigationItems: NavigationItem[] = [
15-
{ label: 'ALLOY PLAN', href: '/en/alloy-plan' },
16-
{ label: 'ALLOY TRACK', href: '/en/alloy-track' },
17-
{ label: 'ALLOY MEET', href: '/en/alloy-meet' },
18-
{ label: 'ABOUT US', href: '/en/about-us' },
19-
];
20-
21-
async function Header({
22-
currentPath,
23-
navigationItems = defaultNavigationItems,
24-
}: HeaderProps) {
25-
const ancestors = currentPath?.ancestors || [];
10+
async function Header({ client, currentPath }: HeaderProps) {
11+
const ancestors = (await client.getPath(currentPath)) || [];
12+
const navLinks = (await client.getItems('/en/')) ?? [];
2613

2714
// Filter out the start page (first item) and create breadcrumbs
2815
const breadcrumbs = ancestors.slice(1).map((ancestor: any) => ({
16+
key: ancestor._metadata.key,
17+
label: ancestor._metadata.displayName,
18+
href: ancestor._metadata.url.hierarchical,
19+
}));
20+
21+
// Create navigation from navLinks of the /en/ page
22+
const navigations = navLinks.map((ancestor: any) => ({
23+
key: ancestor._metadata.key,
2924
label: ancestor._metadata.displayName,
3025
href: ancestor._metadata.url.hierarchical,
3126
}));
@@ -39,12 +34,12 @@ async function Header({
3934
<nav className="hidden md:flex md:items-center md:space-x-8">
4035
<div className="flex-shrink-0">
4136
{/* Logo */}
42-
<img src="/logo.png" alt="Logo" className="h-14 w-14" />
37+
{/* <img src="/logo.png" alt="Logo" className="h-14 w-14" /> */}
4338
</div>
4439
<div className="flex items-center space-x-8">
45-
{navigationItems.map((item, index) => (
40+
{navigations.map((item) => (
4641
<a
47-
key={index}
42+
key={item.key}
4843
href={item.href}
4944
className="text-gray-700 hover:text-teal-600 transition-colors duration-200 text-lg font-extrabold uppercase tracking-wide"
5045
>

0 commit comments

Comments
 (0)