projects/storefrontlib/cms-components/myaccount/my-coupons/my-coupons.component.ts
| selector | cx-my-coupons |
| templateUrl | ./my-coupons.component.html |
Properties |
|
Methods |
constructor(couponService: CustomerCouponService, myCouponsComponentService: MyCouponsComponentService)
|
|||||||||
|
Parameters :
|
| ngOnDestroy |
ngOnDestroy()
|
|
Returns :
void
|
| ngOnInit |
ngOnInit()
|
|
Returns :
void
|
| notificationChange | |||||
notificationChange(undefined: literal type)
|
|||||
|
Parameters :
Returns :
void
|
| pageChange | ||||||
pageChange(page: number)
|
||||||
|
Parameters :
Returns :
void
|
| sortChange | ||||||
sortChange(sort: string)
|
||||||
|
Parameters :
Returns :
void
|
| Private subscriptionFail | ||||||
subscriptionFail(error: boolean)
|
||||||
|
Parameters :
Returns :
void
|
| couponResult$ |
Type : Observable<CustomerCouponSearchResult>
|
| couponsLoading$ |
Type : Observable<boolean>
|
| couponSubscriptionLoading$ |
Type : Observable<boolean>
|
| iconTypes |
Default value : ICON_TYPE
|
| Private PAGE_SIZE |
Type : number
|
Default value : 10
|
| pagination |
Type : PaginationModel
|
| sort |
Type : string
|
Default value : 'byStartDateAsc'
|
| sortLabels |
Type : Observable<literal type>
|
| Private sortMapping |
Type : object
|
Default value : {
byStartDateAsc: 'startDate:asc',
byStartDateDesc: 'startDate:desc',
byEndDateAsc: 'endDate:asc',
byEndDateDesc: 'endDate:desc',
}
|
| Private subscriptions |
Default value : new Subscription()
|
import { Component, OnDestroy, OnInit } from '@angular/core';
import {
CustomerCouponSearchResult,
CustomerCouponService,
PaginationModel,
} from '@spartacus/core';
import { combineLatest, Observable, Subscription } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { ICON_TYPE } from '../../misc/icon/icon.model';
import { MyCouponsComponentService } from './my-coupons.component.service';
@Component({
selector: 'cx-my-coupons',
templateUrl: './my-coupons.component.html',
})
export class MyCouponsComponent implements OnInit, OnDestroy {
couponResult$: Observable<CustomerCouponSearchResult>;
couponsLoading$: Observable<boolean>;
couponSubscriptionLoading$: Observable<boolean>;
iconTypes = ICON_TYPE;
private subscriptions = new Subscription();
private PAGE_SIZE = 10;
private sortMapping = {
byStartDateAsc: 'startDate:asc',
byStartDateDesc: 'startDate:desc',
byEndDateAsc: 'endDate:asc',
byEndDateDesc: 'endDate:desc',
};
sort = 'byStartDateAsc';
sortOptions = [
{
code: 'byStartDateAsc',
selected: false,
},
{
code: 'byStartDateDesc',
selected: false,
},
{
code: 'byEndDateAsc',
selected: false,
},
{
code: 'byEndDateDesc',
selected: false,
},
];
pagination: PaginationModel;
sortLabels: Observable<{
byStartDateAsc: string;
byStartDateDesc: string;
byEndDateAsc: string;
byEndDateDesc: string;
}>;
constructor(
protected couponService: CustomerCouponService,
protected myCouponsComponentService: MyCouponsComponentService
) {}
ngOnInit(): void {
this.couponResult$ = this.couponService
.getCustomerCoupons(this.PAGE_SIZE)
.pipe(
tap(
(coupons) =>
(this.pagination = {
currentPage: coupons.pagination.page,
pageSize: coupons.pagination.count,
totalPages: coupons.pagination.totalPages,
totalResults: coupons.pagination.totalCount,
sort: this.sort,
})
)
);
this.couponsLoading$ = this.couponService.getCustomerCouponsLoading();
this.couponSubscriptionLoading$ = combineLatest([
this.couponService.getSubscribeCustomerCouponResultLoading(),
this.couponService.getUnsubscribeCustomerCouponResultLoading(),
]).pipe(
map(([subscribing, unsubscribing]) => subscribing || unsubscribing)
);
this.sortLabels = this.myCouponsComponentService.getSortLabels();
this.subscriptions
.add(
this.couponService
.getSubscribeCustomerCouponResultError()
.subscribe((error) => {
this.subscriptionFail(error);
})
)
.add(
this.couponService
.getUnsubscribeCustomerCouponResultError()
.subscribe((error) => {
this.subscriptionFail(error);
})
);
}
private subscriptionFail(error: boolean) {
if (error) {
this.couponService.loadCustomerCoupons(this.PAGE_SIZE);
}
}
sortChange(sort: string): void {
this.sort = sort;
this.couponService.loadCustomerCoupons(
this.PAGE_SIZE,
this.pagination.currentPage,
this.sortMapping[sort]
);
}
pageChange(page: number): void {
this.couponService.loadCustomerCoupons(
this.PAGE_SIZE,
page,
this.sortMapping[this.sort]
);
}
notificationChange({
couponId,
notification,
}: {
couponId: string;
notification: boolean;
}): void {
if (notification) {
this.couponService.subscribeCustomerCoupon(couponId);
} else {
this.couponService.unsubscribeCustomerCoupon(couponId);
}
}
ngOnDestroy(): void {
this.subscriptions.unsubscribe();
}
}
<div class="cx-section">
<ng-container *ngIf="!(couponsLoading$ | async); else loading">
<ng-container *ngIf="couponResult$ | async as couponResult">
<div class="cx-my-coupons-header">
<h3>{{ 'myCoupons.myCoupons' | cxTranslate }}</h3>
</div>
<ng-container
*ngIf="couponResult.pagination.totalCount > 0; else noCoupons"
>
<div class="cx-my-coupons-sort top row">
<label
class="
cx-my-coupons-form-group
form-group
col-sm-12 col-md-4 col-lg-4
"
>
<span>{{ 'myCoupons.sortBy' | cxTranslate }}</span>
<cx-sorting
[sortOptions]="sortOptions"
[sortLabels]="sortLabels | async"
(sortListEvent)="sortChange($event)"
[selectedOption]="sort"
>
</cx-sorting>
</label>
<div class="cx-my-coupons-pagination cx-mycoupon-thead-mobile">
<cx-pagination
[pagination]="pagination"
(viewPageEvent)="pageChange($event)"
></cx-pagination>
</div>
</div>
<div class="row cx-coupon-deck">
<div
*ngFor="let coupon of couponResult.coupons"
class="col-md-6 cx-coupon-card"
>
<cx-coupon-card
[coupon]="coupon"
[couponSubscriptionLoading$]="couponSubscriptionLoading$"
(notificationChanged)="notificationChange($event)"
></cx-coupon-card>
</div>
</div>
<div class="cx-my-coupons-sort bottom row">
<label
class="
cx-my-coupons-form-group
form-group
cx-mycoupon-thead-mobile
col-sm-12 col-md-4 col-lg-4
"
>
<span>{{ 'myCoupons.sortBy' | cxTranslate }}</span>
<cx-sorting
[sortOptions]="sortOptions"
[sortLabels]="sortLabels | async"
(sortListEvent)="sortChange($event)"
[selectedOption]="sort"
placeholder="{{ 'myCoupons.sortBy' | cxTranslate }}"
>
</cx-sorting>
</label>
<div class="cx-my-coupons-pagination">
<cx-pagination
[pagination]="pagination"
(viewPageEvent)="pageChange($event)"
></cx-pagination>
</div>
</div>
<div class="cx-my-coupons-notes">
<span>
<cx-icon [type]="iconTypes.INFO"></cx-icon>
{{ 'myCoupons.notesPreffix' | cxTranslate
}}<a [routerLink]="['/my-account/notification-preference']">{{
'myCoupons.notesLink' | cxTranslate
}}</a
>{{ 'myCoupons.notesSuffix' | cxTranslate }}</span
>
</div>
</ng-container>
</ng-container>
<ng-template #noCoupons>
<section>
<p class="cx-section-msg">
{{ 'myCoupons.noCouponsMessage' | cxTranslate }}
</p>
</section>
</ng-template>
</ng-container>
<ng-template #loading>
<div class="col-md-12 cx-coupon-spinner">
<cx-spinner></cx-spinner>
</div>
</ng-template>
</div>