Skip to content

Commit

Permalink
feat(commerce): product seed passed through as recs context (#3904)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenKandelaars authored May 7, 2024
1 parent 01a9035 commit 284eb1b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/headless/src/api/commerce/commerce-api-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ export interface ContextParam {
context: ContextParams;
}

export interface ProductParam {
productId: string;
}

export interface ContextParams {
view: ViewParams;
user?: UserParams;
product?: ProductParam;
cart?: CartItemParam[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export interface RecommendationsOptions {
* The unique identifier of the recommendations slot (e.g., `b953ab2e-022b-4de4-903f-68b2c0682942`).
*/
slotId: string;
/**
* The unique identifier of the product to use for seeded recommendations.
*/
productId?: string;
}

interface RecommendationsProps {
Expand Down Expand Up @@ -85,7 +89,7 @@ export function buildRecommendations(
const controller = buildController(engine);
const {dispatch} = engine;

const {slotId} = props.options;
const {slotId, productId} = props.options;
dispatch(registerRecommendationsSlot({slotId}));

const recommendationStateSelector = createSelector(
Expand All @@ -107,7 +111,7 @@ export function buildRecommendations(
return recommendationStateSelector(engine[stateKey]);
},

refresh: () => dispatch(fetchRecommendations({slotId})),
refresh: () => dispatch(fetchRecommendations({slotId, productId})),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ export interface QueryRecommendationsCommerceAPIThunkReturn {

const buildRecommendationCommerceAPIRequest = async (
slotId: string,
state: StateNeededByQueryCommerceAPI
state: StateNeededByQueryCommerceAPI,
productId?: string
): Promise<CommerceRecommendationsRequest> => {
const commerceAPIRequest = await buildBaseCommerceAPIRequest(state, slotId);
return {
...commerceAPIRequest,
context: {
...commerceAPIRequest.context,
...(productId ? {product: {productId}} : {}),
},
slotId,
};
};
Expand All @@ -38,6 +43,7 @@ export interface FetchRecommendationsActionCreatorPayload {
* The unique identifier of the recommendations slot (e.g., `b953ab2e-022b-4de4-903f-68b2c0682942`).
*/
slotId: string;
productId?: string;
}

export const fetchRecommendations = createAsyncThunk<
Expand All @@ -47,10 +53,11 @@ export const fetchRecommendations = createAsyncThunk<
>(
'commerce/recommendations/fetch',
async (payload, {getState, rejectWithValue, extra: {apiClient}}) => {
const slotId = payload.slotId;
const {slotId, productId} = payload;
const request = await buildRecommendationCommerceAPIRequest(
slotId,
getState()
getState(),
productId
);
const fetched = await apiClient.getRecommendations(request);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Schema} from '@coveo/bueno';
import {StringValue} from '@coveo/bueno';
import {RecommendationsOptions} from '../../../controllers/commerce/recommendations/headless-recommendations';
import {requiredNonEmptyString} from '../../../utils/validate-payload';

export const recommendationsSlotDefinition = {
slotId: requiredNonEmptyString,
productId: new StringValue({required: false, emptyAllowed: false}),
};

export const recommendationsOptionsSchema = new Schema<RecommendationsOptions>(
Expand Down

0 comments on commit 284eb1b

Please sign in to comment.