Browse Source

Working wizard creation

Angus McLeod 7 years ago
parent
commit
a3f9135698

+ 1 - 8
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')
 });

+ 9 - 3
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());
     }
   }
 });

+ 2 - 4
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);
     }
   }
 });

+ 3 - 3
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);
     }
   }
 });

+ 24 - 7
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);
     });
 

+ 3 - 3
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;
   },

+ 2 - 1
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'}}
 
   <div class='buttons'>
     <button {{action "save"}} disabled={{disableSave}} class='btn btn-primary'>{{i18n 'admin.wizard.save'}}</button>

+ 1 - 1
assets/javascripts/discourse/templates/admin-wizards-custom.hbs

@@ -3,7 +3,7 @@
     <ul>
       {{#each model as |w|}}
         <li>
-          {{#link-to "adminWizard" w.name}}{{w.name}}{{/link-to}}
+          {{#link-to "adminWizard" w.dasherizedName}}{{w.name}}{{/link-to}}
         </li>
       {{/each}}
     </ul>

+ 1 - 1
assets/javascripts/discourse/templates/components/wizard-custom-field.hbs

@@ -14,7 +14,7 @@
   {{#if isDropdown}}
     <span>{{i18n 'admin.wizard.field.choices_label'}}</span>
     {{#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}}

+ 1 - 2
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'}}

+ 3 - 2
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"

+ 9 - 16
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)
-
-    key = params[:name].downcase
+    params.require(:id)
 
-    PluginStore.remove('custom_wizards', key)
+    PluginStore.remove('custom_wizards', params[:id])
 
     render json: success_json
   end
 
   def find
-    params.require(:name)
-
-    key = params[:name].downcase
+    params.require(:id)
 
-    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)

+ 2 - 1
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

+ 3 - 3
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'