File

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

Index

Properties

Properties

name
name: string
Type : string
position
position: number
Type : number
symbol
symbol: string
Type : string
weight
weight: number
Type : number
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 PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

export interface Stock {
  count: number;
  currentPrice: number;
  symbol: string;
  time: string;
}

//symbol count lastTransaction boughtPrice currentPrice(+?)

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
];

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;

  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() {
    this.dataService.getStockData().subscribe((response: any) => {
      var data = JSON.parse(response);
      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,
          currentPrice: data.data[i].current_price,
          symbol: data.data[i].symbol,
          time: data.data[i].last_transaction,
        });
      }
      this.dataSourceStocks = STOCK_DATA;
      //TODO move to helper service

      this.profit += this.depotCurrentValue;
    });

    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.depotCost;
    });
  }

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

  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();
    });
  }

  displayedColumns: string[] = [
    'comment',
    'weight',
    'position',
    'name',
    'symbol',
  ];
  displayedColumnsStocks: string[] = [
    'position',
    'name',
    'weight',
    'current-price',
  ];
  dataSource = ELEMENT_DATA;
}

results matching ""

    No results matching ""