discourse/app/assets/javascripts/wizard/models/step.js.es6

58 lines
1.3 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import computed from "ember-addons/ember-computed-decorators";
import ValidState from "wizard/mixins/valid-state";
import { ajax } from "wizard/lib/ajax";
2016-08-26 01:14:56 +08:00
export default Ember.Object.extend(ValidState, {
id: null,
2018-07-30 23:56:48 +08:00
@computed("index")
displayIndex: index => index + 1,
2016-08-26 01:14:56 +08:00
2018-06-15 23:03:24 +08:00
@computed("fields.[]")
2016-09-02 23:42:14 +08:00
fieldsById(fields) {
const lookup = {};
2018-06-15 23:03:24 +08:00
fields.forEach(field => (lookup[field.get("id")] = field));
2016-09-02 23:42:14 +08:00
return lookup;
},
validate() {
2016-08-26 01:14:56 +08:00
let allValid = true;
const result = { warnings: [] };
2018-06-15 23:03:24 +08:00
this.get("fields").forEach(field => {
allValid = allValid && field.check();
2018-06-15 23:03:24 +08:00
const warning = field.get("warning");
if (warning) {
result.warnings.push(warning);
}
2016-08-26 01:14:56 +08:00
});
this.setValid(allValid);
return result;
2016-08-26 01:14:56 +08:00
},
fieldError(id, description) {
2018-06-15 23:03:24 +08:00
const field = this.get("fields").findBy("id", id);
2016-08-26 01:14:56 +08:00
if (field) {
field.setValid(false, description);
}
},
save() {
const fields = {};
2018-06-15 23:03:24 +08:00
this.get("fields").forEach(f => (fields[f.id] = f.value));
2016-08-26 01:14:56 +08:00
return ajax({
2018-06-15 23:03:24 +08:00
url: `/wizard/steps/${this.get("id")}`,
type: "PUT",
2016-08-26 01:14:56 +08:00
data: { fields }
}).catch(response => {
2018-06-15 23:03:24 +08:00
response.responseJSON.errors.forEach(err =>
this.fieldError(err.field, err.description)
);
throw new Error(response);
2016-08-26 01:14:56 +08:00
});
}
});