Skip to content

Commit aa79734

Browse files
committed
Add new test page
1 parent 33e7b49 commit aa79734

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { GraphClient, GraphErrors } from '@optimizely/cms-sdk';
2+
import { OptimizelyComponent } from '@optimizely/cms-sdk/react/server';
3+
import React from 'react';
4+
5+
type Props = {
6+
params: Promise<{
7+
slug: string[];
8+
}>;
9+
// Assume that search params are correct:
10+
searchParams: Promise<{ variation?: string }>;
11+
};
12+
13+
function handleGraphErrors(err: unknown): never {
14+
if (err instanceof GraphErrors.GraphResponseError) {
15+
console.log('Error message:', err.message);
16+
console.log('Query:', err.request.query);
17+
console.log('Variables:', err.request.variables);
18+
}
19+
if (err instanceof GraphErrors.GraphContentResponseError) {
20+
console.log('Detailed errors: ', err.errors);
21+
}
22+
23+
throw err;
24+
}
25+
26+
function getRandomElement<T>(arr: T[]): T {
27+
return arr[Math.floor(Math.random() * arr.length)];
28+
}
29+
30+
export default async function Page({ params, searchParams }: Props) {
31+
const { slug } = await params;
32+
const path = `/en/${slug.join('/')}/`;
33+
34+
const client = new GraphClient(process.env.OPTIMIZELY_GRAPH_SINGLE_KEY!, {
35+
graphUrl: process.env.OPTIMIZELY_GRAPH_URL,
36+
});
37+
38+
const variation = (await searchParams).variation;
39+
40+
if (variation) {
41+
const list = await client
42+
.getContentByPath(path, {
43+
variation: { include: 'SOME', value: [variation] },
44+
})
45+
.catch(handleGraphErrors);
46+
return <OptimizelyComponent opti={list[0]} />;
47+
}
48+
49+
const items = await client
50+
.getContentByPath(path, {
51+
variation: { include: 'SOME', value: [], includeOriginal: true },
52+
})
53+
.catch(handleGraphErrors);
54+
55+
console.log('Variations', items.length);
56+
57+
const content = getRandomElement(items);
58+
59+
return (
60+
<>
61+
<p>Number of variations: {items.length}</p>
62+
<hr />
63+
<OptimizelyComponent opti={content} />
64+
</>
65+
);
66+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
BlankSection,
3+
BlankSection2,
4+
Column,
5+
ColumnA,
6+
Component1,
7+
Component1A,
8+
Component1B,
9+
Component2,
10+
Component2A,
11+
Component3,
12+
Component3C,
13+
ct1,
14+
ct2,
15+
ct3,
16+
dt1,
17+
dt2,
18+
dt3,
19+
dt4,
20+
dt5,
21+
dt6,
22+
Row,
23+
RowA,
24+
} from '@/components/with-display-templates';
25+
import {
26+
initContentTypeRegistry,
27+
initDisplayTemplateRegistry,
28+
} from '@optimizely/cms-sdk';
29+
import { initReactComponentRegistry } from '@optimizely/cms-sdk/react/server';
30+
31+
initContentTypeRegistry([ct1, ct2, ct3]);
32+
initDisplayTemplateRegistry([dt1, dt2, dt3, dt4, dt5, dt6]);
33+
initReactComponentRegistry({
34+
// In this example, all content types have components but only with tags
35+
resolver: {
36+
test_c1: {
37+
tags: {
38+
tagA: Component1A,
39+
tagB: Component1B,
40+
},
41+
},
42+
'test_c2:tagA': Component2A,
43+
test_c3: {
44+
tags: {
45+
tagC: Component3C,
46+
},
47+
},
48+
'BlankSection:tagA': BlankSection2,
49+
_Row: {
50+
tags: {
51+
tagA: RowA,
52+
},
53+
},
54+
'_Column:tagA': ColumnA,
55+
},
56+
});
57+
58+
export default function RootLayout({
59+
children,
60+
}: Readonly<{
61+
children: React.ReactNode;
62+
}>) {
63+
return (
64+
<html lang="en">
65+
<body>{children}</body>
66+
</html>
67+
);
68+
}

0 commit comments

Comments
 (0)