Skip to content

Commit b69cf0a

Browse files
fix(headless): don't leak pagination state to productSuggest call (#4667)
https://coveord.atlassian.net/browse/KIT-3660 For some context, Initially the ticket was "stop culling productSuggest to 5". It could not be done since the pagination part of the state was overwriting the perPage value for the productSuggest call (a feature from CAPI). So let's say there was 25 products in the page, there would be 25 product suggesting since both call were using the same pagination. Pagination is useless for now in productSuggest so we can simply remove it from the request. --------- Co-authored-by: GitHub Actions Bot <>
1 parent 7c2491c commit b69cf0a

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

packages/atomic/src/components/commerce/atomic-commerce-search-box/e2e/page-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class SearchBoxPageObject extends BasePageObject<'atomic-commerce-search-
6565
}: {index?: number; total?: number; listSide?: 'Left' | 'Right'} = {}) {
6666
return this.page.getByLabel(
6767
new RegExp(
68-
`instant result\\.(?: Button\\.)? ${index ?? '\\d'} of ${total ?? '\\d'}\\.${this.listSideAffix(listSide)}`
68+
`instant result\\.(?: Button\\.)? ${index ?? '\\d{1,2}'} of ${total ?? '\\d{1,2}'}\\.${this.listSideAffix(listSide)}`
6969
)
7070
);
7171
}

packages/headless/src/features/commerce/common/actions.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,42 @@ describe('commerce common actions', () => {
530530
});
531531
});
532532
});
533+
534+
describe('#buildInstantProductsAPIRequest', () => {
535+
let state: Actions.ListingAndSearchStateNeededByQueryCommerceAPI;
536+
let mockedBuildInstantProductsAPIRequest: MockInstance;
537+
538+
beforeEach(() => {
539+
vi.clearAllMocks();
540+
state = buildMockCommerceState();
541+
mockedBuildInstantProductsAPIRequest = vi.spyOn(
542+
Actions,
543+
'buildInstantProductsAPIRequest'
544+
);
545+
});
546+
547+
it('given a state that has commercePagination, returns request without it', () => {
548+
state.commercePagination = {
549+
principal: {
550+
page: 1,
551+
perPage: 10,
552+
totalEntries: 50,
553+
totalPages: 5,
554+
},
555+
recommendations: {},
556+
};
557+
558+
const request = Actions.buildInstantProductsAPIRequest(
559+
state,
560+
navigatorContext
561+
);
562+
563+
expect(mockedBuildInstantProductsAPIRequest).toHaveBeenCalledWith(
564+
state,
565+
navigatorContext
566+
);
567+
568+
expect(request.page).toEqual(undefined);
569+
});
570+
});
533571
});

packages/headless/src/features/commerce/common/actions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ export const buildCommerceAPIRequest = (
5353
};
5454
};
5555

56+
export const buildInstantProductsAPIRequest = (
57+
state: ListingAndSearchStateNeededByQueryCommerceAPI,
58+
navigatorContext: NavigatorContext
59+
): CommerceAPIRequest => {
60+
const {commercePagination, ...restState} = state;
61+
return {
62+
...buildCommerceAPIRequest(restState, navigatorContext),
63+
};
64+
};
65+
5666
export const buildBaseCommerceAPIRequest = (
5767
state: StateNeededByQueryCommerceAPI,
5868
navigatorContext: NavigatorContext,

packages/headless/src/features/commerce/search/search-actions.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {ChildProduct} from '../../../api/commerce/common/product.js';
88
import {SearchCommerceSuccessResponse} from '../../../api/commerce/search/response.js';
99
import {validatePayload} from '../../../utils/validate-payload.js';
1010
import {deselectAllNonBreadcrumbs} from '../../breadcrumb/breadcrumb-actions.js';
11-
import {buildCommerceAPIRequest} from '../common/actions.js';
11+
import {
12+
buildCommerceAPIRequest,
13+
buildInstantProductsAPIRequest,
14+
} from '../common/actions.js';
1215
import {
1316
clearAllCoreFacets,
1417
updateAutoSelectionForAllCoreFacets,
@@ -155,21 +158,16 @@ export const fetchInstantProducts = createAsyncThunk<
155158
const {apiClient, navigatorContext} = extra;
156159
const {q} = payload;
157160
const fetched = await apiClient.productSuggestions({
158-
...buildCommerceAPIRequest(state, navigatorContext),
161+
...buildInstantProductsAPIRequest(state, navigatorContext),
159162
query: q,
160163
});
161164

162165
if (isErrorResponse(fetched)) {
163166
return rejectWithValue(fetched.error);
164167
}
165168

166-
// TODO: Should ultimately rely on different config for product suggest endpoint which would support
167-
// different config for pagination: Would not have to cull array of products client side.
168-
// https://coveord.atlassian.net/browse/CAPI-682
169-
const products = fetched.success.products.slice(0, 5);
170-
171169
return {
172-
response: {...fetched.success, products},
170+
response: {...fetched.success, products: fetched.success.products},
173171
};
174172
}
175173
);

0 commit comments

Comments
 (0)