Skip to content

Commit

Permalink
recs properly refresh with the productId
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Dec 16, 2024
1 parent e944b99 commit e85ce25
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
18 changes: 4 additions & 14 deletions packages/headless-react/src/ssr-commerce/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,10 @@ export function buildProviderWithDefinition(looseDefinition: LooseDefinition) {
break;

case Kind.Recommendations:
// if (
// //@ts-expect-error normal
// controller.state.productId &&
// //@ts-expect-error normal
// controller.state.productId !== ''
// ) {
// console.log('controller', controller);
// }
// hydrateControllers[key] = {
// options: {
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
// productId: (controllers as any)[key].state.productId,
// },
// };
hydrateControllers[key] = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
productId: (controllers as any)[key].state.productId,
};
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
} from '../../../app/commerce-ssr-engine/types/common.js';
import {Kind} from '../../../app/commerce-ssr-engine/types/kind.js';
import {ControllerWithKind} from '../../../app/ssr-engine/types/common.js';
// import {Kind} from '../../../app/commerce-ssr-engine/types/kind.js';
// import {ControllerWithKind} from '../../../app/ssr-engine/types/common.js';
import {
RecommendationsOptions,
RecommendationsState,
Expand Down Expand Up @@ -57,12 +55,19 @@ export function defineRecommendations(
options: Omit<RecommendationsOptions, 'slotId'>
) => {
const staticOptions = props.options;
return {
...buildRecommendations(engine, {
options: {...staticOptions, ...options},
}),
_kind: Kind.Recommendations,
};
const controller = buildRecommendations(engine, {
options: {...staticOptions, ...options},
});
const copy = Object.defineProperties(
{},
Object.getOwnPropertyDescriptors(controller)
);

Object.defineProperty(copy, '_kind', {
value: Kind.Recommendations,
});

return copy as typeof controller & {_kind: Kind.Recommendations};
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface RecommendationsState {
error: CommerceAPIErrorStatusResponse | null;
isLoading: boolean;
responseId: string;
// productId: string;
productId: string;
}

export interface RecommendationsOptions {
Expand Down Expand Up @@ -134,8 +134,7 @@ export function buildRecommendations(
const {dispatch} = engine;

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

const recommendationStateSelector = createSelector(
(state: CommerceEngineState) => state.recommendations[slotId]!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const fetchMoreRecommendations = createAsyncThunk<

export interface SlotIdPayload {
slotId: string;
// productId?: string;
productId?: string;
}

export type RegisterRecommendationsSlotPayload = SlotIdPayload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ export const recommendationsReducer = createReducer(
builder
.addCase(registerRecommendationsSlot, (state, action) => {
const slotId = action.payload.slotId;
// const productId = action.payload.productId;
const productId = action.payload.productId;

if (slotId in state) {
return;
}

//send productId here
state[slotId] = buildRecommendationsSlice();
state[slotId] = buildRecommendationsSlice({productId});
})
.addCase(fetchRecommendations.rejected, (state, action) => {
handleError(state, action.meta.arg.slotId, action.payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface RecommendationsSlice {
isLoading: boolean;
responseId: string;
products: Product[];
// productId: string;
productId: string;
}

/**
Expand All @@ -26,5 +26,5 @@ export const getRecommendationsSliceInitialState =
isLoading: false,
responseId: '',
products: [],
// productId: '',
productId: '',
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default function PopularViewed() {
return (
<>
<ul>
This is a recommendation
<h3>{state.headline}</h3>
{state.products.map((product) => (
<li key={product.ec_product_id}>
Expand Down

0 comments on commit e85ce25

Please sign in to comment.