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 | 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
Input,
OnInit,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { BasketView } from 'ish-core/models/basket/basket.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { whenTruthy } from 'ish-core/utils/operators';
/**
* The Basket Promotion Component displays a promotion code input.
* It provides the add promotion code functionality
*
* @example
* <ish-basket-promotion-code></ish-basket-promotion-code>
*/
@Component({
selector: 'ish-basket-promotion-code',
templateUrl: './basket-promotion-code.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasketPromotionCodeComponent implements OnInit {
@Input() toast = true;
private basket$: Observable<BasketView>;
promotionError$: Observable<HttpError>;
codeInput: FormControl;
isCollapsed = true;
codeMaxLength = 128;
private basketPromoCodes: string[];
private lastEnteredPromoCode = '';
private destroyRef = inject(DestroyRef);
constructor(private checkoutFacade: CheckoutFacade, private cd: ChangeDetectorRef) {}
ngOnInit() {
this.basket$ = this.checkoutFacade.basket$;
this.promotionError$ = this.checkoutFacade.promotionError$;
this.codeInput = new FormControl('', [Validators.required, Validators.maxLength(this.codeMaxLength)]);
// update emitted to display spinning animation
this.basket$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(basket => {
this.basketPromoCodes = basket.promotionCodes;
Iif (this.displaySuccessMessage) {
this.codeInput.reset();
this.isCollapsed = true;
}
this.cd.detectChanges();
});
}
/**
* submit promotion code when add promotion code was clicked.
*/
submitPromotionCode() {
// prevent success message if the user enters the same promo code twice
Iif (!this.basketPromoCodes?.includes(this.codeInput.value)) {
this.lastEnteredPromoCode = this.codeInput.value;
}
this.checkoutFacade.addPromotionCodeToBasket(this.codeInput.value);
// prevent further form submit
return false;
}
get displaySuccessMessage(): boolean {
return this.basketPromoCodes?.includes(this.lastEnteredPromoCode);
}
}
|