2017-07-14 05:39:09 +08:00
|
|
|
import {OnDestroy, OnInit, ViewChild} from "@angular/core";
|
|
|
|
import {AuthenticationService} from "../../model/network/authentication.service";
|
|
|
|
import {UserRoles} from "../../../../common/entities/UserDTO";
|
|
|
|
import {Utils} from "../../../../common/Utils";
|
2017-07-15 18:47:11 +08:00
|
|
|
import {ErrorDTO} from "../../../../common/entities/Error";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {NotificationService} from "../../model/notification.service";
|
|
|
|
import {NavigationService} from "../../model/navigation.service";
|
|
|
|
import {ISettingsService} from "./abstract.settings.service";
|
|
|
|
|
|
|
|
|
|
|
|
export abstract class SettingsComponent<T> implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
@ViewChild('settingsForm') form;
|
2017-07-15 18:47:11 +08:00
|
|
|
public settings: T = <T>{};
|
2017-07-14 05:39:09 +08:00
|
|
|
public inProgress = false;
|
2017-07-15 18:47:11 +08:00
|
|
|
private original: T = <T>{};
|
2017-07-14 05:39:09 +08:00
|
|
|
public error: string = null;
|
|
|
|
public changed: boolean = false;
|
2017-07-15 18:47:11 +08:00
|
|
|
private subscription = null;
|
2017-07-14 05:39:09 +08:00
|
|
|
|
|
|
|
constructor(private name,
|
|
|
|
private _authService: AuthenticationService,
|
|
|
|
private _navigation: NavigationService,
|
|
|
|
protected _settingsService: ISettingsService<T>,
|
|
|
|
private notification: NotificationService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (!this._authService.isAuthenticated() ||
|
|
|
|
this._authService.user.value.role < UserRoles.Admin) {
|
|
|
|
this._navigation.toLogin();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.original = Utils.clone(this.settings);
|
|
|
|
this.getSettings();
|
|
|
|
|
|
|
|
this.subscription = this.form.valueChanges.subscribe((data) => {
|
|
|
|
this.changed = !Utils.equalsFilter(this.settings, this.original);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
if (this.subscription != null) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getSettings() {
|
|
|
|
const s = await this._settingsService.getSettings();
|
|
|
|
this.original = Utils.clone(s);
|
|
|
|
this.settings = s;
|
2017-07-15 18:47:11 +08:00
|
|
|
console.log(this.settings);
|
2017-07-14 05:39:09 +08:00
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.getSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
|
|
|
|
public async save() {
|
2017-07-14 05:39:09 +08:00
|
|
|
this.inProgress = true;
|
2017-07-15 18:47:11 +08:00
|
|
|
this.error = "";
|
2017-07-14 05:39:09 +08:00
|
|
|
try {
|
2017-07-15 18:47:11 +08:00
|
|
|
await this._settingsService.updateSettings(this.settings);
|
|
|
|
await this.getSettings();
|
|
|
|
this.notification.success(this.name + ' settings saved', "Success");
|
2017-07-14 05:39:09 +08:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
if (err.message) {
|
2017-07-15 18:47:11 +08:00
|
|
|
this.error = (<ErrorDTO>err).message;
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.inProgress = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|