From 6a93eac76728dae72aea8391e23a4b5f20c345f6 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Wed, 20 Sep 2017 21:52:34 +0200 Subject: [PATCH] Migrate to material. --- src/accounts/account.dataSource.ts | 2 + src/accounts/account.module.ts | 4 +- src/accounts/accountDeleteModal.component.ts | 51 ++---- src/accounts/accountList.component.ts | 5 +- src/accounts/accountRow.component.ts | 176 ------------------- 5 files changed, 20 insertions(+), 218 deletions(-) delete mode 100644 src/accounts/accountRow.component.ts diff --git a/src/accounts/account.dataSource.ts b/src/accounts/account.dataSource.ts index 227d79b..bb5cabf 100644 --- a/src/accounts/account.dataSource.ts +++ b/src/accounts/account.dataSource.ts @@ -1,3 +1,5 @@ +// vim: set tw=80 ts=2 sw=2 sts=2: + import { Injectable } from '@angular/core'; import { DataSource } from '@angular/cdk/collections'; import { Observable } from 'rxjs/Rx'; diff --git a/src/accounts/account.module.ts b/src/accounts/account.module.ts index d911003..a0008f4 100644 --- a/src/accounts/account.module.ts +++ b/src/accounts/account.module.ts @@ -25,7 +25,6 @@ import { AccountDataSource } from './account.dataSource'; import { AccountDeleteModalComponent } from './accountDeleteModal.component'; import { AccountEditModalComponent } from './accountEditModal.component'; import { AccountFormComponent } from './accountForm.component'; -import { AccountRowComponent } from './accountRow.component'; import { DailyBalanceService } from './dailyBalance.service'; import { AccountListState } from './account.states' @@ -45,7 +44,7 @@ import { AccountListState } from './account.states' MdTableModule, NgLoggerModule, ToastrModule, - NgbModule + NgbModule, ], providers: [ AccountService, @@ -58,7 +57,6 @@ import { AccountListState } from './account.states' AccountDeleteModalComponent, AccountEditModalComponent, AccountFormComponent, - AccountRowComponent ], entryComponents: [ AccountListComponent, diff --git a/src/accounts/accountDeleteModal.component.ts b/src/accounts/accountDeleteModal.component.ts index 0f375fa..f8a4bfc 100644 --- a/src/accounts/accountDeleteModal.component.ts +++ b/src/accounts/accountDeleteModal.component.ts @@ -1,53 +1,30 @@ // vim: set tw=80 ts=2 sw=2 sts=2: -import { Component, Input } from '@angular/core'; - -import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; - -import { Account } from './account'; +import { Component, Inject } from '@angular/core'; +import { MD_DIALOG_DATA } from '@angular/material'; @Component({ selector: 'account-delete-modal', template: ` - +

Delete account #{{ data.account.id }}

- + + Do you really want to delete account #{{ data.account.id }} with name:
+ {{ data.account.name }} +
- + ` }) export class AccountDeleteModalComponent { - @Input() account: Account - - constructor(public activeModal: NgbActiveModal) {} - - title(): string { - if(this.account.id) { - return "Account #" + this.account.id; - } else { - return "New account"; - } - } - - submit(): void { - this.activeModal.close(this.account); - } - - cancel(): void { - this.activeModal.dismiss("closed"); - } + constructor( + @Inject(MD_DIALOG_DATA) private data: any + ) {} } diff --git a/src/accounts/accountList.component.ts b/src/accounts/accountList.component.ts index c9f03de..3d9a08d 100644 --- a/src/accounts/accountList.component.ts +++ b/src/accounts/accountList.component.ts @@ -164,9 +164,10 @@ export class AccountListComponent { dialogRef.afterClosed().subscribe((account: Account) => { if(account) { - this.logger.debug("Deleting account", account); - //this.delete(account); + this.delete(account); } + }, reason => { + this.logger.error("Delete dialog failed", reason); }); }; diff --git a/src/accounts/accountRow.component.ts b/src/accounts/accountRow.component.ts deleted file mode 100644 index ebc2cc0..0000000 --- a/src/accounts/accountRow.component.ts +++ /dev/null @@ -1,176 +0,0 @@ -// vim: set tw=80 ts=2 sw=2 sts=2 : -import { CurrencyPipe } from '@angular/common'; -import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core'; - -import { Logger } from '@nsalaun/ng-logger'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; -import { ToastrService } from 'ngx-toastr'; - -import { Account } from './account'; -import { AccountBalances } from './accountBalances'; -import { AccountBalancesService } from './accountBalances.service'; -import { AccountService } from './account.service'; -import { AccountDeleteModalComponent } from './accountDeleteModal.component'; -import { AccountEditModalComponent } from './accountEditModal.component'; - -@Component({ - selector: 'tr[account-row]', - host: { - "[id]": "account.id", - "[class.warning]": "warning", - "[class.danger]": "danger" - }, - template: ` - - {{ account.name }} - - - - - {{ accountBalances?.current | currency:"EUR":true }} - - - - - - {{ accountBalances?.pointed | currency:"EUR":true }} - - - -{{ account.authorized_overdraft | currency:"EUR":true }} - - -
- - - - - - - - - - -
- - ` -}) -export class AccountRowComponent implements OnInit { - @Input('account-row') account: Account; - @Output() needsReload: EventEmitter = new EventEmitter(); - - private accountBalances: AccountBalances; - - constructor( - private accountService: AccountService, - private accountBalancesService: AccountBalancesService, - private toastrService: ToastrService, - private logger: Logger, - private ngbModal: NgbModal - ) { - this.logger.log("AccountRowComponent constructor"); - } - - ngOnInit() { - this.logger.log(this.account); - this.accountBalancesService - .get(this.account.id) - .subscribe((accountBalances: AccountBalances) => { - this.accountBalances = accountBalances; - }) - } - - get warning() { - return this.account && this.accountBalances - && this.account.authorized_overdraft < this.accountBalances.current - && this.accountBalances.current < 0; - }; - - get error() { - return this.account && this.accountBalances - && this.accountBalances.current < this.account.authorized_overdraft; - }; - - /* - * Return the class for a value compared to account authorized overdraft. - */ - valueClass(value: number) { - if (!value) { - return; - } - - if (value < this.account.authorized_overdraft) { - return 'text-danger'; - } else if (value < 0) { - return 'text-warning'; - } - }; - - confirmDelete() { - const modal = this.ngbModal.open(AccountDeleteModalComponent); - - modal.componentInstance.account = this.account; - - modal.result.then((account: Account) => { - this.delete(account); - }, (reason) => function(reason) { - }); - }; - - /* - * Delete an account. - */ - delete(account: Account) { - var id = account.id; - - this.accountService.delete(account).subscribe(account => { - this.toastrService.success('account #' + id + ' deleted.'); - - this.needsReload.emit(); - }, function(result) { - this.toastrService.error( - 'An error occurred while trying to delete account #' + - id + ':
' + result - ); - }); - }; - - /* - * Open the popup to modify the account, save it on confirm. - */ - modify() { - const modal = this.ngbModal.open(AccountEditModalComponent, { - size: 'lg' - }); - - modal.componentInstance.account = this.account; - - modal.result.then((account: Account) => { - this.logger.log("Modal closed => save account", account); - this.save(account); - }, (reason) => function(reason) { - }); - }; - - save(account: Account) { - this.accountService.update(account).subscribe((account: Account) => { - this.toastrService.success('Account #' + account.id + ' saved.'); - - this.needsReload.emit(); - }, result => { - this.logger.error('Error while saving account', account, result); - - this.toastrService.error( - 'Error while saving account: ' + result.message - ); - }); - }; -}