From f6327229166ef3ff390d997070b7166582435144 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 21 Sep 2017 23:13:13 +0200 Subject: [PATCH] Migrate schedule list to material. --- src/scheduler/schedule.module.ts | 16 ++- src/scheduler/scheduleList.component.ts | 171 +++++++++++++++++------- 2 files changed, 138 insertions(+), 49 deletions(-) diff --git a/src/scheduler/schedule.module.ts b/src/scheduler/schedule.module.ts index c3d7656..9e326be 100644 --- a/src/scheduler/schedule.module.ts +++ b/src/scheduler/schedule.module.ts @@ -3,6 +3,14 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ReactiveFormsModule } from '@angular/forms'; +import { + MdButtonModule, + MdDialogModule, + MdIconModule, + MdInputModule, + MdListModule, + MdTableModule, +} from '@angular/material'; import { HttpClientModule } from '@angular/common/http'; import { RouterModule } from '@angular/router'; @@ -16,7 +24,6 @@ import { ScheduleDataSource } from './schedule.dataSource'; import { ScheduleDeleteModalComponent } from './scheduleDeleteModal.component'; import { ScheduleEditModalComponent } from './scheduleEditModal.component'; import { ScheduleFormComponent } from './scheduleForm.component'; -import { ScheduleRowComponent } from './scheduleRow.component'; import { ScheduleListComponent } from './scheduleList.component'; import { ScheduleListState } from './schedule.states'; @@ -28,6 +35,12 @@ import { ScheduleListState } from './schedule.states'; RouterModule.forChild([ ScheduleListState ]), + MdButtonModule, + MdDialogModule, + MdIconModule, + MdInputModule, + MdListModule, + MdTableModule, NgLoggerModule, ToastrModule, NgbModule, @@ -42,7 +55,6 @@ import { ScheduleListState } from './schedule.states'; ScheduleEditModalComponent, ScheduleFormComponent, ScheduleListComponent, - ScheduleRowComponent ], entryComponents: [ ScheduleDeleteModalComponent, diff --git a/src/scheduler/scheduleList.component.ts b/src/scheduler/scheduleList.component.ts index 3a1ba83..5765d63 100644 --- a/src/scheduler/scheduleList.component.ts +++ b/src/scheduler/scheduleList.component.ts @@ -8,6 +8,8 @@ import { Logger } from '@nsalaun/ng-logger'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { ToastrService } from 'ngx-toastr'; +import { ScheduleDataSource } from './schedule.dataSource'; +import { ScheduleDeleteModalComponent } from './scheduleDeleteModal.component'; import { ScheduleEditModalComponent } from './scheduleEditModal.component'; import { ScheduleService } from './schedule.service'; import { Schedule } from './schedule'; @@ -15,41 +17,95 @@ import { Schedule } from './schedule'; @Component({ selector: 'schedule-list', template: ` -
- - - - - - - - - - - - - +
+
+ +
-
- - - - - - -
Date de débutDate de finJourFréq.Libellé de l'opérationMontantCatégorieActions
- -
+ + + + + + + + + +
` }) export class ScheduleListComponent implements OnInit { - accountId: number; - schedules = []; + private accountId: number; + + private displayedColumns: String[] = [ + 'start_date', 'stop_date', 'day', 'frequency', + 'label', 'value', 'category', 'actions' + ]; constructor( private toastrService: ToastrService, @@ -57,6 +113,7 @@ export class ScheduleListComponent implements OnInit { private logger: Logger, private ngbModal: NgbModal, private route: ActivatedRoute, + private schedules: ScheduleDataSource, ) {} ngOnInit() { @@ -66,13 +123,23 @@ export class ScheduleListComponent implements OnInit { this.load(); } + load() { + this.logger.log("Loading schedules for accountId", this.accountId); + if(!this.accountId) { + return; + } + + this.schedules.load(this.accountId); + } + /* * Add a new operation at the beginning of th array. */ add() { - var schedule = new Schedule(); - schedule.account_id = this.accountId; + this.modify(new Schedule()); + }; + modify(schedule: Schedule) { const modal = this.ngbModal.open(ScheduleEditModalComponent, { size: 'lg' }); @@ -83,23 +150,7 @@ export class ScheduleListComponent implements OnInit { this.save(schedule); }, (reason) => function(reason) { }); - }; - - load() { - this.logger.log("Loading schedules for accountId", this.accountId); - if(!this.accountId) { - return; - } - - this.scheduleService.query(this.accountId) - .subscribe((schedules: Schedule[]) => { - this.logger.log("Schedules loaded.", schedules); - this.schedules = schedules; - }, (reason) => { - this.logger.log("Got error", reason); - } - ); - }; + } save(schedule: Schedule) { return this.scheduleService.create(schedule).subscribe((schedule: Schedule) => { @@ -112,4 +163,30 @@ export class ScheduleListComponent implements OnInit { ); }); }; + + confirmDelete(schedule: Schedule) { + const modal = this.ngbModal.open(ScheduleDeleteModalComponent); + + modal.componentInstance.schedule = schedule; + + modal.result.then((schedule: Schedule) => { + this.delete(schedule); + }, (reason) => function(reason) { + }); + } + + delete(schedule: Schedule) { + var id = schedule.id; + + return this.scheduleService.delete(schedule).subscribe(() => { + this.toastrService.success('Schedule #' + id + ' deleted.'); + + this.load(); + }, result => { + this.toastrService.error( + 'An error occurred while trying to delete schedule #' + id + ':
' + + result.message + ); + }); + } };