This commit is contained in:
Alexis Lahouze 2017-08-01 23:22:44 +02:00
parent a1d71b92d5
commit 3e6cadffbd

View File

@ -1,3 +1,4 @@
// vim: set tw=80 ts=2 sw=2 sts=2 :
import { Component, Inject, OnInit } from '@angular/core'; import { Component, Inject, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
@ -12,8 +13,8 @@ import { ScheduleService } from './schedule.service';
import { Schedule } from './schedule'; import { Schedule } from './schedule';
@Component({ @Component({
selector: 'schedule-list', selector: 'schedule-list',
template: ` template: `
<div class="row"> <div class="row">
<table class="table table-sm table-striped table-condensed table-hover"> <table class="table table-sm table-striped table-condensed table-hover">
<thead> <thead>
@ -47,70 +48,70 @@ import { Schedule } from './schedule';
` `
}) })
export class ScheduleListComponent implements OnInit { export class ScheduleListComponent implements OnInit {
accountId: number; accountId: number;
schedules = []; schedules = [];
constructor( constructor(
private toastrService: ToastrService, private toastrService: ToastrService,
private scheduleService: ScheduleService, private scheduleService: ScheduleService,
private logger: Logger, private logger: Logger,
private ngbModal: NgbModal, private ngbModal: NgbModal,
private route: ActivatedRoute, private route: ActivatedRoute,
) {} ) {}
$onInit() { $onInit() {
this.ngOnInit(); this.ngOnInit();
}
ngOnInit() {
this.logger.log("ngOnInit");
this.accountId = +this.route.snapshot.paramMap.get('accountId')
// Load operations on controller initialization.
this.load();
}
/*
* Add a new operation at the beginning of th array.
*/
add() {
var schedule = new Schedule();
schedule.account_id = this.accountId;
const modal = this.ngbModal.open(ScheduleEditModalComponent);
modal.componentInstance.schedule = schedule;
modal.result.then((schedule: Schedule) => {
this.save(schedule);
}, (reason) => function(reason) {
});
};
load() {
this.logger.log("Loading schedules for accountId", this.accountId);
if(!this.accountId) {
return;
} }
ngOnInit() { this.scheduleService.query(this.accountId)
this.logger.log("ngOnInit"); .subscribe((schedules: Schedule[]) => {
this.accountId = +this.route.snapshot.paramMap.get('accountId') this.logger.log("Schedules loaded.", schedules);
// Load operations on controller initialization. this.schedules = schedules;
this.load(); }, (reason) => {
} this.logger.log("Got error", reason);
}
);
};
/* save(schedule: Schedule) {
* Add a new operation at the beginning of th array. return this.scheduleService.create(schedule).subscribe((schedule: Schedule) => {
*/ this.toastrService.success('Schedule #' + schedule.id + ' saved.');
add() {
var schedule = new Schedule();
schedule.account_id = this.accountId;
const modal = this.ngbModal.open(ScheduleEditModalComponent); this.load();
}, (result) => {
modal.componentInstance.schedule = schedule; this.toastrService.error(
'Error while saving schedule: ' + result.message
modal.result.then((schedule: Schedule) => { );
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) => {
this.toastrService.success('Schedule #' + schedule.id + ' saved.');
this.load();
}, (result) => {
this.toastrService.error(
'Error while saving schedule: ' + result.message
);
});
};
}; };