mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Wizard: Step 1
This commit is contained in:
41
app/assets/javascripts/wizard/models/step.js.es6
Normal file
41
app/assets/javascripts/wizard/models/step.js.es6
Normal 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;
|
||||
});
|
||||
}
|
||||
});
|
||||
17
app/assets/javascripts/wizard/models/wizard-field.js.es6
Normal file
17
app/assets/javascripts/wizard/models/wizard-field.js.es6
Normal 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);
|
||||
}
|
||||
});
|
||||
23
app/assets/javascripts/wizard/models/wizard.js.es6
Normal file
23
app/assets/javascripts/wizard/models/wizard.js.es6
Normal 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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user