diff --git a/projects/ch14/src/app/pos/pos.module.ts b/projects/ch14/src/app/pos/pos.module.ts index 28afbb88..9843249a 100644 --- a/projects/ch14/src/app/pos/pos.module.ts +++ b/projects/ch14/src/app/pos/pos.module.ts @@ -1,11 +1,12 @@ import { CommonModule } from '@angular/common' import { NgModule } from '@angular/core' +import { AppMaterialModule } from '../app-material.module' import { PosRoutingModule } from './pos-routing.module' import { PosComponent } from './pos/pos.component' @NgModule({ declarations: [PosComponent], - imports: [CommonModule, PosRoutingModule], + imports: [CommonModule, PosRoutingModule, AppMaterialModule], }) export class PosModule {} diff --git a/projects/ch14/src/app/pos/pos/pos.component.html b/projects/ch14/src/app/pos/pos/pos.component.html index 25094919..b3d41a50 100644 --- a/projects/ch14/src/app/pos/pos/pos.component.html +++ b/projects/ch14/src/app/pos/pos/pos.component.html @@ -2,3 +2,8 @@

+

+ +

diff --git a/projects/ch14/src/app/pos/pos/pos.component.ts b/projects/ch14/src/app/pos/pos/pos.component.ts index 63480938..83ad22b0 100644 --- a/projects/ch14/src/app/pos/pos/pos.component.ts +++ b/projects/ch14/src/app/pos/pos/pos.component.ts @@ -1,12 +1,57 @@ -import { Component, OnInit } from '@angular/core' +import { Component, OnDestroy, OnInit } from '@angular/core' +import { filter, tap } from 'rxjs/operators' +import { SubSink } from 'subsink' + +import { UiService } from '../../common/ui.service' +import { ITransaction } from '../transaction/transaction' +import { TransactionType } from '../transaction/transaction.enum' +import { TransactionService } from '../transaction/transaction.service' + +interface IEvent { + event: 'checkoutCompleted' | 'checkoutInitiated' +} +declare let dataLayer: IEvent[] @Component({ selector: 'app-pos', templateUrl: './pos.component.html', styleUrls: ['./pos.component.css'], }) -export class PosComponent implements OnInit { - constructor() {} +export class PosComponent implements OnInit, OnDestroy { + private subs = new SubSink() + currentTransaction: ITransaction + constructor( + private transactionService: TransactionService, + private uiService: UiService + ) {} + + ngOnInit() { + this.currentTransaction = { + paymentAmount: 25.78, + paymentType: TransactionType.Credit, + } as ITransaction + } + + checkout(transaction: ITransaction) { + this.uiService.showToast('Checkout initiated') + dataLayer.push({ + event: 'checkoutInitiated', + }) + this.subs.sink = this.transactionService + .processTransaction(transaction) + .pipe( + filter((tx) => tx != null || tx !== undefined), + tap((transactionId) => { + this.uiService.showToast('Checkout completed') + dataLayer.push({ + event: 'checkoutCompleted', + }) + }) + ) + .subscribe() + } - ngOnInit(): void {} + ngOnDestroy() { + this.subs.unsubscribe() + } } diff --git a/projects/ch14/src/app/pos/transaction/transaction.enum.ts b/projects/ch14/src/app/pos/transaction/transaction.enum.ts new file mode 100644 index 00000000..08f491d0 --- /dev/null +++ b/projects/ch14/src/app/pos/transaction/transaction.enum.ts @@ -0,0 +1,5 @@ +export enum TransactionType { + Cash, + Credit, + LemonCoin, +} diff --git a/projects/ch14/src/app/pos/transaction/transaction.service.spec.ts b/projects/ch14/src/app/pos/transaction/transaction.service.spec.ts new file mode 100644 index 00000000..8cc4edd5 --- /dev/null +++ b/projects/ch14/src/app/pos/transaction/transaction.service.spec.ts @@ -0,0 +1,17 @@ +import { HttpClientTestingModule } from '@angular/common/http/testing' +import { TestBed, inject } from '@angular/core/testing' + +import { TransactionService } from './transaction.service' + +describe('TransactionService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [TransactionService], + }) + }) + + it('should be created', inject([TransactionService], (service: TransactionService) => { + expect(service).toBeTruthy() + })) +}) diff --git a/projects/ch14/src/app/pos/transaction/transaction.service.ts b/projects/ch14/src/app/pos/transaction/transaction.service.ts new file mode 100644 index 00000000..5471c729 --- /dev/null +++ b/projects/ch14/src/app/pos/transaction/transaction.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@angular/core' +import { BehaviorSubject, Observable } from 'rxjs' + +import { ITransaction } from './transaction' + +@Injectable({ + providedIn: 'root', +}) +export class TransactionService { + constructor() {} + + processTransaction(transaction: ITransaction): Observable { + return new BehaviorSubject('5a6352c6810c19729de860ea').asObservable() + } +} diff --git a/projects/ch14/src/app/pos/transaction/transaction.ts b/projects/ch14/src/app/pos/transaction/transaction.ts new file mode 100644 index 00000000..d8034faa --- /dev/null +++ b/projects/ch14/src/app/pos/transaction/transaction.ts @@ -0,0 +1,7 @@ +import { TransactionType } from './transaction.enum' + +export interface ITransaction { + paymentType: TransactionType + paymentAmount: number + transactionId?: string +} diff --git a/projects/ch14/src/index.html b/projects/ch14/src/index.html index fc477e81..5d8d3b8b 100644 --- a/projects/ch14/src/index.html +++ b/projects/ch14/src/index.html @@ -2,6 +2,25 @@ + + + Ch14: LemonMart @@ -20,6 +39,12 @@ + + + diff --git a/src/app/pos/pos/pos.component.html b/src/app/pos/pos/pos.component.html index 0f0d0405..b3d41a50 100644 --- a/src/app/pos/pos/pos.component.html +++ b/src/app/pos/pos/pos.component.html @@ -4,6 +4,6 @@