From f6251ace06b6d260368cced757d7ba0180c90e0d Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sun, 22 Oct 2017 11:37:58 +0800 Subject: [PATCH] various --- .../components/wizard-custom-action.js.es6 | 8 ++- .../discourse/models/custom-wizard.js.es6 | 6 +- .../routes/admin-wizard-submissions.js.es6 | 4 +- .../routes/admin-wizards-custom.js.es6 | 2 +- .../routes/admin-wizards-submissions.js.es6 | 2 +- .../templates/admin-wizards-submissions.hbs | 4 +- .../components/wizard-custom-action.hbs | 9 ++- .../wizard/initializers/custom.js.es6 | 1 + .../javascripts/wizard/models/custom.js.es6 | 2 + .../wizard/routes/custom-index.js.es6 | 11 +-- .../wizard/routes/custom-steps.js.es6 | 5 ++ .../javascripts/wizard/routes/custom.js.es6 | 3 +- assets/stylesheets/wizard/wizard_custom.scss | 4 ++ assets/stylesheets/wizard_custom_admin.scss | 10 ++- config/locales/client.en.yml | 67 ++++++++++--------- config/locales/server.en.yml | 1 + controllers/admin.rb | 22 ++---- controllers/wizard.rb | 11 ++- lib/builder.rb | 15 +++-- lib/step_updater.rb | 1 + lib/wizard.rb | 14 ++-- plugin.rb | 7 +- 22 files changed, 124 insertions(+), 85 deletions(-) create mode 100644 assets/javascripts/wizard/routes/custom-steps.js.es6 diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index fa900ac9..146a24d4 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/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'), diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index bf13fbc7..77971cd6 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/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; diff --git a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 b/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 index db19a780..824606ec 100644 --- a/assets/javascripts/discourse/routes/admin-wizard-submissions.js.es6 +++ b/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) { diff --git a/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 index 86893505..49803495 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-custom.js.es6 +++ b/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) { diff --git a/assets/javascripts/discourse/routes/admin-wizards-submissions.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-submissions.js.es6 index a30cdd5b..214f4b4d 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-submissions.js.es6 +++ b/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) { diff --git a/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs b/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs index 00da5748..f0047e88 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-submissions.hbs @@ -1,9 +1,9 @@
    - {{#each model as |s|}} + {{#each model as |w|}}
  • - {{#link-to "adminWizardSubmissions" s.id}}{{s.name}}{{/link-to}} + {{#link-to "adminWizardSubmissions" w.id}}{{w.name}}{{/link-to}}
  • {{/each}}
diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 8e542c93..d8d2140c 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -13,6 +13,7 @@
{{combo-box value=action.type content=types}} +
@@ -45,7 +46,7 @@
- + {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}}
{{/if}} @@ -80,10 +81,16 @@ allowedUsers="true"}} + +
+ + {{wizard-custom-input inputs=action.add_fields valueContent=wizardFields noneValue='admin.wizard.action.none'}} +
{{/if}} {{#if updateProfile}}
+ {{wizard-custom-input inputs=action.profile_updates valueContent=profileFields keyContent=wizardFields diff --git a/assets/javascripts/wizard/initializers/custom.js.es6 b/assets/javascripts/wizard/initializers/custom.js.es6 index 473e9f1f..10340365 100644 --- a/assets/javascripts/wizard/initializers/custom.js.es6 +++ b/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' }); }); }); diff --git a/assets/javascripts/wizard/models/custom.js.es6 b/assets/javascripts/wizard/models/custom.js.es6 index 79f8b1c8..021ad1ce 100644 --- a/assets/javascripts/wizard/models/custom.js.es6 +++ b/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); diff --git a/assets/javascripts/wizard/routes/custom-index.js.es6 b/assets/javascripts/wizard/routes/custom-index.js.es6 index 08aa8eb7..3d8a5e1d 100644 --- a/assets/javascripts/wizard/routes/custom-index.js.es6 +++ b/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); + } } }, diff --git a/assets/javascripts/wizard/routes/custom-steps.js.es6 b/assets/javascripts/wizard/routes/custom-steps.js.es6 new file mode 100644 index 00000000..b1dff4ff --- /dev/null +++ b/assets/javascripts/wizard/routes/custom-steps.js.es6 @@ -0,0 +1,5 @@ +export default Ember.Route.extend({ + redirect() { + this.transitionTo('custom.index'); + } +}); diff --git a/assets/javascripts/wizard/routes/custom.js.es6 b/assets/javascripts/wizard/routes/custom.js.es6 index 38490a55..7faeed35 100644 --- a/assets/javascripts/wizard/routes/custom.js.es6 +++ b/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({ diff --git a/assets/stylesheets/wizard/wizard_custom.scss b/assets/stylesheets/wizard/wizard_custom.scss index d8161458..c742528b 100644 --- a/assets/stylesheets/wizard/wizard_custom.scss +++ b/assets/stylesheets/wizard/wizard_custom.scss @@ -33,6 +33,10 @@ width: 200px; line-height: 24px; } + + ul { + padding: 0; + } } .wizard-step-form .wizard-btn { diff --git a/assets/stylesheets/wizard_custom_admin.scss b/assets/stylesheets/wizard_custom_admin.scss index 1e3dc6aa..d44c53d0 100644 --- a/assets/stylesheets/wizard_custom_admin.scss +++ b/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 { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 1be29877..e521511d 100644 --- a/config/locales/client.en.yml +++ b/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." diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 64ec3e73..92647923 100644 --- a/config/locales/server.en.yml +++ b/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." diff --git a/controllers/admin.rb b/controllers/admin.rb index c0f197d1..d9bf415e 100644 --- a/controllers/admin.rb +++ b/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) + + submissions = [*rows].map { |r| ::JSON.parse(r.value) } render json: success_json.merge(submissions: submissions) end - - 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) - end end diff --git a/controllers/wizard.rb b/controllers/wizard.rb index d03e56c0..9c5539aa 100644 --- a/controllers/wizard.rb +++ b/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 diff --git a/lib/builder.rb b/lib/builder.rb index 28d0849b..1d88b7ef 100644 --- a/lib/builder.rb +++ b/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 diff --git a/lib/step_updater.rb b/lib/step_updater.rb index 05e60d76..f267db9d 100644 --- a/lib/step_updater.rb +++ b/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 diff --git a/lib/wizard.rb b/lib/wizard.rb index a3ae6a5d..acc37232 100644 --- a/lib/wizard.rb +++ b/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 diff --git a/plugin.rb b/plugin.rb index a1980d2b..7694de26 100644 --- a/plugin.rb +++ b/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