import { DaffProductCompositePageEffects } from '@daffodil/product-composite/routing'
Handles composite product specific actions for the product page.
@Injectable()
class DaffProductCompositePageEffects<<T extends DaffCompositeProduct = DaffCompositeProduct>> {
preselectCompositeOptions$: Observable<typeof EMPTY | DaffCompositeProductApplyOption<T>> = createEffect(() => this.actions$.pipe(
ofType(DaffProductPageActionTypes.ProductPageLoadSuccessAction),
switchMap((action: DaffProductPageLoadSuccess<T>) => {
const queryParam = this.paramGetter.get();
// get the product corresponding to the current product page
const product: DaffCompositeProduct = action.payload.products.filter(({ id }) => id === action.payload.id)[0];
// if we don't have a query param set or if the product isn't composite,
// we have nothing to do
if (!queryParam || product?.type !== DaffProductTypeEnum.Composite) {
return EMPTY;
}
let selection: DaffProductCompositeSelectionPayload;
try {
selection = this.config.compositeSelectionQueryParamDecode(queryParam, product);
} catch (error) {
return EMPTY;
}
const applyActions = Object.keys(selection).reduce<DaffCompositeProductApplyOption<T>[]>(
(actions, itemId) => actions.concat(buildApplyActions(product, itemId, selection[itemId])),
[],
);
// dispatch each of the apply actions into the action stream
return of(...applyActions);
}),
))
}
Observable<typeof EMPTY | DaffCompositeProductApplyOption<T>>
Default | createEffect(() => this.actions$.pipe(
ofType(DaffProductPageActionTypes.ProductPageLoadSuccessAction),
switchMap((action: DaffProductPageLoadSuccess<T>) => {
const queryParam = this.paramGetter.get();
// get the product corresponding to the current product page
const product: DaffCompositeProduct = action.payload.products.filter(({ id }) => id === action.payload.id)[0];
// if we don't have a query param set or if the product isn't composite,
// we have nothing to do
if (!queryParam || product?.type !== DaffProductTypeEnum.Composite) {
return EMPTY;
}
let selection: DaffProductCompositeSelectionPayload;
try {
selection = this.config.compositeSelectionQueryParamDecode(queryParam, product);
} catch (error) {
return EMPTY;
}
const applyActions = Object.keys(selection).reduce<DaffCompositeProductApplyOption<T>[]>(
(actions, itemId) => actions.concat(buildApplyActions(product, itemId, selection[itemId])),
[],
);
// dispatch each of the apply actions into the action stream
return of(...applyActions);
}),
)) |
---|---|
Description | Applies composite item options based on the value of the configured query param. |