2016-08-25 13:14:56 -04:00
|
|
|
import computed from 'ember-addons/ember-computed-decorators';
|
|
|
|
import ValidState from 'wizard/mixins/valid-state';
|
|
|
|
import { ajax } from 'wizard/lib/ajax';
|
|
|
|
|
|
|
|
export default Ember.Object.extend(ValidState, {
|
|
|
|
id: null,
|
|
|
|
|
|
|
|
@computed('index')
|
|
|
|
displayIndex: index => index + 1,
|
|
|
|
|
2016-09-02 11:42:14 -04:00
|
|
|
@computed('fields.[]')
|
|
|
|
fieldsById(fields) {
|
|
|
|
const lookup = {};
|
|
|
|
fields.forEach(field => lookup[field.get('id')] = field);
|
|
|
|
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: [] };
|
|
|
|
|
2016-08-25 13:14:56 -04:00
|
|
|
this.get('fields').forEach(field => {
|
2016-09-20 12:28:22 -04:00
|
|
|
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);
|
2016-09-20 12:28:22 -04:00
|
|
|
|
|
|
|
return result;
|
2016-08-25 13:14:56 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
fieldError(id, description) {
|
2016-10-26 15:44:36 -04:00
|
|
|
const field = this.get('fields').findBy('id', id);
|
2016-08-25 13:14:56 -04:00
|
|
|
if (field) {
|
|
|
|
field.setValid(false, description);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
|
|
|
const fields = {};
|
|
|
|
this.get('fields').forEach(f => fields[f.id] = f.value);
|
|
|
|
|
|
|
|
return ajax({
|
|
|
|
url: `/wizard/steps/${this.get('id')}`,
|
|
|
|
type: 'PUT',
|
|
|
|
data: { fields }
|
|
|
|
}).catch(response => {
|
|
|
|
response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
|
|
|
|
throw response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|