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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x | import { Location } from '@angular/common';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
Input,
OnInit,
ViewChild,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap';
import { Observable, concat, of, timer } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
import { whenTruthy } from 'ish-core/utils/operators';
@Component({
selector: 'ish-mini-basket',
templateUrl: './mini-basket.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MiniBasketComponent implements OnInit {
@ViewChild('miniBasketDropdown', { static: true }) miniBasketDropdown!: NgbDropdown;
basketAnimation$: Observable<string>;
itemTotal$: Observable<PriceItem>;
itemCount$: Observable<number>;
basketLoading$: Observable<boolean>;
@Input() view: 'auto' | 'small' | 'full' = 'auto';
private basketError$: Observable<HttpError>;
private destroyRef = inject(DestroyRef);
constructor(
private checkoutFacade: CheckoutFacade,
private appFacade: AppFacade,
private location: Location,
private cdRef: ChangeDetectorRef
) {}
ngOnInit() {
this.itemCount$ = this.checkoutFacade.basketItemCount$;
this.itemTotal$ = this.checkoutFacade.basketItemTotal$;
this.basketError$ = this.checkoutFacade.basketError$;
this.basketLoading$ = this.checkoutFacade.basketLoading$;
this.basketError$
.pipe(
whenTruthy(),
filter(() => this.location.path() !== '/basket'),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(() => this.open());
this.basketAnimation$ = this.checkoutFacade.basketChange$.pipe(
filter(() => !this.location.path().startsWith('/basket')),
switchMap(() => concat(of('mini-basket-animation'), timer(2500).pipe(map(() => ''))))
);
this.basketAnimation$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(animation => {
if (animation) {
this.open();
} else {
this.collapse();
}
});
this.appFacade.routingInProgress$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.collapse();
});
}
/**
* Collapse the mini basket programmatically.
*/
collapse() {
this.miniBasketDropdown.close();
this.cdRef.markForCheck();
}
/**
* Open the mini basket programmatically.
*/
// visible-for-testing
open() {
this.miniBasketDropdown.open();
this.cdRef.markForCheck();
}
}
|