From 564985e46e8ef7149ab206f1937a05397fadb5e3 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 11 Nov 2021 17:20:10 +0530 Subject: [PATCH] FEATURE: allow 'New Topic' button to redirect to a wizard --- .../create-topic-wizard.hbs | 16 +++++++++++++ .../create-topic-wizard.js.es6 | 24 +++++++++++++++++++ .../initializers/custom-wizard-edits.js.es6 | 18 ++++++++++++++ config/locales/client.en.yml | 3 +++ plugin.rb | 1 + 5 files changed, 62 insertions(+) create mode 100644 assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.hbs create mode 100644 assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.js.es6 diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.hbs new file mode 100644 index 00000000..027296f0 --- /dev/null +++ b/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.hbs @@ -0,0 +1,16 @@ +

{{i18n "admin.wizard.category_settings.create_topic_wizard"}}

+ +
+ +
+ {{combo-box + value=wizardListVal + content=wizardList + onChange=(action "changeWizard") + options=(hash + none="admin.wizard.select" + )}} +
+
diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.js.es6 b/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.js.es6 new file mode 100644 index 00000000..0ff16c29 --- /dev/null +++ b/assets/javascripts/discourse/connectors/category-custom-settings/create-topic-wizard.js.es6 @@ -0,0 +1,24 @@ +import { ajax } from "discourse/lib/ajax"; +import CustomWizard from "../../models/custom-wizard"; +import { popupAjaxError } from "discourse/lib/ajax-error"; + +export default { + setupComponent(attrs, component) { + CustomWizard.all() + .then(result => { + component.set('wizardList', result); + }) + .catch(popupAjaxError); + component.set( + 'wizardListVal', + attrs.category.custom_fields.create_topic_wizard + ); + }, + + actions: { + changeWizard(wizard){ + this.set('wizardListVal', wizard); + this.set('category.custom_fields.create_topic_wizard', wizard); + } + } +} diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 63ddd5e8..d615e5bf 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -1,4 +1,6 @@ import DiscourseURL from "discourse/lib/url"; +import { withPluginApi } from "discourse/lib/plugin-api"; +import getUrl from "discourse-common/lib/get-url"; export default { name: "custom-wizard-edits", @@ -16,5 +18,21 @@ export default { } return existing.apply(this, [path, opts]); }; + + withPluginApi("0.8.7", (api) => { + api.modifyClass("component:d-navigation", { + pluginId: 'custom-wizard', + actions: { + clickCreateTopicButton(){ + let createTopicWizard = this.get('category.custom_fields.create_topic_wizard'); + if (createTopicWizard) { + window.location.href = getUrl(`/w/${createTopicWizard}`); + } else { + this._super(); + } + } + } + }) + }); }, }; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b07fd84a..cbcf6e5a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -60,6 +60,9 @@ en: select_type: "Select a type" condition: "Condition" index: "Index" + category_settings: + create_topic_wizard: "Create Topic Wizard" + select_wizard: "Select a wizard you want to use for creating topics in this category" message: wizard: diff --git a/plugin.rb b/plugin.rb index b4d09421..356650f2 100644 --- a/plugin.rb +++ b/plugin.rb @@ -117,6 +117,7 @@ after_initialize do end Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty) + Site.preloaded_category_custom_fields << "create_topic_wizard" add_class_method(:wizard, :user_requires_completion?) do |user| wizard_result = self.new(user).requires_completion?