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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.3 KiB
JavaScript
Raw Normal View History

import EmberObject from "@ember/object";
2016-08-25 13:14:56 -04:00
import ValidState from "wizard/mixins/valid-state";
import { ajax } from "discourse/lib/ajax";
import discourseComputed from "discourse-common/utils/decorators";
2016-08-25 13:14:56 -04:00
export default EmberObject.extend(ValidState, {
2016-08-25 13:14:56 -04:00
id: null,
@discourseComputed("index")
displayIndex(index) {
return index + 1;
},
2016-08-25 13:14:56 -04:00
@discourseComputed("fields.[]")
2016-09-02 11:42:14 -04:00
fieldsById(fields) {
const lookup = {};
fields.forEach((field) => (lookup[field.get("id")] = field));
return lookup;
},
validate() {
2016-08-25 13:14:56 -04:00
let allValid = true;
const result = { warnings: [] };
this.fields.forEach((field) => {
allValid = allValid && field.check();
const warning = field.get("warning");
if (warning) {
result.warnings.push(warning);
}
2016-08-25 13:14:56 -04:00
});
this.setValid(allValid);
return result;
2016-08-25 13:14:56 -04:00
},
fieldError(id, description) {
const field = this.fields.findBy("id", id);
2016-08-25 13:14:56 -04:00
if (field) {
field.setValid(false, description);
}
},
save() {
const fields = {};
this.fields.forEach((f) => (fields[f.id] = f.value));
2016-08-25 13:14:56 -04:00
return ajax({
url: `/wizard/steps/${this.id}`,
2016-08-25 13:14:56 -04:00
type: "PUT",
data: { fields },
}).catch((error) => {
error.jqXHR.responseJSON.errors.forEach((err) =>
2016-08-25 13:14:56 -04:00
this.fieldError(err.field, err.description)
);
});
},
});