Wizard: Step 1

This commit is contained in:
Robin Ward
2016-08-25 13:14:56 -04:00
parent 0471ad393c
commit 3a4615c205
50 changed files with 1103 additions and 80 deletions

View File

@@ -0,0 +1,41 @@
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,
checkFields() {
let allValid = true;
this.get('fields').forEach(field => {
field.check();
allValid = allValid && field.get('valid');
});
this.setValid(allValid);
},
fieldError(id, description) {
const field = this.get('fields').findProperty('id', id);
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;
});
}
});

View File

@@ -0,0 +1,17 @@
import ValidState from 'wizard/mixins/valid-state';
export default Ember.Object.extend(ValidState, {
id: null,
type: null,
value: null,
required: null,
check() {
if (!this.get('required')) {
return this.setValid(true);
}
const val = this.get('value');
this.setValid(val && val.length > 0);
}
});

View File

@@ -0,0 +1,23 @@
import Step from 'wizard/models/step';
import WizardField from 'wizard/models/wizard-field';
import { ajax } from 'wizard/lib/ajax';
import computed from 'ember-addons/ember-computed-decorators';
const Wizard = Ember.Object.extend({
@computed('steps.length')
totalSteps: length => length
});
export function findWizard() {
return ajax({ url: '/wizard.json' }).then(response => {
const wizard = response.wizard;
wizard.steps = wizard.steps.map(step => {
const stepObj = Step.create(step);
stepObj.fields = stepObj.fields.map(f => WizardField.create(f));
return stepObj;
});
return Wizard.create(wizard);
});
}