Skip to content
Snippets Groups Projects
settings.component.ts 1.93 KiB
Newer Older
import {Component, OnDestroy, OnInit} from '@angular/core';
import {SettingsService} from "../services/settings.service";
import {Observable, Subscription} from "rxjs";
import {QueueSetting, Setting} from "../model/setting";
import {AlertService} from "../services/alert.service";

@Component({
  selector: 'app-settings',
  templateUrl: './settings.component.html',
  styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit, OnDestroy {

  settings$: Subscription;
  settings: Setting;
  queueSettings: Observable<Array<QueueSetting>>;

  constructor(
    private settingService: SettingsService,
    private alertService: AlertService
  ) {
  }

  ngOnInit() {
    this.queueSettings = this.settingService.getQueueSettings();
    this.getSettings();
  }

  getSettings(): void {
    this.alertService.info("Retrieving Settings");
    this.settings$ = this.settingService.getSettings().subscribe((s: Setting) => {
      if (s) {
        this.settings = s;
        this.alertService.success('Settings Retrieved');
      } else {
        this.alertService.error('Setting could not be retrieved');
      }
    }, error => {
      this.alertService.error('Settings could not be retrieved. ' + error);
    });
  }

  updateSettings(key: string, value: boolean): void {
    if (this.settings.hasOwnProperty(key) && this.settings[key] !== value) {
      this.alertService.info('Updating settings');
      this.settings[key] = value;
      this.settingService.setSettings(this.settings).subscribe((s: Setting) => {
          if (s) {
            this.settings = s;
            this.alertService.success('Settings Updated');
          } else {
            this.alertService.error('Setting could not be updated');
          }
        },
        error => {
          this.alertService.error('Settings could not be updated');
        })
    }
  }

  ngOnDestroy(): void {
    if (this.settings$) {
      this.settings$.unsubscribe();
    }
  }

}