import { Component , Input , ViewChild , AfterViewInit } from '@angular/core' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatTableDataSource } from '@angular/material/table' ; import { Transaction , Account , isMultiOutSameTransaction , isMultiOutTransaction , getRecipientsAmount , TransactionMiningSubtype , TransactionType } from '@signumjs/core' ; import { Amount , ChainTime } from '@signumjs/util' ; import { UtilService } from 'app/util.service' ; import { takeUntil } from 'rxjs/operators' ; import { UnsubscribeOnDestroy } from '../../../util/UnsubscribeOnDestroy' ; import { StoreService } from '../../../store/store.service' ; import { formatDate } from '@angular/common' ; @ Component ( { selector : 'app-transaction-table' , styleUrls : [ './transaction-table.component.scss' ] , templateUrl : './transaction-table.component.html' , } ) export class TransactionTableComponent extends UnsubscribeOnDestroy implements AfterViewInit { @ Input ( ) dataSource : MatTableDataSource < Transaction > ; @ Input ( ) displayedColumns = [ 'transaction_id' , 'timestamp' , 'type' , 'amount' , 'account' , 'confirmations' ] ; @ Input ( ) paginationEnabled = true ; @ Input ( ) account : Account ; @ ViewChild ( MatPaginator , { static : false } ) paginator : MatPaginator ; public locale : string ; constructor ( private utilService , private storeService ) { super ( ) ; this . storeService . settings . pipe ( takeUntil ( this . unsubscribeAll ) ) . subscribe ( ( { language } ) => { this . locale = language ; } ) ; } public isMultiOutPayment ( transaction ) { return isMultiOutSameTransaction ( transaction ) || isMultiOutTransaction ( transaction ) ; } public ngAfterViewInit ( ) { this . dataSource . paginator = this . paginator ; } public convertTimestamp ( timestamp ) { return ChainTime . fromChainTimestamp ( timestamp ) . getDate ( ) ; } public getTransactionNameFromType ( transaction ) { return this . utilService . translateTransactionSubtype ( transaction , this . account ) ; } public isOwnAccount ( accountId ) { return accountId && accountId === this . account . account ; } public getAmount ( transaction ) { if ( this . isOwnAccount ( transaction . sender ) ) { return Amount . fromPlanck ( transaction . amountNQT ) . multiply ( - 1 ) . getSigna ( ) ; } return this . isMultiOutPayment ( transaction ) ? getRecipientsAmount ( this . account . account , transaction ) . toString ( 10 ) : Amount . fromPlanck ( transaction . amountNQT ) . getSigna ( ) ; } public isAmountNegative ( transaction ) { const isZero = parseFloat ( transaction . amountNQT ) === 0 ; return ! isZero && this . isOwnAccount ( transaction . sender ) ; } public isCommitment ( transaction ) { return transaction . type === TransactionType . Mining && ( transaction . subtype === TransactionMiningSubtype . AddCommitment || transaction . subtype === TransactionMiningSubtype . RemoveCommitment ) ; } getCommitmentAmount ( transaction ) { return Amount . fromPlanck ( transaction . attachment . amountNQT || '0' ) . getSigna ( ) ; } getDate ( tx ) { const time = ChainTime . fromChainTimestamp ( tx . timestamp ) ; return formatDate ( time . getDate ( ) , 'shortDate' , this . locale ) ; } getTime ( tx ) { const time = ChainTime . fromChainTimestamp ( tx . timestamp ) ; return formatDate ( time . getDate ( ) , 'shortTime' , this . locale ) ; } getRowClass ( row ) { const cx = className => row . confirmations === undefined ? `${ className } unconfirmed` : className ; const isNegative = this . isAmountNegative ( row ) ; if ( ( ! row . recipient && ( row . type !== TransactionType . Payment ) ) || ( row . recipient === this . account . account && row . sender === this . account . account ) ) { return cx ( 'self' ) ; } if ( ( row . recipient === this . account . account && row . sender !== this . account . account ) || ( ! row . recipient && ( row . type === TransactionType . Payment ) && ! isNegative ) ) { return cx ( 'incoming' ) ; } return cx ( 'outgoing' ) ; } public chopPrefix ( reedSolomonAddress ) { return reedSolomonAddress . substr ( reedSolomonAddress . indexOf ( '-' ) + 1 ) ; } }