Skip to content

Commit

Permalink
remove commercePagination from instant products call
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Jan 17, 2025
1 parent 3393fe0 commit fec5a56
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
38 changes: 38 additions & 0 deletions packages/headless/src/features/commerce/common/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,42 @@ describe('commerce common actions', () => {
});
});
});

describe('#buildInstantProductsAPIRequest', () => {
let state: Actions.ListingAndSearchStateNeededByQueryCommerceAPI;
let mockedBuildInstantProductsAPIRequest: MockInstance;

beforeEach(() => {
vi.clearAllMocks();
state = buildMockCommerceState();
mockedBuildInstantProductsAPIRequest = vi.spyOn(
Actions,
'buildInstantProductsAPIRequest'
);
});

it('given a state that has commercePagination, returns request without it', () => {
state.commercePagination = {
principal: {
page: 1,
perPage: 10,
totalEntries: 50,
totalPages: 5,
},
recommendations: {},
};

const request = Actions.buildInstantProductsAPIRequest(
state,
navigatorContext
);

expect(mockedBuildInstantProductsAPIRequest).toHaveBeenCalledWith(
state,
navigatorContext
);

expect(request.page).toEqual(undefined);
});
});
});
10 changes: 10 additions & 0 deletions packages/headless/src/features/commerce/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export const buildCommerceAPIRequest = (
};
};

export const buildInstantProductsAPIRequest = (
state: ListingAndSearchStateNeededByQueryCommerceAPI,
navigatorContext: NavigatorContext
): CommerceAPIRequest => {
const {commercePagination, ...restState} = state;
return {
...buildCommerceAPIRequest(restState, navigatorContext),
};
};

export const buildBaseCommerceAPIRequest = (
state: StateNeededByQueryCommerceAPI,
navigatorContext: NavigatorContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {ChildProduct} from '../../../api/commerce/common/product.js';
import {SearchCommerceSuccessResponse} from '../../../api/commerce/search/response.js';
import {validatePayload} from '../../../utils/validate-payload.js';
import {deselectAllNonBreadcrumbs} from '../../breadcrumb/breadcrumb-actions.js';
import {buildCommerceAPIRequest} from '../common/actions.js';
import {
buildCommerceAPIRequest,
buildInstantProductsAPIRequest,
} from '../common/actions.js';
import {
clearAllCoreFacets,
updateAutoSelectionForAllCoreFacets,
Expand Down Expand Up @@ -152,10 +155,11 @@ export const fetchInstantProducts = createAsyncThunk<
'commerce/search/fetchInstantProducts',
async (payload, {getState, rejectWithValue, extra}) => {
const state = getState();
console.log(state);
const {apiClient, navigatorContext} = extra;
const {q} = payload;
const fetched = await apiClient.productSuggestions({
...buildCommerceAPIRequest(state, navigatorContext),
...buildInstantProductsAPIRequest(state, navigatorContext),
query: q,
});

Expand Down

0 comments on commit fec5a56

Please sign in to comment.