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