### File

`src/app/Views/dashboard/dashboard.component.ts`

### Metadata

selector | `app-dashboard`
styleUrls | `dashboard.component.scss`
templateUrl | `dashboard.component.html`

### Constructor

`constructor(dataService: any, helper: any, dialog: MatDialog)`

### Methods

getTransactions
`getTransactions()`
Returns: `void`

openDialog
`openDialog()`
Returns: `void`

### Properties

comment
`comment: `string``

count
`count: `number``
Default value: `0`

dataSourceStocks
`dataSourceStocks: `Stock[]``

dataSourceTransactions
`dataSourceTransactions: `TransactionData[]``

depotCost
`depotCost: `number``
Default value: `0`

depotCurrentValue
`depotCurrentValue: `number``
Default value: `0`

dialog
`dialog: `MatDialog``

displayedColumns
`displayedColumns: `string[]``

displayedColumnsStocks
`displayedColumnsStocks: `string[]``

isin
`isin: `string``

price
`price: `number``
Default value: `0`

profit
`profit: `number``
Default value: `0`

time
`time: `Date``

    import { Component, OnInit } from '@angular/core';
    import { DataService } from 'src/app/Services/data.service';
    import { MatDialog } from '@angular/material/dialog';
    import { UserDialogComponent } from './user-dialog/user-dialog.component';
    import { HelperService } from 'src/app/Helpers/helper.service';

    export interface Stock {
      count: number;
      comment: string;
      isin: string;
      last_transaction: string;
      current_price: number;
    }

    var TRANSACTION_DATA: TransactionData[] = [];
    var STOCK_DATA: Stock[] = [];

    export interface TransactionData {
      comment: string;
      isin: string;
      time: string;
      count: number;
      price: number;
    }

    @Component({
      selector: 'app-dashboard',
      templateUrl: './dashboard.component.html',
      styleUrls: ['./dashboard.component.scss'],
    })
    export class DashboardComponent implements OnInit {
      constructor(
        private dataService: DataService,
        private helper: HelperService,
        public dialog: MatDialog
      ) {}

      dataSourceTransactions: TransactionData[] = [];
      dataSourceStocks: Stock[] = [];
      depotCurrentValue: number = 0;
      depotCost: number = 0;
      profit: number = 0;

      comment: string = '';
      isin: string = '';
      time: Date = new Date();
      count: number = 0.0;
      price: number = 0.0;

      getTransactions() {
        var TRANSACTION_DATA: TransactionData[] = [];
        this.dataService.getTransactionData().subscribe((response: any) => {
          var data = JSON.parse(response);
          this.depotCost = 0;
          for (let i = 0; i < data.data.length; i++) {
            this.depotCost += data.data[i].price;
            TRANSACTION_DATA.push({
              comment: data.data[i].comment,
              isin: data.data[i].isin,
              time: data.data[i].time,
              count: data.data[i].count,
              price: data.data[i].price,
            });
          }
          this.dataSourceTransactions = TRANSACTION_DATA;
          //TODO move to helper service

          this.profit = this.depotCurrentValue - this.depotCost;
        });
      }

      ngOnInit() {
        /**
         * Function gets the stock data and pushes it in the temporary object array
         */
        this.dataService.getStockData().subscribe((response: any) => {
          var data = JSON.parse(response);
          console.log(data);
          this.depotCurrentValue = 0;
          for (let i = 0; i < data.data.length; i++) {
            this.depotCurrentValue = data.data[i].current_price;
            STOCK_DATA.push({
              count: data.data[i].count,
              comment: data.data[i].comment,
              isin: data.data[i].isin,
              last_transaction: data.data[i].last_transaction,
              current_price: data.data[i].current_price,
            });
          }
          // assign data source
          this.dataSourceStocks = STOCK_DATA;
          this.profit += this.depotCurrentValue;
        });

        /**
         * Function gets the transaction data and pushes it in the temporary object array
         */
        this.dataService.getTransactionData().subscribe((response: any) => {
          var data = JSON.parse(response);
          this.depotCost = 0;
          for (let i = 0; i < data.data.length; i++) {
            this.depotCost += data.data[i].price;
            TRANSACTION_DATA.push({
              comment: data.data[i].comment,
              isin: data.data[i].isin,
              time: data.data[i].time,
              count: data.data[i].count,
              price: data.data[i].price,
            });
          }
          // assign data source
          this.dataSourceTransactions = TRANSACTION_DATA;
          this.profit += this.depotCost;
        });
      }

      // function to open the user dialog to create a new transaction
      openDialog(): void {
        const dialogRef = this.dialog.open(UserDialogComponent, {
          width: '50vw',
          data: {
            comment: this.comment,
            isin: this.isin,
            time: this.time,
            count: this.count,
            price: this.price,
          },
        });

        dialogRef.afterClosed().subscribe((result) => {
          this.helper.delay(1000);
          this.getTransactions();
        });
      }

      // assign columns for transactions to display in html, you can change order and add/remove columns
      displayedColumns: string[] = [
        'comment',
        'weight',
        'position',
        'name',
        'symbol',
      ];

      // assign columns to display in html, you can change order and add/remove columns
      displayedColumnsStocks: string[] = [
        'count',
        'comment',
        'isin',
        'current_price',
        'last_transaction',
      ];
    }

# results matching ""

# No results matching ""