Skip to content

Commit

Permalink
fix: disable add-to-basket when quantity is zero (#1066)
Browse files Browse the repository at this point in the history
(cherry picked from commit d655d63)
  • Loading branch information
dhhyi authored and SGrueber committed Aug 11, 2022
1 parent e0aa856 commit edf1573
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/app/core/facades/product-context.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ export class ProductContextFacade extends RxState<ProductContext> implements OnD
(state, minOrderQuantity) => (state.quantity ??= minOrderQuantity)
);

this.connect(
'quantity',
this.select('children').pipe(
map(children => Object.values(children)),
skipWhile(children => !children?.length),
map(children =>
children.reduce(
(sum, child) =>
sum +
(Number.isInteger(child.quantity) && !child.hasQuantityError && !child.hasProductError
? child.quantity
: 0),
0
)
),
distinctUntilChanged()
)
);

this.connect(
'hasProductError',
this.select('product').pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Product Add To Basket Component', () => {
context = mock(ProductContextFacade);
when(context.select('displayProperties', 'addToBasket')).thenReturn(of(true));
when(context.select('product')).thenReturn(of({} as ProductView));
when(context.select('quantity')).thenReturn(of(1));
when(context.select('hasQuantityError')).thenReturn(of(false));
when(context.select('hasProductError')).thenReturn(of(false));

Expand Down Expand Up @@ -78,6 +79,12 @@ describe('Product Add To Basket Component', () => {
expect(element.querySelector('button').disabled).toBeTruthy();
});

it('should show disabled button when context has no quantity', () => {
when(context.select('quantity')).thenReturn(of(0));
fixture.detectChanges();
expect(element.querySelector('button').disabled).toBeTruthy();
});

it('should use default translation when nothing is configured', () => {
fixture.detectChanges();
expect(element.textContent).toMatchInlineSnapshot(`"product.add_to_cart.link"`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export class ProductAddToBasketComponent implements OnInit, OnDestroy {

const hasQuantityError$ = this.context.select('hasQuantityError');
const hasProductError$ = this.context.select('hasProductError');
this.buttonDisabled$ = combineLatest([
this.displaySpinner$.pipe(startWith(false)),
hasQuantityError$.pipe(),
hasProductError$.pipe(),
]).pipe(map(conditions => conditions.some(c => !!c)));
const hasNoQuantity$ = this.context.select('quantity').pipe(map(quantity => quantity <= 0));
const loading$ = this.displaySpinner$.pipe(startWith(false));
this.buttonDisabled$ = combineLatest([loading$, hasQuantityError$, hasProductError$, hasNoQuantity$]).pipe(
map(conditions => conditions.some(c => !!c))
);
}

addToBasket() {
Expand Down

0 comments on commit edf1573

Please sign in to comment.