ghostfolio/apps/client/src/app/components/transactions-table/transactions-table.component.ts

266 lines
7.6 KiB
TypeScript
Raw Normal View History

2021-05-12 22:03:07 +02:00
import { COMMA, ENTER } from '@angular/cdk/keycodes';
2021-04-13 21:53:58 +02:00
import {
ChangeDetectionStrategy,
Component,
ElementRef,
2021-04-13 21:53:58 +02:00
EventEmitter,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
ViewChild
} from '@angular/core';
2021-05-12 22:03:07 +02:00
import { FormControl } from '@angular/forms';
import {
MatAutocomplete,
MatAutocompleteSelectedEvent
} from '@angular/material/autocomplete';
import { MatChipInputEvent } from '@angular/material/chips';
2021-04-13 21:53:58 +02:00
import { MatDialog } from '@angular/material/dialog';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
import { OrderWithAccount } from '@ghostfolio/api/app/order/interfaces/order-with-account.type';
2021-04-19 14:38:55 +02:00
import { DEFAULT_DATE_FORMAT } from '@ghostfolio/helper';
import { BehaviorSubject, Observable, Subject, Subscription } from 'rxjs';
2021-04-13 21:53:58 +02:00
import { takeUntil } from 'rxjs/operators';
import { PositionDetailDialog } from '../position/position-detail-dialog/position-detail-dialog.component';
const SEARCH_STRING_SEPARATOR = ',';
2021-04-13 21:53:58 +02:00
@Component({
selector: 'gf-transactions-table',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './transactions-table.component.html',
styleUrls: ['./transactions-table.component.scss']
})
export class TransactionsTableComponent
implements OnChanges, OnDestroy, OnInit {
@Input() baseCurrency: string;
@Input() deviceType: string;
@Input() locale: string;
@Input() showActions: boolean;
@Input() transactions: OrderWithAccount[];
2021-04-13 21:53:58 +02:00
@Output() transactionDeleted = new EventEmitter<string>();
@Output() transactionToClone = new EventEmitter<OrderWithAccount>();
@Output() transactionToUpdate = new EventEmitter<OrderWithAccount>();
2021-04-13 21:53:58 +02:00
@ViewChild('autocomplete') matAutocomplete: MatAutocomplete;
@ViewChild('searchInput') searchInput: ElementRef<HTMLInputElement>;
2021-04-13 21:53:58 +02:00
@ViewChild(MatSort) sort: MatSort;
public dataSource: MatTableDataSource<OrderWithAccount> = new MatTableDataSource();
2021-04-13 21:53:58 +02:00
public defaultDateFormat = DEFAULT_DATE_FORMAT;
public displayedColumns = [];
public filteredTransactions$: Subject<string[]> = new BehaviorSubject([]);
public filteredTransactions: Observable<
string[]
> = this.filteredTransactions$.asObservable();
2021-04-13 21:53:58 +02:00
public isLoading = true;
public routeQueryParams: Subscription;
public searchControl = new FormControl();
public searchKeywords: string[] = [];
public separatorKeysCodes: number[] = [ENTER, COMMA];
2021-04-13 21:53:58 +02:00
private allFilteredTransactions: string[];
2021-04-13 21:53:58 +02:00
private unsubscribeSubject = new Subject<void>();
public constructor(
private dialog: MatDialog,
private route: ActivatedRoute,
private router: Router
) {
this.routeQueryParams = route.queryParams
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((params) => {
2021-04-21 21:15:49 +02:00
if (params['positionDetailDialog'] && params['symbol']) {
2021-04-13 21:53:58 +02:00
this.openPositionDialog({
symbol: params['symbol'],
title: params['title']
});
}
});
this.searchControl.valueChanges.subscribe((keyword) => {
if (keyword) {
const filterValue = keyword.toLowerCase();
this.filteredTransactions$.next(
this.allFilteredTransactions.filter(
(filter) => filter.toLowerCase().indexOf(filterValue) === 0
)
);
} else {
this.filteredTransactions$.next(this.allFilteredTransactions);
}
});
}
public addKeyword({ input, value }: MatChipInputEvent): void {
if (value?.trim()) {
this.searchKeywords.push(value.trim());
this.updateFilter();
}
// Reset the input value
if (input) {
input.value = '';
}
this.searchControl.setValue(null);
}
public removeKeyword(keyword: string): void {
const index = this.searchKeywords.indexOf(keyword);
if (index >= 0) {
this.searchKeywords.splice(index, 1);
this.updateFilter();
}
}
public keywordSelected(event: MatAutocompleteSelectedEvent): void {
this.searchKeywords.push(event.option.viewValue);
this.updateFilter();
this.searchInput.nativeElement.value = '';
this.searchControl.setValue(null);
2021-04-13 21:53:58 +02:00
}
public ngOnInit() {}
public ngOnChanges() {
this.displayedColumns = [
'date',
'type',
'symbol',
'currency',
'quantity',
'unitPrice',
'fee',
'account'
2021-04-13 21:53:58 +02:00
];
if (this.showActions) {
this.displayedColumns.push('actions');
}
this.isLoading = true;
2021-04-13 21:53:58 +02:00
if (this.transactions) {
this.dataSource = new MatTableDataSource(this.transactions);
this.dataSource.filterPredicate = (data, filter) => {
const dataString = TransactionsTableComponent.getFilterableValues(data)
.join(' ')
.toLowerCase();
let contains = true;
for (const singleFilter of filter.split(SEARCH_STRING_SEPARATOR)) {
contains =
contains && dataString.includes(singleFilter.trim().toLowerCase());
}
return contains;
};
2021-04-13 21:53:58 +02:00
this.dataSource.sort = this.sort;
this.updateFilter();
2021-04-13 21:53:58 +02:00
this.isLoading = false;
}
}
public onDeleteTransaction(aId: string) {
const confirmation = confirm(
'Do you really want to delete this transaction?'
);
if (confirmation) {
this.transactionDeleted.emit(aId);
}
}
public onOpenPositionDialog({
symbol,
title
}: {
symbol: string;
title: string;
}): void {
this.router.navigate([], {
queryParams: { positionDetailDialog: true, symbol, title }
});
}
public onUpdateTransaction(aTransaction: OrderWithAccount) {
2021-04-13 21:53:58 +02:00
this.transactionToUpdate.emit(aTransaction);
}
public onCloneTransaction(aTransaction: OrderWithAccount) {
this.transactionToClone.emit(aTransaction);
}
2021-04-13 21:53:58 +02:00
public openPositionDialog({
symbol,
title
}: {
symbol: string;
title: string;
}): void {
const dialogRef = this.dialog.open(PositionDetailDialog, {
autoFocus: false,
data: {
symbol,
title,
baseCurrency: this.baseCurrency,
deviceType: this.deviceType,
locale: this.locale
},
height: this.deviceType === 'mobile' ? '97.5vh' : '80vh',
width: this.deviceType === 'mobile' ? '100vw' : '50rem'
});
dialogRef.afterClosed().subscribe(() => {
this.router.navigate(['.'], { relativeTo: this.route });
});
}
public ngOnDestroy() {
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
}
private updateFilter() {
this.dataSource.filter = this.searchKeywords.join(SEARCH_STRING_SEPARATOR);
const lowercaseSearchKeywords = this.searchKeywords.map((keyword) =>
keyword.trim().toLowerCase()
);
this.allFilteredTransactions = TransactionsTableComponent.getSearchableFieldValues(
this.transactions
).filter((item) => {
return !lowercaseSearchKeywords.includes(item.trim().toLowerCase());
});
this.filteredTransactions$.next(this.allFilteredTransactions);
}
private static getSearchableFieldValues(
transactions: OrderWithAccount[]
): string[] {
const fieldValues = new Set<string>();
for (const transaction of transactions) {
this.getFilterableValues(transaction, fieldValues);
}
return [...fieldValues].filter((item) => item != undefined).sort();
}
private static getFilterableValues(
transaction,
fieldValues: Set<string> = new Set<string>()
): string[] {
fieldValues.add(transaction.currency);
fieldValues.add(transaction.symbol);
fieldValues.add(transaction.type);
fieldValues.add(transaction.Account?.name);
fieldValues.add(transaction.Account?.Platform?.name);
return [...fieldValues].filter((item) => item != undefined);
}
2021-04-13 21:53:58 +02:00
}