Skip to content

Commit d7edd8a

Browse files
committed
Fix PR comments
Feature: CMS-47083
1 parent 37d442f commit d7edd8a

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed
Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,36 @@
11
import { describe, it, expect } from 'vitest';
22
import { parseDisplaySettings } from '../displayTemplates.js';
33

4-
54
describe('parseDisplaySettings', () => {
65
it('should parse valid display settings correctly', () => {
7-
const template = {
8-
settings: {
9-
layout: { editor: 'select', choices: { grid: {}, list: {} } },
10-
theme: { editor: 'select', choices: { dark: {}, light: {} } }
11-
}
12-
} as any;
13-
146
const input = [
157
{ key: 'layout', value: 'grid' },
16-
{ key: 'theme', value: 'dark' }
8+
{ key: 'theme', value: 'dark' },
179
];
18-
19-
const result = parseDisplaySettings(input, template.settings);
10+
const result = parseDisplaySettings(input);
2011
expect(result).toEqual({
2112
layout: 'grid',
22-
theme: 'dark'
13+
theme: 'dark',
2314
});
2415
});
2516

26-
it('should handle checkbox correctly', () => {
27-
const template = {
28-
settings: {
29-
showImage: { editor: 'checkbox', choices: {} }
30-
}
31-
} as any;
32-
33-
const input = [{ key: 'showImage', value: 'true' }];
34-
const result = parseDisplaySettings(input, template.settings);
17+
it('should handle missing properties gracefully', () => {
18+
const input = [{ key: 'layout', value: 'grid' }];
19+
const result = parseDisplaySettings(input);
3520
expect(result).toEqual({
36-
showImage: 'true'
21+
layout: 'grid',
3722
});
3823
});
3924

40-
it('should return undefined for null input', () => {
41-
const template = { settings: {} } as any;
25+
it('should return an empty object for null input', () => {
4226
const input = null;
43-
const result = parseDisplaySettings(input, template.settings);
44-
expect(result).toEqual({});
27+
const result = parseDisplaySettings(input ?? undefined);
28+
expect(result).toEqual(undefined);
4529
});
4630

47-
it('should return undefined for undefined input', () => {
48-
const template = { settings: {} } as any;
31+
it('should return an empty object for undefined input', () => {
4932
const input = undefined;
50-
const result = parseDisplaySettings(input, template.settings);
51-
expect(result).toEqual({});
33+
const result = parseDisplaySettings(input);
34+
expect(result).toEqual(undefined);
5235
});
53-
});
54-
36+
});

packages/optimizely-cms-sdk/src/model/displayTemplates.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,20 @@ export type DisplayTemplate<T = DisplayTemplateVariant> = T & {
6262
__type: 'displayTemplate';
6363
};
6464

65-
6665
export function parseDisplaySettings(
67-
settings: DisplaySettingsType[] | null | undefined,
68-
templateSettings: Record<string, { editor: string; choices: Record<string, any> }>
69-
): Record<string, string> {
70-
if (!settings || settings.length === 0) return {};
71-
72-
const result: Record<string, string> = {};
66+
displaySettings?: DisplaySettingsType[] | null
67+
): Record<string, string | boolean> | undefined {
68+
if (!displaySettings) {
69+
return undefined; // Return undefined if displaySettings is not provided
70+
}
7371

74-
settings.forEach(s => {
75-
const settingDef = templateSettings[s.key];
76-
if (!settingDef) return;
72+
const result: Record<string, string | boolean> = {}; // Initialize an empty object
7773

78-
if (settingDef.editor === 'select') {
79-
result[s.key] = s.value;
80-
} else if (settingDef.editor === 'checkbox') {
81-
result[s.key] = s.value === 'true' ? 'true' : 'false';
82-
}
83-
});
74+
// Iterate over the input array
75+
for (const { key, value } of displaySettings) {
76+
// Assign the value to the key in the result object
77+
result[key] = value === 'true' ? true : value === 'false' ? false : value;
78+
}
8479

8580
return result;
8681
}
87-

packages/optimizely-cms-sdk/src/react/server.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
Infer,
1515
} from '../infer.js';
1616
import { isComponentNode } from '../util/baseTypeUtil.js';
17-
import { DisplayTemplate, parseDisplaySettings } from '../model/displayTemplates.js';
17+
import { parseDisplaySettings } from '../model/displayTemplates.js';
1818
import { getDisplayTemplate, getDisplayTemplateTag } from '../model/displayTemplateRegistry.js';
1919
import { isDev } from '../util/environment.js';
2020
import { appendToken } from '../util/preview.js';
@@ -73,7 +73,7 @@ export function initReactComponentRegistry(options: InitOptions) {
7373
}
7474

7575
/** Props for the {@linkcode OptimizelyComponent} component */
76-
type OptimizelyComponentProps<T extends DisplayTemplate = DisplayTemplate> = {
76+
type OptimizelyComponentProps = {
7777
/** Data read from the CMS */
7878
opti: {
7979
/** Content type name */
@@ -90,7 +90,7 @@ type OptimizelyComponentProps<T extends DisplayTemplate = DisplayTemplate> = {
9090
__composition?: ExperienceCompositionNode;
9191
};
9292

93-
displaySettings?: Partial<Infer<T>>;
93+
displaySettings?: Record<string, string | boolean>;
9494
};
9595

9696
export async function OptimizelyComponent({
@@ -108,11 +108,6 @@ export async function OptimizelyComponent({
108108
});
109109

110110
if (!Component) {
111-
console.log(
112-
`[optimizely-cms-sdk] No component found for content type ${opti.__typename
113-
} ${opti.__tag ? `with tag "${opti.__tag}"` : ''}`
114-
);
115-
116111
return (
117112
<FallbackComponent>
118113
No component found for content type <b>{opti.__typename}</b>
@@ -129,16 +124,16 @@ export async function OptimizelyComponent({
129124
);
130125
}
131126

132-
export type StructureContainerProps<T extends DisplayTemplate = DisplayTemplate> = {
127+
export type StructureContainerProps = {
133128
node: ExperienceStructureNode;
134129
children: React.ReactNode;
135130
index?: number;
136-
displaySettings?: Partial<Infer<T>>;
131+
displaySettings?: Record<string, string | boolean>;
137132
};
138-
export type ComponentContainerProps<T extends DisplayTemplate = DisplayTemplate> = {
133+
export type ComponentContainerProps = {
139134
node: ExperienceComponentNode;
140135
children: React.ReactNode;
141-
displaySettings?: Partial<Infer<T>>;
136+
displaySettings?: Record<string, string | boolean>;
142137
};
143138
export type StructureContainer = (
144139
props: StructureContainerProps
@@ -161,9 +156,8 @@ export function OptimizelyExperience({
161156
: null;
162157

163158
const parsedDisplaySettings = template
164-
? parseDisplaySettings(node.displaySettings, template.settings)
165-
: {};
166-
console.error(parsedDisplaySettings);
159+
? parseDisplaySettings(node.displaySettings)
160+
: undefined;
167161

168162
if (isComponentNode(node)) {
169163
const Wrapper = ComponentWrapper ?? React.Fragment;
@@ -269,8 +263,8 @@ export function OptimizelyGridSection({
269263
: null;
270264

271265
const parsedDisplaySettings = template
272-
? parseDisplaySettings(node.displaySettings, template.settings)
273-
: {};
266+
? parseDisplaySettings(node.displaySettings)
267+
: undefined;
274268

275269
if (isComponentNode(node)) {
276270
return (

0 commit comments

Comments
 (0)