Skip to content

Commit 464f0ce

Browse files
committed
fix: commerce pages in ssr
1 parent 8048bc6 commit 464f0ce

5 files changed

Lines changed: 89 additions & 5 deletions

File tree

packages/react/src/contents/hooks/__tests__/useContentPage.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { cleanup, renderHook } from '@testing-library/react';
22
import { ContentPageType } from '@farfetch/blackout-client';
33
import { fetchContentPage } from '@farfetch/blackout-redux';
44
import {
5+
mockCommercePageWithDataState,
56
mockContentPageEntry,
67
mockContentPageErrorState,
78
mockContentPageInitialState,
@@ -102,6 +103,34 @@ describe('useContentPage', () => {
102103

103104
expect(fetchContentPage).not.toHaveBeenCalled();
104105
});
106+
107+
it('should return values correctly with commercePagesHash option', () => {
108+
const { result } = renderHook(
109+
() =>
110+
useContentPage(
111+
ContentPageType.Listing,
112+
{ slug },
113+
{ commercePagesHash: true },
114+
),
115+
{
116+
wrapper: withStore(mockCommercePageWithDataState),
117+
},
118+
);
119+
120+
expect(result.current).toStrictEqual({
121+
data: [
122+
mockCommercePageWithDataState.entities.contents[
123+
mockContentPageEntry.publicationId
124+
],
125+
],
126+
isLoading: false,
127+
error: null,
128+
isFetched: true,
129+
actions: {
130+
fetch: expect.any(Function),
131+
},
132+
});
133+
});
105134
});
106135

107136
describe('actions', () => {

packages/react/src/contents/hooks/types/useContentPage.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import type { Config } from '@farfetch/blackout-client';
33
export interface UseContentPageOptions {
44
enableAutoFetch?: boolean;
55
fetchConfig?: Config;
6+
commercePagesHash?: boolean;
67
}

packages/react/src/contents/hooks/useContentPage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@ const useContentPage = <T = [ComponentType]>(
2424
) => {
2525
const store = useStore();
2626

27-
const { enableAutoFetch = true, fetchConfig } = options;
27+
const {
28+
enableAutoFetch = true,
29+
fetchConfig,
30+
commercePagesHash = false,
31+
} = options;
2832

2933
const query = useMemo(
3034
() => ({
31-
contentTypeCode: ContentTypeCode.ContentPage,
35+
contentTypeCode: commercePagesHash
36+
? ContentTypeCode.CommercePages
37+
: ContentTypeCode.ContentPage,
3238
codes: fetchQuery.slug.split('?')[0] as string,
3339
}),
34-
[fetchQuery.slug],
40+
[fetchQuery.slug, commercePagesHash],
3541
);
3642

3743
const fetchQueryWithoutSlug = useMemo(() => {

packages/redux/src/contents/serverInitialState.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const serverInitialState: ServerInitialState = ({ model }) => {
2121
}
2222

2323
const { searchContentRequests, slug, seoMetadata, subfolder } = model;
24+
const url = subfolder !== '/' ? slug?.replace(subfolder, '') : slug;
2425

2526
const contents = searchContentRequests.reduce((acc, item) => {
2627
const { searchResponse } = item;
@@ -30,8 +31,13 @@ const serverInitialState: ServerInitialState = ({ model }) => {
3031
return acc;
3132
}
3233

34+
const code =
35+
firstSearchResponseItem.contentTypeCode === 'commerce_pages'
36+
? url
37+
: firstSearchResponseItem.code;
38+
3339
const hash = generateContentHash({
34-
codes: firstSearchResponseItem.code,
40+
codes: code,
3541
contentTypeCode: firstSearchResponseItem.contentTypeCode,
3642
});
3743

@@ -53,7 +59,6 @@ const serverInitialState: ServerInitialState = ({ model }) => {
5359
});
5460
}, {});
5561

56-
const url = subfolder !== '/' ? slug?.replace(subfolder, '') : slug;
5762
const { pathname, query } = parse(url, true);
5863

5964
delete query.json;

tests/__fixtures__/contents/contentPage.fixtures.mts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const contentQuery = {
1515

1616
export const contentPagesHash = generateContentHash(contentQuery);
1717

18+
const commerceQuery = {
19+
codes: slugContentWithoutQuery,
20+
contentTypeCode: 'commerce_pages',
21+
};
22+
const commercePagesHash = generateContentHash(commerceQuery);
23+
1824
export const mockContentPageEntry = {
1925
publicationId: 'dc9c0c95-9485-45c2-a76c-6923bb39b544',
2026
versionId: '78f1922d-0ef1-46ed-b02c-ca541d0a0d80',
@@ -65,6 +71,11 @@ export const mockContentPageEntry = {
6571
],
6672
};
6773

74+
const mockCommercePageEntry = {
75+
...mockContentPageEntry,
76+
contentTypeCode: 'commerce_pages',
77+
};
78+
6879
export const mockContentPage = {
6980
number: 1,
7081
totalItems: 1,
@@ -153,3 +164,35 @@ export const mockContentPageWithDataState = {
153164
},
154165
},
155166
};
167+
168+
export const mockCommercePageWithDataState = {
169+
entities: {
170+
contents: {
171+
[mockCommercePageEntry.publicationId]: {
172+
...mockCommercePageEntry,
173+
publicationDate: 0,
174+
metadata: {
175+
...mockCommercePageEntry.metadata,
176+
custom: {
177+
...mockCommercePageEntry.metadata.custom,
178+
eventDate: 0,
179+
},
180+
},
181+
},
182+
},
183+
},
184+
contents: {
185+
...mockContentPageInitialState.contents,
186+
searchResults: {
187+
[commercePagesHash]: {
188+
isLoading: false,
189+
error: null,
190+
result: {
191+
hash: commercePagesHash,
192+
...mockContentPage,
193+
entries: [mockCommercePageEntry.publicationId],
194+
},
195+
},
196+
},
197+
},
198+
};

0 commit comments

Comments
 (0)