2020-12-05 07:30:30 +01:00
|
|
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
2020-11-25 08:57:17 +01:00
|
|
|
import {MatTableDataSource} from '@angular/material/table';
|
|
|
|
import {SelectionModel} from '@angular/cdk/collections';
|
|
|
|
import {MatPaginator} from '@angular/material/paginator';
|
|
|
|
import {MatSort} from '@angular/material/sort';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {UserService} from '@app/_services';
|
2020-11-25 08:57:17 +01:00
|
|
|
import {Router} from '@angular/router';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-accounts',
|
|
|
|
templateUrl: './accounts.component.html',
|
|
|
|
styleUrls: ['./accounts.component.scss']
|
|
|
|
})
|
2020-12-05 07:30:30 +01:00
|
|
|
export class AccountsComponent implements OnInit {
|
2020-11-25 08:57:17 +01:00
|
|
|
dataSource: MatTableDataSource<any>;
|
2020-12-05 07:30:30 +01:00
|
|
|
accounts: any[] = [];
|
|
|
|
displayedColumns = ['name', 'phone', 'created', 'balance', 'type', 'status', 'location', 'failedPinAttempts', 'select'];
|
2020-11-25 08:57:17 +01:00
|
|
|
initialSelection = [];
|
|
|
|
allowMultiSelect = true;
|
|
|
|
selection: SelectionModel<any>;
|
2020-12-05 07:30:30 +01:00
|
|
|
accountsType = 'all';
|
2020-11-25 08:57:17 +01:00
|
|
|
|
|
|
|
@ViewChild(MatPaginator) paginator: MatPaginator;
|
|
|
|
@ViewChild(MatSort) sort: MatSort;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private userService: UserService,
|
|
|
|
private router: Router
|
2020-12-05 07:30:30 +01:00
|
|
|
) {
|
|
|
|
this.userService.getAccounts();
|
|
|
|
}
|
2020-11-25 08:57:17 +01:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-12-05 07:30:30 +01:00
|
|
|
this.userService.accountsSubject.subscribe(accounts => {
|
|
|
|
this.dataSource = new MatTableDataSource<any>(accounts);
|
|
|
|
this.dataSource.paginator = this.paginator;
|
|
|
|
this.dataSource.sort = this.sort;
|
|
|
|
this.accounts = accounts;
|
|
|
|
});
|
2020-11-25 08:57:17 +01:00
|
|
|
this.selection = new SelectionModel<any>(this.allowMultiSelect, this.initialSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
isAllSelected(): boolean {
|
|
|
|
const numSelected = this.selection.selected.length;
|
|
|
|
const numRows = this.dataSource.data.length;
|
|
|
|
return numSelected === numRows;
|
|
|
|
}
|
|
|
|
|
|
|
|
masterToggle(): void {
|
|
|
|
this.isAllSelected() ? this.selection.clear() : this.dataSource.data.forEach(row => this.selection.select(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
doFilter(value: string): void {
|
|
|
|
this.dataSource.filter = value.trim().toLocaleLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
viewAccount(account): void {
|
2020-12-05 07:30:30 +01:00
|
|
|
this.router.navigateByUrl(`/accounts/${account.id}`).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
approveAccount(): void {
|
|
|
|
for (const account of this.selection.selected) {
|
|
|
|
account.status = 'approved';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filterAccounts(): void {
|
|
|
|
if (this.accountsType === 'all') {
|
|
|
|
this.userService.accountsSubject.subscribe(accounts => {
|
|
|
|
this.dataSource.data = accounts;
|
|
|
|
this.accounts = accounts;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.dataSource.data = this.accounts.filter(account => account.type === this.accountsType);
|
|
|
|
}
|
2020-11-25 08:57:17 +01:00
|
|
|
}
|
|
|
|
}
|