import { Component , OnInit } from '@angular/core' ; import { Transaction } from 'src/app/models/transaction' ; import { TradeService } from '../../../services/trade.service' ; import { Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; @ Component ( { selector : 'app-blotter-view' , templateUrl : './blotter-view.component.html' , styleUrls : [ './blotter-view.component.css' ] } ) export class BlotterViewComponent implements OnInit { filter = { ccyPair : '' , date : 0 } ; private unsubscribe = new Subject ( ) ; transactions : Transaction [ ] = [ ] ; private initialTransactions : Transaction [ ] = [ ] ; currenciesPairs : ( string | undefined ) [ ] = [ ] ; constructor ( private tradeService ) { } ngOnInit ( ) { this . startPooling ( ) ; } startPooling ( ) { this . tradeService . getTransactionsPolling ( ) . pipe ( takeUntil ( this . unsubscribe ) ) . subscribe ( response => { const transactionsWithCcyPair = response . map ( transaction => ( { ... transaction , ccyPair : `${ transaction . primaryCcy }/${ transaction . secondaryCcy }` } ) ) ; this . transactions = transactionsWithCcyPair ; this . initialTransactions = [ ... transactionsWithCcyPair ] ; this . currenciesPairs = this . transactions . map ( transaction => transaction . ccyPair ) . filter ( ( x , i , a ) => x && a . indexOf ( x ) === i ) ; this . filterBy ( ) ; } ) ; } getDateWithoutHourAndMinuteAndSeconds ( date ) { return new Date ( new Date ( date ) . getFullYear ( ) , new Date ( date ) . getMonth ( ) , new Date ( date ) . getDay ( ) ) ; } filterBy ( ) { this . transactions = this . initialTransactions . filter ( transaction => this . filter . ccyPair && transaction . ccyPair === this . filter . ccyPair || ! this . filter . ccyPair ) . filter ( transaction => this . filter . date && this . getDateWithoutHourAndMinuteAndSeconds ( transaction . date ) . getTime ( ) === this . getDateWithoutHourAndMinuteAndSeconds ( this . filter . date ) . getTime ( ) || ! this . filter . date ) ; } ngOnDestroy ( ) { this . unsubscribe . next ( '' ) ; this . unsubscribe . complete ( ) ; } }