All files / src/app/shared/components/product/product-quantity product-quantity.component.ts

72.72% Statements 24/33
50% Branches 5/10
50% Functions 6/12
72.41% Lines 21/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 748x 8x 8x 8x 8x   8x               8x 5x 5x                             5x     5x 10x 5x 5x 5x 5x 5x   5x 1x     5x 5x                                                    
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { range } from 'lodash-es';
import { Observable, combineLatest } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { v4 as uuid } from 'uuid';
 
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
 
@Component({
  selector: 'ish-product-quantity',
  templateUrl: './product-quantity.component.html',
  styleUrls: ['./product-quantity.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductQuantityComponent implements OnInit {
  @Input() type: 'input' | 'select' | 'counter' = 'counter';
  @Input() elementId: string = uuid();
 
  visible$: Observable<boolean>;
  quantity$: Observable<number>;
  min$: Observable<number>;
  max$: Observable<number>;
  step$: Observable<number>;
  hasQuantityError$: Observable<boolean>;
  quantityError$: Observable<string>;
 
  selectValues$: Observable<number[]>;
 
  cannotIncrease$: Observable<boolean>;
  cannotDecrease$: Observable<boolean>;
 
  constructor(private context: ProductContextFacade) {}
 
  ngOnInit() {
    this.visible$ = this.context.select('displayProperties', 'quantity');
    this.quantity$ = this.context.select('quantity').pipe(filter(n => typeof n === 'number' && !isNaN(n)));
    this.min$ = this.context.select('minQuantity');
    this.max$ = this.context.select('maxQuantity');
    this.step$ = this.context.select('stepQuantity');
    this.hasQuantityError$ = this.context.select('hasQuantityError');
    this.quantityError$ = this.context.select('quantityError');
 
    this.selectValues$ = combineLatest([this.min$, this.max$, this.step$]).pipe(
      map(([min, max, step]) => range(min, max + 1, step))
    );
 
    this.cannotIncrease$ = combineLatest([this.max$, this.quantity$]).pipe(map(([max, quantity]) => quantity >= max));
    this.cannotDecrease$ = combineLatest([this.min$, this.quantity$]).pipe(map(([min, quantity]) => quantity <= min));
  }
 
  private setValue(value: number) {
    this.context.set('quantity', () => value);
  }
 
  private setNextValue(value: number) {
    const max = this.context.get('maxQuantity');
    const min = this.context.get('minQuantity');
    const step = this.context.get('stepQuantity');
    this.setValue(value > max ? max : value < min ? min : value - (value % step));
  }
 
  increase() {
    this.setNextValue(this.context.get('quantity') + this.context.get('stepQuantity'));
  }
 
  decrease() {
    this.setNextValue(this.context.get('quantity') - this.context.get('stepQuantity'));
  }
 
  change(target: EventTarget) {
    this.setValue(Number.parseInt((target as HTMLDataElement).value, 10));
  }
}