Skip to content

Commit 6c9ce4b

Browse files
authored
Fallback to Merchandisers displayName set for recommendation pods (#193)
* Fallback to Merchandisers displayName set for recommendation pods * Update docs
1 parent 66924df commit 6c9ce4b

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const sectionsDescription = `- by default, typing a query will fetch data
7272
- the order of the objects in the \`sections\` array determines the order of the results
7373
- each autocomplete section object must have a \`indexSectionName\`
7474
- each recommendation section object must have a \`podId\`
75+
- passing displayName in the sectionConfiguration will override the displayName set by Merchandisers on the Customer Dashboard for the recommendation section
7576
- each custom section object must have a \`displayName\`
7677
- each section object can specify a \`type\`
7778
- each section object can override the default \`numResults\` of 8
@@ -112,6 +113,7 @@ export const zeroStateDescription = `- when the text input field has no text, we
112113
- the order of the objects in the \`zeroStateSections\` array determines the order of the results
113114
- each autocomplete section object must have a \`indexSectionName\`
114115
- each recommendation section object must have a \`podId\`
116+
- passing displayName in the sectionConfiguration will override the displayName set by Merchandisers on the Customer Dashboard for the recommendation section
115117
- each custom section object must have a \`displayName\`
116118
- each section object can specify a \`type\`
117119
- each section object can override the default \`numResults\` of 8`;

src/hooks/useSections/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export default function useSections(
6262
sectionsResults,
6363
activeSections,
6464
sectionsRefs,
65-
query
65+
query,
66+
recommendations.podsData
6667
);
6768

6869
return {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
/* eslint-disable max-params */
12
import { RefObject, useEffect, useState } from 'react';
2-
import { UserDefinedSection, Section, SectionsData } from '../../types';
3+
import { UserDefinedSection, Section, SectionsData, PodData } from '../../types';
34
import { getActiveSectionsWithData } from '../../utils';
45

56
export default function useActiveSectionsWithData(
67
sectionsResults: SectionsData,
78
activeSections: UserDefinedSection[],
89
sectionsRefs: React.MutableRefObject<RefObject<HTMLLIElement>[]>,
9-
query: string
10+
query: string,
11+
podsData: Record<string, PodData>
1012
) {
1113
const [activeSectionsWithData, setActiveSectionsWithData] = useState<Section[]>([]);
1214

@@ -15,13 +17,14 @@ export default function useActiveSectionsWithData(
1517
const activeSectionsWithDataValue = getActiveSectionsWithData(
1618
activeSections,
1719
sectionsResults,
18-
sectionsRefs
20+
sectionsRefs,
21+
podsData
1922
);
2023

2124
if (activeSectionsWithDataValue.length || !query) {
2225
setActiveSectionsWithData(activeSectionsWithDataValue);
2326
}
24-
}, [activeSections, sectionsResults, sectionsRefs, query]);
27+
}, [activeSections, sectionsResults, sectionsRefs, query, podsData]);
2528

2629
return activeSectionsWithData;
2730
}

src/stories/tests/ComponentTests.stories.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,22 @@ TypeSearchTermRenderSectionsDefaultOrder.play = async ({ canvasElement }) => {
224224
expect(canvas.getAllByTestId('cio-item-Products').length).toBeGreaterThan(0);
225225
expect(canvas.getAllByText('Best Sellers').length).toBeGreaterThan(0);
226226

227-
expect(canvas.getByTestId('cio-results').children[0].className).toContain('cio-section');
228-
expect(canvas.getByTestId('cio-results').children[0].className).toContain(
227+
expect(canvas.getByTestId('cio-results').children[1].className).toContain('cio-section');
228+
expect(canvas.getByTestId('cio-results').children[1].className).toContain(
229229
'cio-section-search-suggestions'
230230
);
231231

232-
expect(canvas.getByTestId('cio-results').children[1].className).toContain('cio-section');
233-
expect(canvas.getByTestId('cio-results').children[1].className).toContain('cio-section-products');
234-
235-
// bestsellers indexSectionName is products, and we render class based on that
236232
expect(canvas.getByTestId('cio-results').children[2].className).toContain('cio-section');
237233
expect(canvas.getByTestId('cio-results').children[2].className).toContain('cio-section-products');
238234

235+
// bestsellers indexSectionName is products, and we render class based on that
236+
expect(canvas.getByTestId('cio-results').children[3].className).toContain('cio-section');
237+
expect(canvas.getByTestId('cio-results').children[3].className).toContain('cio-section-products');
238+
239239
// @deprecated The following classNames will be removed in the next major version
240-
expect(canvas.getByTestId('cio-results').children[0].className).toContain('Search Suggestions');
241-
expect(canvas.getByTestId('cio-results').children[1].className).toContain('Products');
242-
expect(canvas.getByTestId('cio-results').children[2].className).toContain('bestsellers');
240+
expect(canvas.getByTestId('cio-results').children[1].className).toContain('Search Suggestions');
241+
expect(canvas.getByTestId('cio-results').children[2].className).toContain('Products');
242+
expect(canvas.getByTestId('cio-results').children[3].className).toContain('bestsellers');
243243
};
244244

245245
// - type search term => render all sections in custom order
@@ -268,22 +268,22 @@ TypeSearchTermRenderSectionsCustomOrder.play = async ({ canvasElement }) => {
268268
expect(canvas.getAllByTestId('cio-item-Products').length).toBeGreaterThan(0);
269269
expect(canvas.getAllByText('Best Sellers').length).toBeGreaterThan(0);
270270

271-
expect(canvas.getByTestId('cio-results').children[0].className).toContain('cio-section');
272-
expect(canvas.getByTestId('cio-results').children[0].className).toContain('cio-section-products');
273-
274-
// bestsellers indexSectionName is products, and we render class based on that
275271
expect(canvas.getByTestId('cio-results').children[1].className).toContain('cio-section');
276272
expect(canvas.getByTestId('cio-results').children[1].className).toContain('cio-section-products');
277273

274+
// bestsellers indexSectionName is products, and we render class based on that
278275
expect(canvas.getByTestId('cio-results').children[2].className).toContain('cio-section');
279-
expect(canvas.getByTestId('cio-results').children[2].className).toContain(
276+
expect(canvas.getByTestId('cio-results').children[2].className).toContain('cio-section-products');
277+
278+
expect(canvas.getByTestId('cio-results').children[3].className).toContain('cio-section');
279+
expect(canvas.getByTestId('cio-results').children[3].className).toContain(
280280
'cio-section-search-suggestions'
281281
);
282282

283283
// @deprecated The following classNames will be removed in the next major version
284-
expect(canvas.getByTestId('cio-results').children[0].className).toContain('Products');
285-
expect(canvas.getByTestId('cio-results').children[1].className).toContain('bestsellers');
286-
expect(canvas.getByTestId('cio-results').children[2].className).toContain('Search Suggestions');
284+
expect(canvas.getByTestId('cio-results').children[1].className).toContain('Products');
285+
expect(canvas.getByTestId('cio-results').children[2].className).toContain('bestsellers');
286+
expect(canvas.getByTestId('cio-results').children[3].className).toContain('Search Suggestions');
287287
};
288288

289289
// - select term suggestion => network tracking event

src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
CONSTANTS,
1414
} from './beaconUtils';
1515
import { isRecommendationsSection } from './typeGuards';
16-
import { Item, Section, UserDefinedSection, SectionsData, Translations } from './types';
16+
import { Item, Section, UserDefinedSection, SectionsData, Translations, PodData } from './types';
1717
import version from './version';
1818

1919
export type GetItemPosition = (args: { item: Item; items: Item[] }) => {
@@ -180,7 +180,8 @@ export const getCioClient = (apiKey?: string, cioJsClientOptions?: ConstructorCl
180180
export const getActiveSectionsWithData = (
181181
activeSections: UserDefinedSection[],
182182
sectionsResults: SectionsData,
183-
sectionsRefs: React.MutableRefObject<React.RefObject<HTMLLIElement>[]>
183+
sectionsRefs: React.MutableRefObject<React.RefObject<HTMLLIElement>[]>,
184+
podsData: Record<string, PodData>
184185
) => {
185186
const activeSectionsWithData: Section[] = [];
186187

@@ -216,6 +217,11 @@ export const getActiveSectionsWithData = (
216217
data: sectionData,
217218
};
218219

220+
if (sectionConfig.type === 'recommendations') {
221+
section.displayName =
222+
sectionConfig.displayName || podsData[sectionConfig.podId].displayName;
223+
}
224+
219225
// If ref passed as part of `SectionConfiguration`, use it.
220226
// Otherwise, use the ref from our library generated refs array
221227
const userDefinedSectionRef = sectionConfig.ref;

0 commit comments

Comments
 (0)