cic-staff-client/src/app/pages/accounts/accounts.component.ts

78 lines
2.4 KiB
TypeScript

import {Component, OnInit, ViewChild} from '@angular/core';
import {MatTableDataSource} from '@angular/material/table';
import {SelectionModel} from '@angular/cdk/collections';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {UserService} from '@app/_services';
import {Router} from '@angular/router';
@Component({
selector: 'app-accounts',
templateUrl: './accounts.component.html',
styleUrls: ['./accounts.component.scss']
})
export class AccountsComponent implements OnInit {
dataSource: MatTableDataSource<any>;
accounts: any[] = [];
displayedColumns = ['name', 'phone', 'created', 'balance', 'type', 'status', 'location', 'failedPinAttempts', 'select'];
initialSelection = [];
allowMultiSelect = true;
selection: SelectionModel<any>;
accountsType = 'all';
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(
private userService: UserService,
private router: Router
) {
this.userService.getAccounts();
}
ngOnInit(): void {
this.userService.accountsSubject.subscribe(accounts => {
this.dataSource = new MatTableDataSource<any>(accounts);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.accounts = accounts;
});
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 {
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);
}
}
}