diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index ac8b8526..84c5a11b 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -1,11 +1,4 @@ -import { default as computed } from 'ember-addons/ember-computed-decorators'; - export default Ember.Component.extend({ targets: ['topic', 'profile', 'email', 'badge', 'save'], - isTopic: Ember.computed.equal('targets', 'topic'), - - init() { - this._super(...arguments); - console.log(this) - }, + isTopic: Ember.computed.equal('targets', 'topic') }); diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index ba09166f..032c96fe 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -4,18 +4,24 @@ export default Ember.Component.extend({ classNames: 'wizard-custom-field', fieldTypes: ['dropdown', 'image', 'radio', 'text', 'textarea'], isDropdown: Ember.computed.equal('field.type', 'dropdown'), - choices: Ember.A(), + + init() { + this._super(...arguments); + + if (!this.get('field.choices')) { + this.set('field.choices', Ember.A()); + } + }, @observes('field.label') setFieldId() { const label = this.get('field.label'); - console.log('setting id') this.set('field.id', Ember.String.underscore(label)); }, actions: { addChoice() { - + this.get('field.choices').pushObject(Ember.Object.create()); } } }); diff --git a/assets/javascripts/discourse/components/wizard-custom-step.js.es6 b/assets/javascripts/discourse/components/wizard-custom-step.js.es6 index 4aede4eb..e8e4774c 100644 --- a/assets/javascripts/discourse/components/wizard-custom-step.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-step.js.es6 @@ -5,13 +5,11 @@ export default Ember.Component.extend({ @computed('step.fields.@each.id') allowAddAction(stepFields) { - console.log(stepFields) return stepFields.get('firstObject.id'); }, actions: { addField() { - console.log('adding field') this.get('step.fields').pushObject(Ember.Object.create()); }, @@ -19,8 +17,8 @@ export default Ember.Component.extend({ this.get('step.actions').pushObject(Ember.Object.create()); }, - removeStep() { - this.sendAction('removeStep', this.get('step.name')); + removeField(field) { + this.get('step.fields').removeObject(field); } } }); diff --git a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 index 615d5dd1..de59ed75 100644 --- a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 @@ -7,7 +7,7 @@ export default Ember.Controller.extend({ }, remove() { - this.get('model').destroy().then(() => { + this.get('model').remove().then(() => { this.transitionToRoute('adminWizardsCustom'); }); }, @@ -19,8 +19,8 @@ export default Ember.Controller.extend({ }); }, - removeStep(name) { - this.get('model.steps').findBy('name', name); + removeStep(step) { + this.get('model.steps').removeObject(step); } } }); diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index ebb03a11..3dc3eef1 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -1,20 +1,36 @@ import { ajax } from 'discourse/lib/ajax'; +import { default as computed } from 'ember-addons/ember-computed-decorators'; const CustomWizard = Discourse.Model.extend({ steps: Ember.A(), + @computed('name') + dasherizedName(name) { + return Ember.String.dasherize(name); + }, + save() { - const steps = JSON.stringify(this.get('steps').toArray()); - return ajax(`/admin/wizards/custom/${this.get('name')}`, { + const wizard = { + id: this.get('id'), + steps: this.get('steps').toArray(), + name: this.get('name') + }; + + return ajax(`/admin/wizards/custom/save`, { type: 'PUT', - data: { steps } + data: { + wizard: JSON.stringify(wizard) + } }); }, - destroy() { - return ajax(`/admin/wizards/custom/${this.get('name')}`, { - type: 'DELETE' - }); + remove() { + return ajax(`/admin/wizards/custom/remove`, { + type: 'DELETE', + data: { + id: this.get('id') + } + }).then(() => this.destroy()); } }); @@ -31,6 +47,7 @@ CustomWizard.reopenClass({ steps.forEach((s) => { s.fields = Ember.A(s.fields); + s.fields.forEach((f) => f.choices = Ember.A(f.choices)); s.actions = Ember.A(s.actions); }); diff --git a/assets/javascripts/discourse/routes/admin-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizard.js.es6 index 6dc2de60..c60c7227 100644 --- a/assets/javascripts/discourse/routes/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizard.js.es6 @@ -4,14 +4,14 @@ export default Discourse.Route.extend({ model(params) { if (params.name === 'new') { this.set('new', true); - return CustomWizard.create(); + return CustomWizard.create({ name: '', steps: []}); } this.set('new', false); - const wizard = this.modelFor('admin-wizards-custom').findBy('name', params.name ); + const wizard = this.modelFor('admin-wizards-custom').findBy('dasherizedName', params.name ); - if (!wizard) { return this.transitionTo('adminWizardsCustom.index'); } + if (!wizard) return this.transitionTo('adminWizardsCustom.index'); return wizard; }, diff --git a/assets/javascripts/discourse/templates/admin-wizard.hbs b/assets/javascripts/discourse/templates/admin-wizard.hbs index 8fb77453..7aa2de2a 100644 --- a/assets/javascripts/discourse/templates/admin-wizard.hbs +++ b/assets/javascripts/discourse/templates/admin-wizard.hbs @@ -8,10 +8,11 @@ {{#if model.steps}} {{#each model.steps as |s|}} {{wizard-custom-step step=s}} + {{d-button action='removeStep' actionParam=s label='admin.wizard.step.remove'}} {{/each}} {{/if}} - {{d-button action='addStep' label='admin.wizard.add_step'}} + {{d-button action='addStep' label='admin.wizard.step.add'}}