unageanu/jiji2

View on GitHub
sites/src/js/viewmodel/settings/pair-setting-model.js

Summary

Maintainability
A
40 mins
Test Coverage
import Observable        from "../../utils/observable"
import Deferred          from "../../utils/deferred"
import Error             from "../../model/error"
import ErrorMessages     from "../../errorhandling/error-messages"
import Validators        from "../../utils/validation/validators"
import PairSelectorModel from "../widgets/pair-selector-model"
import DateFormatter     from "../utils/date-formatter"

export default class PairSettingModel extends PairSelectorModel {

  constructor(pairSettingService, pairs, timeSource) {
    super(Validators.pairNames);
    this.pairs = pairs;
    this.pairSettingService = pairSettingService;
    this.timeSource = timeSource;

    this.message = null;
    this.isSaving = false;
  }

  initialize() {
    this.message = null;
    Deferred.when([
      this.pairSettingService.getPairs(),
      this.pairSettingService.getAllPairs()
    ]).then( (results) => {
      super.initialize(results[1], results[0].map((p) => p.name));
    });
  }

  save() {
    this.message = null;
    if (!this.validate()) return;
    this.isSaving = true;
    const pairs = this.pairNames.map((p) => { return { name:p } });
    this.pairSettingService.setPairs(pairs).then(
      (result) => {
        this.isSaving = false;
        this.message = "設定を変更しました。 ("
          + DateFormatter.format(this.timeSource.now) + ")" ;
        this.pairs.reload();
      },
      (error) => {
        this.isSaving = false;
        this.pairNamesError = ErrorMessages.getMessageFor(error);
        error.preventDefault = true;
      });
  }

  get message() {
    return this.getProperty("message");
  }
  set message(message) {
    this.setProperty("message", message);
  }
  get isSaving() {
    return this.getProperty("isSaving");
  }
  set isSaving(isSaving) {
    this.setProperty("isSaving", isSaving);
  }
}