From 290bacc4bd0bc8e365d7478a3dbcb2c0b43ee2b3 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 17 Jul 2020 17:02:48 +1000 Subject: [PATCH] Add wizard actions and fix field listings --- .../components/wizard-mapper-selector.js.es6 | 46 +++++++++++++++---- .../components/wizard-text-editor.js.es6 | 6 +++ .../admin-wizards-wizard-show.js.es6 | 2 +- .../discourse/lib/wizard-mapper.js.es6 | 1 + .../components/wizard-custom-action.hbs | 1 + .../components/wizard-text-editor.hbs | 7 +++ config/locales/client.en.yml | 3 ++ lib/custom_wizard/action.rb | 8 ++++ lib/custom_wizard/action_result.rb | 2 +- lib/custom_wizard/mapper.rb | 4 ++ 10 files changed, 70 insertions(+), 10 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 index 91af570b..7d37f74f 100644 --- a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 @@ -13,6 +13,7 @@ export default Component.extend({ showText: computed('activeType', function() { return this.showInput('text') }), showWizardField: computed('activeType', function() { return this.showInput('wizardField') }), + showWizardAction: computed('activeType', function() { return this.showInput('wizardAction') }), showUserField: computed('activeType', function() { return this.showInput('userField') }), showUserFieldOptions: computed('activeType', function() { return this.showInput('userFieldOptions') }), showCategory: computed('activeType', function() { return this.showInput('category') }), @@ -22,6 +23,7 @@ export default Component.extend({ showList: computed('activeType', function() { return this.showInput('list') }), textEnabled: computed('options.textSelection', 'inputType', function() { return this.optionEnabled('textSelection') }), wizardFieldEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }), + wizardActionEnabled: computed('options.wizardActionSelection', 'inputType', function() { return this.optionEnabled('wizardActionSelection') }), userFieldEnabled: computed('options.userFieldSelection', 'inputType', function() { return this.optionEnabled('userFieldSelection') }), userFieldOptionsEnabled: computed('options.userFieldOptionsSelection', 'inputType', function() { return this.optionEnabled('userFieldOptionsSelection') }), categoryEnabled: computed('options.categorySelection', 'inputType', function() { return this.optionEnabled('categorySelection') }), @@ -32,7 +34,7 @@ export default Component.extend({ groups: alias('site.groups'), categories: alias('site.categories'), - showComboBox: or('showWizardField', 'showUserField', 'showUserFieldOptions'), + showComboBox: or('showWizardField', 'showWizardAction', 'showUserField', 'showUserFieldOptions'), showMultiSelect: or('showCategory', 'showGroup'), hasTypes: gt('selectorTypes.length', 1), showTypes: false, @@ -73,20 +75,48 @@ export default Component.extend({ return type ? I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`) : null; }, - comboBoxAllowAny: alias('showWizardField'), + comboBoxAllowAny: or('showWizardField', 'showWizardAction'), - @discourseComputed('activeType') - comboBoxContent(activeType) { - const controller = getOwner(this).lookup('controller:admin-wizards-wizard-show'); - const wizardFields = controller.wizardFields; - const userFields = controller.userFields; + @discourseComputed + showController() { + return getOwner(this).lookup('controller:admin-wizards-wizard-show'); + }, + + @discourseComputed( + 'activeType', + 'showController.wizardFields.[]', + 'showController.wizard.actions.[]', + 'showController.userFields.[]', + 'showController.currentField.id', + 'showController.currentAction.id' + ) + comboBoxContent( + activeType, + wizardFields, + wizardActions, + userFields, + currentFieldId, + currentActionId + ) { let content; if (activeType === 'wizardField') { content = wizardFields; if (this.options.context === 'field') { - content = content.filter(field => field.id !== controller.currentField.id); + content = content.filter(field => field.id !== currentFieldId); + } + } + + if (activeType === 'wizardAction') { + content = wizardActions.map(a => ({ + id: a.id, + label: `${generateName(a.type)} (${a.id})`, + type: a.type + })); + + if (this.options.context === 'action') { + content = content.filter(a => a.id !== currentActionId); } } diff --git a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 index 25cf05e8..6b95eee9 100644 --- a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 +++ b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 @@ -11,6 +11,7 @@ export default Component.extend({ previewEnabled: true, fieldsEnabled: true, hasWizardFields: notEmpty('wizardFieldList'), + hasWizardActions: notEmpty('wizardActionList'), didReceiveAttrs() { this._super(...arguments); @@ -46,6 +47,11 @@ export default Component.extend({ return wizardFields.map((f) => ` w{${f.id}}`); }, + @discourseComputed('wizardActions') + wizardActionList(wizardActions) { + return wizardActions.map((a) => ` w{${a.id}}`); + }, + actions: { togglePreview() { this.toggleProperty('forcePreview'); diff --git a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 index fd01f605..fd8470e4 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6 @@ -46,7 +46,7 @@ export default Controller.extend({ I18n.t('admin.wizard.after_time_time_label'); }, - @discourseComputed('currentStep.id', 'wizard.save_submissions', 'wizard.steps.@each.fields[]') + @discourseComputed('currentStep.id', 'wizard.save_submissions', 'currentStep.fields.@each.label') wizardFields(currentStepId, saveSubmissions) { let steps = this.wizard.steps; if (!saveSubmissions) { diff --git a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 index 6d179ef6..470a78c4 100644 --- a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 @@ -80,6 +80,7 @@ const selectionTypes = [ 'text', 'list', 'wizardField', + 'wizardAction', 'userField', 'userFieldOptions', 'group', diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 6aa2eb49..8f331772 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -654,6 +654,7 @@ inputTypes='association' textSelection=true wizardFieldSelection=true + wizardActionSelection='key' userFieldSelection=true groupSelection='key' context='action' diff --git a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs index f9f6cc9b..36be715f 100644 --- a/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-text-editor.hbs @@ -28,6 +28,13 @@ {{wizardFieldList}} {{/if}} + + {{#if hasWizardActions}} + + {{/if}} {{/if}} {{/if}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 07f3f67b..8ebf661e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -94,6 +94,7 @@ en: label: text: "text" wizard_field: "wizard field" + wizard_action: "wizard action" user_field: "user field" user_field_options: "user field options" user: "user" @@ -106,6 +107,7 @@ en: text: "Enter text" property: "Select property" wizard_field: "Select field" + wizard_action: "Select action" user_field: "Select field" user_field_options: "Select field" user: "Select user" @@ -226,6 +228,7 @@ en: label: "Builder" user_properties: "User Properties" wizard_fields: "Wizard Fields" + wizard_actions: "Wizard Actions" placeholder: "Insert wizard fields using the field_id in w{}. Insert user properties using property in u{}." add_to_group: label: "Add to Group" diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 45bdbd35..dcd44f88 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -24,6 +24,10 @@ class CustomWizard::Action @result.handler.enqueue_jobs end + if @result.success? && @result.output.present? + data[action['id']] = @result.output + end + save_log end @@ -51,6 +55,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created topic", "id: #{post.topic.id}") result.handler = creator + result.output = post.topic.id end else log_error("invalid topic params", "title: #{params[:title]}; post: #{params[:raw]}") @@ -89,6 +94,7 @@ class CustomWizard::Action if creator.errors.blank? log_success("created message", "id: #{post.topic.id}") result.handler = creator + result.output = post.topic.id end else log_error( @@ -289,6 +295,7 @@ class CustomWizard::Action if group.save GroupActionLogger.new(user, group).log_change_group_settings log_success("Group created", group.name) + result.output = group.name else log_error("Group creation failed") end @@ -307,6 +314,7 @@ class CustomWizard::Action if category.save StaffActionLogger.new(user).log_category_creation(category) log_success("Category created", category.name) + result.output = category.id else log_error("Category creation failed") end diff --git a/lib/custom_wizard/action_result.rb b/lib/custom_wizard/action_result.rb index c731e752..80f2b55d 100644 --- a/lib/custom_wizard/action_result.rb +++ b/lib/custom_wizard/action_result.rb @@ -1,5 +1,5 @@ class CustomWizard::ActionResult - attr_accessor :success, :handler + attr_accessor :success, :handler, :output def initialize @success = false diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 44ccf2f5..e06a9da9 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -187,6 +187,10 @@ class CustomWizard::Mapper def map_wizard_field(value) data && !data.key?("submitted_at") && data[value] end + + def map_wizard_action(value) + data && !data.key?("submitted_at") && data[value] + end def map_user_field(value) if value.include?(User::USER_FIELD_PREFIX)