Angus McLeod 7 anos atrás
pai
commit
f6251ace06

+ 7 - 1
assets/javascripts/discourse/components/wizard-custom-action.js.es6

@@ -1,5 +1,11 @@
 import { default as computed } from 'ember-addons/ember-computed-decorators';
 
+const ACTION_TYPES = [
+  { id: 'create_topic', name: 'create_topic *' },
+  { id: 'update_profile', name: 'update_profile *' },
+  { id: 'send_message', name: 'send_message *' }
+];
+
 const PROFILE_FIELDS = [
   'name',
   'email',
@@ -19,7 +25,7 @@ const PROFILE_FIELDS = [
 
 export default Ember.Component.extend({
   classNames: 'wizard-custom-action',
-  types: ['create_topic', 'update_profile', 'send_message'],
+  types: ACTION_TYPES,
   profileFields: PROFILE_FIELDS,
   createTopic: Ember.computed.equal('action.type', 'create_topic'),
   updateProfile: Ember.computed.equal('action.type', 'update_profile'),

+ 3 - 3
assets/javascripts/discourse/models/custom-wizard.js.es6

@@ -102,7 +102,7 @@ const CustomWizard = Discourse.Model.extend({
 });
 
 CustomWizard.reopenClass({
-  findAll() {
+  all() {
     return ajax("/admin/wizards/custom/all", {
       type: 'GET'
     }).then(result => {
@@ -110,8 +110,8 @@ CustomWizard.reopenClass({
     });
   },
 
-  findAllSubmissions() {
-    return ajax("/admin/wizards/submissions/all", {
+  submissions(wizardId) {
+    return ajax(`/admin/wizards/submissions/${wizardId}`, {
       type: "GET"
     }).then(result => {
       return result.submissions;

+ 3 - 1
assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6

@@ -1,6 +1,8 @@
+import CustomWizard from '../models/custom-wizard';
+
 export default Discourse.Route.extend({
   model(params) {
-    return this.modelFor('admin-wizards-submissions').findBy('id', params.wizard_id);
+    return CustomWizard.submissions(params.wizard_id);
   },
 
   setupController(controller, model) {

+ 1 - 1
assets/javascripts/discourse/routes/admin-wizards-custom.js.es6

@@ -2,7 +2,7 @@ import CustomWizard from '../models/custom-wizard';
 
 export default Discourse.Route.extend({
   model() {
-    return CustomWizard.findAll();
+    return CustomWizard.all();
   },
 
   afterModel(model) {

+ 1 - 1
assets/javascripts/discourse/routes/admin-wizards-submissions.js.es6

@@ -2,7 +2,7 @@ import CustomWizard from '../models/custom-wizard';
 
 export default Discourse.Route.extend({
   model() {
-    return CustomWizard.findAllSubmissions();
+    return CustomWizard.all();
   },
 
   afterModel(model, transition) {

+ 2 - 2
assets/javascripts/discourse/templates/admin-wizards-submissions.hbs

@@ -1,9 +1,9 @@
 <div class='row'>
   <div class='content-list wizard-list'>
     <ul>
-      {{#each model as |s|}}
+      {{#each model as |w|}}
         <li>
-          {{#link-to "adminWizardSubmissions" s.id}}{{s.name}}{{/link-to}}
+          {{#link-to "adminWizardSubmissions" w.id}}{{w.name}}{{/link-to}}
         </li>
       {{/each}}
     </ul>

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

@@ -13,6 +13,7 @@
   </div>
   <div class="setting-value">
     {{combo-box value=action.type content=types}}
+    <label>*{{i18n 'admin.wizard.action.requires_save'}}</label>
   </div>
 </div>
 
@@ -45,7 +46,7 @@
   </div>
 
   <div class="setting full">
-    <label>{{i18n "admin.wizard.action.create_topic.add_fields"}}</label>
+    <label>{{i18n "admin.wizard.action.add_fields" type='Topic'}}</label>
     {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}}
   </div>
 {{/if}}
@@ -80,10 +81,16 @@
                       allowedUsers="true"}}
     </div>
   </div>
+
+  <div class="setting full">
+    <label>{{i18n "admin.wizard.action.add_fields" type='Message'}}</label>
+    {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}}
+  </div>
 {{/if}}
 
 {{#if updateProfile}}
   <div class="setting full">
+    <label>{{i18n "admin.wizard.action.add_fields" type='Profile'}}</label>
     {{wizard-custom-input inputs=action.profile_updates
                           valueContent=profileFields
                           keyContent=wizardFields

+ 1 - 0
assets/javascripts/wizard/initializers/custom.js.es6

@@ -18,6 +18,7 @@ export default {
 
     Router.map(function() {
       this.route('custom', { path: '/:wizard_id' }, function() {
+        this.route('steps');
         this.route('step', { path: '/steps/:step_id' });
       });
     });

+ 2 - 0
assets/javascripts/wizard/models/custom.js.es6

@@ -12,6 +12,8 @@ export function findCustomWizard(wizardId) {
   return ajax({ url: `/w/${wizardId}` }).then(result => {
     const wizard = result.wizard;
 
+    if (!wizard) return null;
+
     if (!wizard.completed) {
       wizard.steps = wizard.steps.map(step => {
         const stepObj = Step.create(step);

+ 7 - 4
assets/javascripts/wizard/routes/custom-index.js.es6

@@ -1,10 +1,13 @@
 export default Ember.Route.extend({
   beforeModel() {
     const appModel = this.modelFor('custom');
-    if (appModel.completed) {
-      this.set('completed', true);
-    } else if (appModel.start) {
-      this.replaceWith('custom.step', appModel.start);
+
+    if (appModel) {
+      if (appModel.completed) {
+        this.set('completed', true);
+      } else if (appModel.start) {
+        this.replaceWith('custom.step', appModel.start);
+      }
     }
   },
 

+ 5 - 0
assets/javascripts/wizard/routes/custom-steps.js.es6

@@ -0,0 +1,5 @@
+export default Ember.Route.extend({
+  redirect() {
+    this.transitionTo('custom.index');
+  }
+});

+ 2 - 1
assets/javascripts/wizard/routes/custom.js.es6

@@ -16,8 +16,9 @@ export default Ember.Route.extend({
   },
 
   setupController(controller, model) {
+    const background = model ? model.get('background') : 'AliceBlue';
     Ember.run.scheduleOnce('afterRender', this, function(){
-      $('body.custom-wizard').css('background', model.get('background'));
+      $('body.custom-wizard').css('background', background);
     });
 
     controller.setProperties({

+ 4 - 0
assets/stylesheets/wizard/wizard_custom.scss

@@ -33,6 +33,10 @@
       width: 200px;
       line-height: 24px;
     }
+
+    ul {
+      padding: 0;
+    }
   }
 
   .wizard-step-form .wizard-btn {

+ 8 - 2
assets/stylesheets/wizard_custom_admin.scss

@@ -44,8 +44,14 @@
       width: 90px;
     }
 
-    .setting-value span {
-      font-size: 0.929em;
+    .setting-value {
+      label {
+        font-size: 0.85em;
+      }
+
+      span {
+        font-size: 0.929em;
+      }
     }
 
     &.full {

+ 36 - 31
config/locales/client.en.yml

@@ -64,48 +64,53 @@ en:
           title: "Title"
           post: "Post"
           none: "Select a field"
+          requires_save: "Requires 'Save' to be turned on."
+          add_fields: "Add Fields To {{type}}"
           send_message:
             label: "Send Message"
             recipient: "Recipient"
           create_topic:
             label: "Create Topic"
             category: "Category"
-            add_fields: "Add Fields"
           update_profile:
             label: "Update Profile"
             wizard_field: "Wizard Field"
             profile_field: "Profile Field"
 
   wizard_js:
+    location:
+      name:
+        title: "Name (optional)"
+        desc: "e.g. P. Sherman Dentist"
+      street:
+        title: "Number and Street"
+        desc: "e.g. 42 Wallaby Way"
+      postalcode:
+        title: "Postal Code (Zip)"
+        desc: "e.g. 2548"
+      city:
+        title: "City, Town or Village"
+        desc: "e.g. Sydney"
+      country_code:
+        title: "Country"
+        placeholder: "Select a Country"
+      query:
+        title: "Address"
+        desc: "e.g. 42 Wallaby Way, Sydney."
+      geo:
+        desc: "Locations provided by {{provider}}"
+        btn:
+          label: "Find Location"
+        results: "Locations"
+        no_results: "No results. Please double check the spelling."
+        show_map: "Show Map"
+      validation:
+        city: "Please enter a city or town."
+        countrycode: "Please enter a country."
+        geo_location: "Search and select a result."
+
+    select_box:
+      filter_placeholder: "Search..."
+
     wizard:
       completed: "You have completed this wizard."
-      location:
-        name:
-          title: "Name (optional)"
-          desc: "e.g. P. Sherman Dentist"
-        street:
-          title: "Number and Street"
-          desc: "e.g. 42 Wallaby Way"
-        postalcode:
-          title: "Postal Code (Zip)"
-          desc: "e.g. 2548"
-        city:
-          title: "City, Town or Village"
-          desc: "e.g. Sydney"
-        country_code:
-          title: "Country"
-          placeholder: "Select a Country"
-        query:
-          title: "Address"
-          desc: "e.g. 42 Wallaby Way, Sydney."
-        geo:
-          desc: "Locations provided by {{provider}}"
-          btn:
-            label: "Find Location"
-          results: "Locations"
-          no_results: "No results. Please double check the spelling."
-          show_map: "Show Map"
-        validation:
-          city: "Please enter a city or town."
-          countrycode: "Please enter a country."
-          geo_location: "Search and select a result."

+ 1 - 0
config/locales/server.en.yml

@@ -3,3 +3,4 @@ en:
     custom_title: "Wizard"
     field:
       too_short: "%{label} must be at least %{min} characters"
+    none: "We couldn't find a wizard at that address."

+ 4 - 18
controllers/admin.rb

@@ -44,27 +44,13 @@ class CustomWizard::AdminController < ::ApplicationController
     render json: success_json.merge(wizards: wizards)
   end
 
-  def find_submissions
+  def submissions
     params.require(:wizard_id)
 
-    wizard = PluginStore.get('custom_wizard_submissions', params[:wizard_id].underscore)
+    rows = PluginStoreRow.where(plugin_name: "#{params[:wizard_id]}_submissions").order(:id)
 
-    render json: success_json.merge(submissions: submissions)
-  end
+    submissions = [*rows].map { |r| ::JSON.parse(r.value) }
 
-  def submissions
-    rows = PluginStoreRow.where(plugin_name: 'custom_wizard_submissions').order(:id)
-
-    all = [*rows].map do |r|
-      wizard = PluginStore.get('custom_wizard', r.key)
-      name = wizard ? wizard['name'] : r.key
-      {
-        id: r.key,
-        name: name,
-        submissions: ::JSON.parse(r.value)
-      }
-    end
-
-    render json: success_json.merge(submissions: all)
+    render json: success_json.merge(submissions: submissions)
   end
 end

+ 8 - 3
controllers/wizard.rb

@@ -5,14 +5,19 @@ class CustomWizard::WizardController < ::ApplicationController
 
   def wizard_page_title
     wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
-    wizard['name'] || wizard['id']
+    wizard ? (wizard['name'] || wizard['id']) : I18n.t('wizard.custom_title')
   end
 
   def index
     respond_to do |format|
       format.json do
-        wizard = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore).build
-        render_serialized(wizard, WizardSerializer)
+        template = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
+        if template.wizard.present?
+          wizard = template.build
+          render_serialized(wizard, WizardSerializer)
+        else
+          render json: { error: I18n.t('wizard.none') }
+        end
       end
       format.html {}
     end

+ 9 - 6
lib/builder.rb

@@ -1,9 +1,12 @@
 class CustomWizard::Builder
 
-  attr_accessor :wizard, :updater, :submission
+  attr_accessor :wizard, :updater, :submissions
 
   def initialize(user, wizard_id)
     data = PluginStore.get('custom_wizard', wizard_id)
+
+    return if data.blank?
+
     @template = CustomWizard::Template.new(data)
     @wizard = CustomWizard::Wizard.new(user,
       id: wizard_id,
@@ -12,7 +15,7 @@ class CustomWizard::Builder
       background: data["background"],
       name: data["name"]
     )
-    @submissions = Array.wrap(PluginStore.get("custom_wizard_submissions", wizard_id))
+    @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id))
   end
 
   def self.sorted_handlers
@@ -121,7 +124,7 @@ class CustomWizard::Builder
 
             if s['actions'] && s['actions'].length
               s['actions'].each do |a|
-                if a['type'] === 'create_topic'
+                if a['type'] === 'create_topic' && submission
                   title = submission[a['title']]
                   post = submission[a['post']]
 
@@ -177,7 +180,7 @@ class CustomWizard::Builder
                   end
                 end
 
-                if a['type'] === 'send_message'
+                if a['type'] === 'send_message' && submission
                   title = submission[a['title']]
                   post = submission[a['post']]
 
@@ -198,7 +201,7 @@ class CustomWizard::Builder
                   end
                 end
 
-                if a['type'] === 'update_profile' && a['profile_updates'].length
+                if a['type'] === 'update_profile' && a['profile_updates'].length && submission
                   user_updater = UserUpdater.new(user, user)
                   attributes = {}
                   a['profile_updates'].each do |pu|
@@ -222,7 +225,7 @@ class CustomWizard::Builder
               end
 
               @submissions.push(submission)
-              PluginStore.set('custom_wizard_submissions', @wizard.id, @submissions)
+              PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
             end
           end
         end

+ 1 - 0
lib/step_updater.rb

@@ -16,6 +16,7 @@ class CustomWizard::StepUpdater
 
     if success?
       UserHistory.create(action: UserHistory.actions[:custom_wizard_step],
+                         acting_user_id: @current_user.id,
                          context: @wizard.id,
                          subject: @step.id)
     end

+ 8 - 6
lib/wizard.rb

@@ -46,9 +46,10 @@ class CustomWizard::Wizard
   def start
     completed = ::UserHistory.where(
       acting_user_id: @user.id,
-      action: ::UserHistory.actions[:custom_wizard_step]
-    ).where(context: @steps.map(&:id))
-      .uniq.pluck(:context)
+      action: ::UserHistory.actions[:custom_wizard_step],
+      context: @id,
+      subject: @steps.map(&:id)
+    ).uniq.pluck(:subject)
 
     @steps.each do |s|
       return s unless completed.include?(s.id)
@@ -62,9 +63,10 @@ class CustomWizard::Wizard
 
     completed = ::UserHistory.where(
       acting_user_id: @user.id,
-      action: ::UserHistory.actions[:custom_wizard_step]
-    ).where(context: steps)
-      .distinct.order(:context).pluck(:context)
+      action: ::UserHistory.actions[:custom_wizard_step],
+      context: @id,
+      subject: steps
+    ).distinct.order(:subject).pluck(:subject)
 
     steps.sort == completed
   end

+ 3 - 4
plugin.rb

@@ -10,7 +10,7 @@ config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'as
 config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'assets', 'stylesheets', 'wizard')
 
 after_initialize do
-  UserHistory.actions[:custom_wizard_step] = 100
+  UserHistory.actions[:custom_wizard_step] = 1000
 
   require_dependency 'application_controller'
   module ::CustomWizard
@@ -22,7 +22,7 @@ after_initialize do
 
   CustomWizard::Engine.routes.draw do
     get ':wizard_id' => 'wizard#index'
-    get ':wizard_id/steps' => 'steps#index'
+    get ':wizard_id/steps' => 'wizard#index'
     get ':wizard_id/steps/:step_id' => 'wizard#index'
     put ':wizard_id/steps/:step_id' => 'steps#update'
   end
@@ -41,8 +41,7 @@ after_initialize do
       put 'admin/wizards/custom/save' => 'admin#save'
       delete 'admin/wizards/custom/remove' => 'admin#remove'
       get 'admin/wizards/submissions' => 'admin#index'
-      get 'admin/wizards/submissions/all' => 'admin#submissions'
-      get 'admin/wizards/submissions/:wizard_id' => 'admin#find_submissions'
+      get 'admin/wizards/submissions/:wizard_id' => 'admin#submissions'
     end
   end