UNPKG

stitch-ui

Version:

50 lines (42 loc) 959 B
import { Record, Set } from "immutable"; import JSONState from "../../models/JSONState"; export default class Rule extends Record({ _id: "", actions: new Set(), when: new JSONState({}), dirty: false }) { static fromRawRule(raw) { return new Rule({ _id: raw._id, actions: new Set(raw.actions), when: JSONState.fromRaw(raw.when, {}), dirty: false }); } toRawRule() { const out = { _id: this._id, actions: this.actions.toArray() }; if (this.when.data) { out.when = this.when.data; } return out; } setAction(a, enabled) { return this.set( "actions", enabled ? this.actions.add(a) : this.actions.remove(a) ).set("dirty", true); } updateWhenInput(v) { return this.setIn(["when", "input"], v).set("dirty", true); } hasError() { return this.when.error != null; } parse() { return this.set("when", this.when.parseInput()); } }