github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/angular-app/src/app/AllTransactions/AllTransactions.component.ts (about)

     1  import { Component, OnInit, Input } from '@angular/core';
     2  import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
     3  import { Observable } from 'rxjs/Observable';
     4  
     5  import { AllTransactionsService } from './AllTransactions.service';
     6  import 'rxjs/add/operator/toPromise';
     7  
     8  @Component({
     9  	selector: 'app-AllTransactions',
    10  	templateUrl: './AllTransactions.component.html',
    11  	styleUrls: ['./AllTransactions.component.css'],
    12    	providers: [AllTransactionsService]
    13  })
    14  export class AllTransactionsComponent {
    15  
    16    private errorMessage;
    17    private allTransactions;
    18  
    19    private systemTransactions = [];
    20    private performedTransactions = [];
    21  
    22    constructor(private serviceTransaction:AllTransactionsService, fb: FormBuilder) {
    23  
    24    };
    25  
    26  
    27    ngOnInit(): void {
    28  
    29      //call to retrieve transactions
    30      this.loadAllTransactions();
    31  
    32    }
    33  
    34    //sort the objects on key
    35    sortByKey(array, key): Object[] {
    36      return array.sort(function(a, b) {
    37          var x = a[key]; var y = b[key];
    38          return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    39      });
    40    }
    41  
    42    //get all transactions
    43    loadAllTransactions(): Promise<any> {
    44  
    45      //initialize arrays to collect performed and system transactions
    46      let tempList = [];
    47      let systemList = [];
    48      let performedList = [];
    49  
    50      //collect all transactions for display
    51      return this.serviceTransaction.getTransactions()
    52      .toPromise()
    53      .then((result) => {
    54        
    55        //sort the transactions by timestamp
    56        result = this.sortByKey(result, 'transactionTimestamp');
    57        this.errorMessage = null;
    58        
    59        //for each transaction, determine whether system transaction
    60        result.forEach(transaction => {
    61          tempList.push(transaction);
    62  
    63          //split the transactionType string
    64          var importClass = transaction["transactionType"];
    65          var importClassArray = importClass.split(".");
    66  
    67          //if `hyperledger` string in the transactionType, then add to systemList, otherwise performedList
    68          if(importClassArray[1] == 'hyperledger'){
    69            systemList.push(transaction);
    70          }
    71          else {
    72            performedList.push(transaction);
    73          }
    74        });
    75  
    76        //update object
    77        this.systemTransactions = systemList;
    78        this.performedTransactions = performedList;
    79        this.allTransactions = tempList;
    80        console.log(this.allTransactions)
    81        console.log(this.performedTransactions)
    82        console.log(this.systemTransactions)
    83      })
    84      .catch((error) => {
    85          if(error == 'Server error'){
    86              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
    87          }
    88          else if(error == '404 - Not Found'){
    89  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
    90          }
    91          else{
    92              this.errorMessage = error;
    93          }
    94      });
    95    }
    96  
    97  
    98  }