From a3f9135698d437708ad9ea51e8dd9648ae82d738 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sun, 24 Sep 2017 11:01:18 +0800 Subject: [PATCH] Working wizard creation --- .../components/wizard-custom-action.js.es6 | 9 +----- .../components/wizard-custom-field.js.es6 | 12 +++++-- .../components/wizard-custom-step.js.es6 | 6 ++-- .../discourse/controllers/admin-wizard.js.es6 | 6 ++-- .../discourse/models/custom-wizard.js.es6 | 31 ++++++++++++++----- .../discourse/routes/admin-wizard.js.es6 | 6 ++-- .../discourse/templates/admin-wizard.hbs | 3 +- .../templates/admin-wizards-custom.hbs | 2 +- .../components/wizard-custom-field.hbs | 2 +- .../components/wizard-custom-step.hbs | 3 +- config/locales/client.en.yml | 5 +-- controllers/admin.rb | 25 ++++++--------- lib/wizard.rb | 3 +- plugin.rb | 6 ++-- 14 files changed, 64 insertions(+), 55 deletions(-) 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'}}
diff --git a/assets/javascripts/discourse/templates/admin-wizards-custom.hbs b/assets/javascripts/discourse/templates/admin-wizards-custom.hbs index bcbdce91..2cb65c2f 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-custom.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-custom.hbs @@ -3,7 +3,7 @@ diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index 82dbe4cb..8a5fe33a 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -14,7 +14,7 @@ {{#if isDropdown}} {{i18n 'admin.wizard.field.choices_label'}} {{#each field.choices as |c|}} - {{input type='text' value=c}} + {{input type='text' value=c.label}} {{/each}} {{d-button action='addChoice' label='admin.wizard.field.add_choice'}} {{/if}} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs index b74b1488..4d167d55 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs @@ -15,6 +15,7 @@ {{#each step.fields as |f|}} {{wizard-custom-field field=f}} + {{d-button action='removeField' actionParam=f label="admin.wizard.field.remove"}} {{/each}} {{d-button action='addField' label='admin.wizard.step.add_field'}} @@ -26,5 +27,3 @@ {{#if allowAddAction}} {{d-button action='addAction' label='admin.wizard.step.add_action'}} {{/if}} - -{{d-button action='removeStep' label='admin.wizard.remove_step'}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index d6114f14..a2b3d1a2 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -7,8 +7,6 @@ en: custom_label: "Custom" name: "Name" name_placeholder: "name of the wizard" - add_step: "Add Step" - remove_step: "Remove Step" save: "Save Wizard" remove: "Delete Wizard" step: @@ -18,6 +16,8 @@ en: banner_placeholder: "This image will appear under the title" description: "Step Description" description_placeholder: "This will appear underneath the title and / or title" + add: "Add Step" + remove: "Remove Step" add_field: "Add Field" add_action: "Add Action" field: @@ -27,5 +27,6 @@ en: choices_label: "Dropdown Choices" add_choice: "Add Choice" required: "Field Required" + remove: "Remove Field" action: email: "Email" diff --git a/controllers/admin.rb b/controllers/admin.rb index 1c0df935..404c4f5f 100644 --- a/controllers/admin.rb +++ b/controllers/admin.rb @@ -7,42 +7,35 @@ class CustomWizard::AdminController < ::ApplicationController end def save - params.require(:name) - params.permit(:steps) + params.require(:wizard) - wizard = { name: params[:name] } + wizard = ::JSON.parse(params[:wizard]) - wizard['steps'] = params[:steps] if params[:steps] + wizard["id"] = SecureRandom.hex(8) if !wizard["id"] - key = params[:name].downcase - - PluginStore.set('custom_wizards', key, wizard) + PluginStore.set('custom_wizards', wizard["id"], wizard) render json: success_json end def remove - params.require(:name) + params.require(:id) - key = params[:name].downcase - - PluginStore.remove('custom_wizards', key) + PluginStore.remove('custom_wizards', params[:id]) render json: success_json end def find - params.require(:name) + params.require(:id) - key = params[:name].downcase - - wizard = PluginStore.get('custom_wizards', key) + wizard = PluginStore.get('custom_wizards', params[:id]) render json: success_json.merge(wizard: wizard) end def all - rows = PluginStoreRow.where(plugin_name: 'custom_wizards') + rows = PluginStoreRow.where(plugin_name: 'custom_wizards').order(:id) wizards = rows ? [*rows].map do |r| CustomWizard::Wizard.new(r.value) diff --git a/lib/wizard.rb b/lib/wizard.rb index 2ed526d1..5e151bba 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -4,7 +4,8 @@ class CustomWizard::Wizard def initialize(data) parsed = ::JSON.parse(data) + @id = parsed['id'] @name = parsed['name'] - @steps = JSON.parse(parsed['steps']) + @steps = parsed['steps'] end end diff --git a/plugin.rb b/plugin.rb index 66b9a287..fb86bdcd 100644 --- a/plugin.rb +++ b/plugin.rb @@ -18,9 +18,9 @@ after_initialize do get 'custom' => 'admin#index' get 'custom/new' => 'admin#index' get 'custom/all' => "admin#all" - get 'custom/:name' => "admin#find" - put 'custom/:name' => "admin#save" - delete 'custom/:name' => "admin#remove" + get 'custom/:id' => "admin#find" + put 'custom/save' => "admin#save" + delete 'custom/remove' => "admin#remove" end require_dependency 'admin_constraint'