Skip to content

Commit

Permalink
fix: respect ICM setting "Add Product Behavior" when adding basket it…
Browse files Browse the repository at this point in the history
…ems (#905)
  • Loading branch information
dhhyi authored Oct 11, 2021
1 parent 072f747 commit a5fd804
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ describe('Basket Items Effects', () => {
});

describe('addProductToBasket$', () => {
it('should accumulate AddProductToBasket to a single AddItemsToBasket action', () => {
it('should accumulate AddProductToBasket to a single action', () => {
store$.dispatch(loadProductSuccess({ product: { sku: 'SKU1', packingUnit: 'pcs.' } as Product }));
store$.dispatch(loadProductSuccess({ product: { sku: 'SKU2', packingUnit: 'pcs.' } as Product }));
const action1 = addProductToBasket({ sku: 'SKU1', quantity: 1 });
const action2 = addProductToBasket({ sku: 'SKU2', quantity: 1 });
const completion = addItemsToBasket({
items: [
{ sku: 'SKU2', quantity: 2, unit: 'pcs.' },
{ sku: 'SKU1', quantity: 2, unit: 'pcs.' },
{ sku: 'SKU2', quantity: 1, unit: 'pcs.' },
{ sku: 'SKU1', quantity: 1, unit: 'pcs.' },
{ sku: 'SKU2', quantity: 1, unit: 'pcs.' },
{ sku: 'SKU1', quantity: 1, unit: 'pcs.' },
],
});
actions$ = hot(' -b-a-b-a--|', { a: action1, b: action2 });
Expand Down
25 changes: 7 additions & 18 deletions src/app/core/store/customer/basket/basket-items.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
map,
mapTo,
mergeMap,
reduce,
switchMap,
toArray,
window,
withLatestFrom,
} from 'rxjs/operators';
Expand Down Expand Up @@ -53,30 +53,19 @@ export class BasketItemsEffects {

/**
* Add a product to the current basket.
* Triggers the internal AddItemsToBasket action that handles the actual adding of the product to the basket.
* Triggers the internal action that handles the actual adding of the product to the basket.
*/
addProductToBasket$ = createEffect(() =>
this.actions$.pipe(
ofType(addProductToBasket),
mapToPayload(),
// add unit
withLatestFrom(this.store.pipe(select(getProductEntities))),
map(([val, entities]) => ({ ...val, unit: entities[val.sku] && entities[val.sku].packingUnit })),
// accumulate all actions
window(this.actions$.pipe(ofType(addProductToBasket), debounceTime(1000))),
mergeMap(window$ =>
window$.pipe(
withLatestFrom(this.store.pipe(select(getProductEntities))),
// accumulate changes
reduce((acc, [val, entities]) => {
const element = acc.find(x => x.sku === val.sku);
if (element) {
element.quantity += val.quantity;
} else {
acc.push({ ...val, unit: entities[val.sku] && entities[val.sku].packingUnit });
}
return acc;
}, []),
map(items => addItemsToBasket({ items }))
)
)
mergeMap(window$ => window$.pipe(toArray())),
map(items => addItemsToBasket({ items }))
)
);

Expand Down

0 comments on commit a5fd804

Please sign in to comment.