From 10df3208df861480edd5e55e335a7897bc6ccdca Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 7 Jul 2023 17:16:51 +0100 Subject: [PATCH 01/14] IMPROVE: Move pending wizard check to page change event --- .../initializers/custom-wizard-edits.js.es6 | 90 +++++++++++-------- .../custom-wizard-redirect.js.es6 | 44 --------- 2 files changed, 54 insertions(+), 80 deletions(-) delete mode 100644 assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index c974fafb..5da38fe8 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -21,6 +21,24 @@ export default { }; withPluginApi("0.8.36", (api) => { + api.onAppEvent('page:changed', (data) => { + const currentUser = container.lookup("service:current-user"); + const settings = container.lookup("service:site-settings"); + const redirectToWizard = currentUser.redirect_to_wizard; + const excludedPaths = settings.wizard_redirect_exclude_paths + .split("|") + .concat(["loading"]); + if ( + redirectToWizard && + (data.currentRouteName !== "customWizardStep") && + !(excludedPaths.find((p) => { + return data.currentRouteName.indexOf(p) > -1; + })) + ) { + window.location = "/w/" + redirectToWizard.dasherize(); + } + }); + api.modifyClass("component:d-navigation", { pluginId: "custom-wizard", actions: { @@ -42,47 +60,47 @@ export default { // Needed to ensure appEvents get registered when navigating between steps @observes("id") initOnStepChange() { - if (/wizard-field|wizard-step/.test(this.id)) { - this._initialize(); - } - }, + if(/ wizard - field | wizard - step /.test(this.id)) { + this._initialize(); + } + }, }); - api.modifyClass("component:d-editor", { - pluginId: "custom-wizard", + api.modifyClass("component:d-editor", { + pluginId: "custom-wizard", - didInsertElement() { - this._super(...arguments); + didInsertElement() { + this._super(...arguments); - if (this.wizardComposer) { - this.appEvents.on( - `wizard-editor:insert-text`, - this, - "_wizardInsertText" - ); - this.appEvents.on( - "wizard-editor:replace-text", - this, - "_wizardReplaceText" - ); - } - }, + if (this.wizardComposer) { + this.appEvents.on( + `wizard-editor:insert-text`, + this, + "_wizardInsertText" + ); + this.appEvents.on( + "wizard-editor:replace-text", + this, + "_wizardReplaceText" + ); + } + }, - _wizardInsertText(text, options) { - if ( - this.session.wizardEventFieldId === this.fieldId && - this.element - ) { - this.insertText(text, options); - } - }, + _wizardInsertText(text, options) { + if ( + this.session.wizardEventFieldId === this.fieldId && + this.element + ) { + this.insertText(text, options); + } + }, - _wizardReplaceText(oldVal, newVal, opts = {}) { - if (this.session.wizardEventFieldId === this.fieldId) { - this.replaceText(oldVal, newVal, opts); - } - }, - }); - }); + _wizardReplaceText(oldVal, newVal, opts = {}) { + if (this.session.wizardEventFieldId === this.fieldId) { + this.replaceText(oldVal, newVal, opts); + } + }, + }); +}); }, }; diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 deleted file mode 100644 index 1438f0d6..00000000 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ /dev/null @@ -1,44 +0,0 @@ -export default { - name: "custom-wizard-redirect", - after: "message-bus", - - initialize: function (container) { - const messageBus = container.lookup("service:message-bus"); - const siteSettings = container.lookup("service:site-settings"); - - if (!siteSettings.custom_wizard_enabled || !messageBus) { - return; - } - - messageBus.subscribe("/redirect_to_wizard", function (wizardId) { - const wizardUrl = window.location.origin + "/w/" + wizardId; - window.location.href = wizardUrl; - }); - - const ApplicationRoute = requirejs("discourse/routes/application").default; - - ApplicationRoute.reopen({ - actions: { - willTransition(transition) { - const redirectToWizard = this.get("currentUser.redirect_to_wizard"); - const excludedPaths = this.siteSettings.wizard_redirect_exclude_paths - .split("|") - .concat(["loading"]); - - if ( - redirectToWizard && - (!transition.intent.name || - !excludedPaths.find((p) => { - return transition.intent.name.indexOf(p) > -1; - })) - ) { - transition.abort(); - window.location = "/w/" + redirectToWizard.dasherize(); - } - - return this._super(transition); - }, - }, - }); - }, -}; From 39b947d003f602af08412fd7d8eba0504b6334f3 Mon Sep 17 00:00:00 2001 From: merefield Date: Mon, 10 Jul 2023 06:58:52 +0100 Subject: [PATCH 02/14] bump patch --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 9d67bd1c..70e5a037 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.4.10 +# version: 2.4.11 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From 2df920895d882f8ada5660c88182369379b382ae Mon Sep 17 00:00:00 2001 From: merefield Date: Mon, 10 Jul 2023 07:03:45 +0100 Subject: [PATCH 03/14] FE linting --- .../custom-wizard-composer-editor.js.es6 | 8 +- .../controllers/admin-wizards-api-show.js.es6 | 25 +----- .../initializers/custom-wizard-edits.js.es6 | 80 +++++++++---------- 3 files changed, 46 insertions(+), 67 deletions(-) diff --git a/assets/javascripts/discourse/components/custom-wizard-composer-editor.js.es6 b/assets/javascripts/discourse/components/custom-wizard-composer-editor.js.es6 index 5335da81..5560a126 100644 --- a/assets/javascripts/discourse/components/custom-wizard-composer-editor.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-composer-editor.js.es6 @@ -14,7 +14,8 @@ import { uploadIcon } from "discourse/lib/uploads"; import { dasherize } from "@ember/string"; import showModal from "discourse/lib/show-modal"; -const IMAGE_MARKDOWN_REGEX = /!\[(.*?)\|(\d{1,4}x\d{1,4})(,\s*\d{1,3}%)?(.*?)\]\((upload:\/\/.*?)\)(?!(.*`))/g; +const IMAGE_MARKDOWN_REGEX = + /!\[(.*?)\|(\d{1,4}x\d{1,4})(,\s*\d{1,3}%)?(.*?)\]\((upload:\/\/.*?)\)(?!(.*`))/g; export default ComposerEditor.extend({ classNameBindings: ["fieldClass"], @@ -116,9 +117,8 @@ export default ComposerEditor.extend({ event.target.closest(".button-wrapper").dataset.imageIndex, 10 ); - const matchingPlaceholder = this.get("composer.reply").match( - IMAGE_MARKDOWN_REGEX - ); + const matchingPlaceholder = + this.get("composer.reply").match(IMAGE_MARKDOWN_REGEX); this.session.set("wizardEventFieldId", this.field.id); this.appEvents.trigger( diff --git a/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 index 11bf94d7..c08e820c 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 @@ -21,29 +21,8 @@ export default Controller.extend({ "application/x-www-form-urlencoded", ]), successCodes: selectKitContent([ - 100, - 101, - 102, - 200, - 201, - 202, - 203, - 204, - 205, - 206, - 207, - 208, - 226, - 300, - 301, - 302, - 303, - 303, - 304, - 305, - 306, - 307, - 308, + 100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, + 302, 303, 303, 304, 305, 306, 307, 308, ]), @discourseComputed( diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 5da38fe8..eb285d15 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -21,7 +21,7 @@ export default { }; withPluginApi("0.8.36", (api) => { - api.onAppEvent('page:changed', (data) => { + api.onAppEvent("page:changed", (data) => { const currentUser = container.lookup("service:current-user"); const settings = container.lookup("service:site-settings"); const redirectToWizard = currentUser.redirect_to_wizard; @@ -30,10 +30,10 @@ export default { .concat(["loading"]); if ( redirectToWizard && - (data.currentRouteName !== "customWizardStep") && - !(excludedPaths.find((p) => { + data.currentRouteName !== "customWizardStep" && + !excludedPaths.find((p) => { return data.currentRouteName.indexOf(p) > -1; - })) + }) ) { window.location = "/w/" + redirectToWizard.dasherize(); } @@ -60,47 +60,47 @@ export default { // Needed to ensure appEvents get registered when navigating between steps @observes("id") initOnStepChange() { - if(/ wizard - field | wizard - step /.test(this.id)) { - this._initialize(); - } - }, + if (/ wizard - field | wizard - step /.test(this.id)) { + this._initialize(); + } + }, }); - api.modifyClass("component:d-editor", { - pluginId: "custom-wizard", + api.modifyClass("component:d-editor", { + pluginId: "custom-wizard", - didInsertElement() { - this._super(...arguments); + didInsertElement() { + this._super(...arguments); - if (this.wizardComposer) { - this.appEvents.on( - `wizard-editor:insert-text`, - this, - "_wizardInsertText" - ); - this.appEvents.on( - "wizard-editor:replace-text", - this, - "_wizardReplaceText" - ); - } - }, + if (this.wizardComposer) { + this.appEvents.on( + `wizard-editor:insert-text`, + this, + "_wizardInsertText" + ); + this.appEvents.on( + "wizard-editor:replace-text", + this, + "_wizardReplaceText" + ); + } + }, - _wizardInsertText(text, options) { - if ( - this.session.wizardEventFieldId === this.fieldId && - this.element - ) { - this.insertText(text, options); - } - }, + _wizardInsertText(text, options) { + if ( + this.session.wizardEventFieldId === this.fieldId && + this.element + ) { + this.insertText(text, options); + } + }, - _wizardReplaceText(oldVal, newVal, opts = {}) { - if (this.session.wizardEventFieldId === this.fieldId) { - this.replaceText(oldVal, newVal, opts); - } - }, - }); -}); + _wizardReplaceText(oldVal, newVal, opts = {}) { + if (this.session.wizardEventFieldId === this.fieldId) { + this.replaceText(oldVal, newVal, opts); + } + }, + }); + }); }, }; From ccb7095b0095f4823491a0231f0fe1e38e297e5d Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 11 Jul 2023 08:24:36 +0100 Subject: [PATCH 04/14] bump patch --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 70e5a037..287419b2 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.4.11 +# version: 2.4.12 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From 0cec7432531483fe9689d07ff9c99b5e28e3ea87 Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 11 Jul 2023 08:48:20 +0100 Subject: [PATCH 05/14] fix for anon --- .../initializers/custom-wizard-edits.js.es6 | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index eb285d15..202025ef 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -24,18 +24,20 @@ export default { api.onAppEvent("page:changed", (data) => { const currentUser = container.lookup("service:current-user"); const settings = container.lookup("service:site-settings"); - const redirectToWizard = currentUser.redirect_to_wizard; - const excludedPaths = settings.wizard_redirect_exclude_paths - .split("|") - .concat(["loading"]); - if ( - redirectToWizard && - data.currentRouteName !== "customWizardStep" && - !excludedPaths.find((p) => { - return data.currentRouteName.indexOf(p) > -1; - }) - ) { - window.location = "/w/" + redirectToWizard.dasherize(); + if (currentUser) { + const redirectToWizard = currentUser.redirect_to_wizard; + const excludedPaths = settings.wizard_redirect_exclude_paths + .split("|") + .concat(["loading"]); + if ( + redirectToWizard && + data.currentRouteName !== "customWizardStep" && + !excludedPaths.find((p) => { + return data.currentRouteName.indexOf(p) > -1; + }) + ) { + window.location = "/w/" + redirectToWizard.dasherize(); + } } }); From 7fd4d9bd245a1c107e702d9c10532e39afc4a72e Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 11 Jul 2023 14:32:04 +0100 Subject: [PATCH 06/14] revert unintended changes --- .../discourse/initializers/custom-wizard-edits.js.es6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 202025ef..9f48ac00 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -62,7 +62,7 @@ export default { // Needed to ensure appEvents get registered when navigating between steps @observes("id") initOnStepChange() { - if (/ wizard - field | wizard - step /.test(this.id)) { + if (/wizard-field|wizard-step/.test(this.id)) { this._initialize(); } }, From 603886b39476fe246239d53bd395bb633881ee1c Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 11 Jul 2023 17:49:08 +0100 Subject: [PATCH 07/14] add test --- .../wizard-subscription-badge.js.es6 | 1 + .../initializers/custom-wizard-edits.js.es6 | 11 ++-------- test/javascripts/acceptance/wizard-test.js | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 b/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 index 301c618e..3adfe14a 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 @@ -25,6 +25,7 @@ export default Component.extend(Subscription, { }, click() { + debugger; DiscourseURL.routeTo(this.subscriptionLink); }, }); diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 9f48ac00..8c32a5c7 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -2,6 +2,7 @@ import DiscourseURL from "discourse/lib/url"; import { withPluginApi } from "discourse/lib/plugin-api"; import getUrl from "discourse-common/lib/get-url"; import { observes } from "discourse-common/utils/decorators"; +import { dasherize } from "@ember/string"; export default { name: "custom-wizard-edits", @@ -12,14 +13,6 @@ export default { return; } - const existing = DiscourseURL.routeTo; - DiscourseURL.routeTo = function (path, opts) { - if (path && path.indexOf("/w/") > -1) { - return (window.location = path); - } - return existing.apply(this, [path, opts]); - }; - withPluginApi("0.8.36", (api) => { api.onAppEvent("page:changed", (data) => { const currentUser = container.lookup("service:current-user"); @@ -36,7 +29,7 @@ export default { return data.currentRouteName.indexOf(p) > -1; }) ) { - window.location = "/w/" + redirectToWizard.dasherize(); + DiscourseURL.routeTo(`/w/${dasherize(redirectToWizard)}`); } } }); diff --git a/test/javascripts/acceptance/wizard-test.js b/test/javascripts/acceptance/wizard-test.js index e2e6ce04..05bd0e0a 100644 --- a/test/javascripts/acceptance/wizard-test.js +++ b/test/javascripts/acceptance/wizard-test.js @@ -13,6 +13,8 @@ import { wizardNoUser, wizardNotPermitted, } from "../helpers/wizard"; +import DiscourseURL from "discourse/lib/url"; +import sinon from "sinon"; acceptance("Wizard | Not logged in", function (needs) { needs.pretender((server, helper) => { @@ -54,6 +56,26 @@ acceptance("Wizard | Completed", function (needs) { }); }); +acceptance("Wizard | Redirect", function (needs) { + needs.user({ + redirect_to_wizard: "wizard" + }); + needs.pretender((server, helper) => { + server.get("/w/wizard.json", () => { + return helper.response(wizard); + }); + }); + + test("Redirect to pending Wizard", async function (assert) { + sinon.stub(DiscourseURL, "routeTo"); + await visit("/latest"); + assert.ok( + DiscourseURL.routeTo.calledWith("/w/wizard"), + "pending wizard routing works" + ); + }); +}); + acceptance("Wizard | Wizard", function (needs) { needs.user(); needs.pretender((server, helper) => { From 6beccd95f073ec0b135861ba238f5a268d032d0c Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 11 Jul 2023 17:55:19 +0100 Subject: [PATCH 08/14] linting --- .../discourse/components/wizard-subscription-badge.js.es6 | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 b/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 index 3adfe14a..301c618e 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription-badge.js.es6 @@ -25,7 +25,6 @@ export default Component.extend(Subscription, { }, click() { - debugger; DiscourseURL.routeTo(this.subscriptionLink); }, }); From bf2dcdba86652d4fe5dac9c73063f0c09e3b60e3 Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 11 Jul 2023 17:58:10 +0100 Subject: [PATCH 09/14] linting --- test/javascripts/acceptance/wizard-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/javascripts/acceptance/wizard-test.js b/test/javascripts/acceptance/wizard-test.js index 05bd0e0a..dd441649 100644 --- a/test/javascripts/acceptance/wizard-test.js +++ b/test/javascripts/acceptance/wizard-test.js @@ -58,7 +58,7 @@ acceptance("Wizard | Completed", function (needs) { acceptance("Wizard | Redirect", function (needs) { needs.user({ - redirect_to_wizard: "wizard" + redirect_to_wizard: "wizard", }); needs.pretender((server, helper) => { server.get("/w/wizard.json", () => { From 6a9f93bccb47aa24f24dd55d31188171a39b03e5 Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 11 Jul 2023 19:49:19 +0100 Subject: [PATCH 10/14] Add back message bus subscription --- .../discourse/initializers/custom-wizard-edits.js.es6 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 8c32a5c7..c010145c 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -7,12 +7,18 @@ import { dasherize } from "@ember/string"; export default { name: "custom-wizard-edits", initialize(container) { + const messageBus = container.lookup("service:message-bus"); const siteSettings = container.lookup("service:site-settings"); if (!siteSettings.custom_wizard_enabled) { return; } + messageBus.subscribe("/redirect_to_wizard", function (wizardId) { + const wizardUrl = window.location.origin + "/w/" + wizardId; + window.location.href = wizardUrl; + }); + withPluginApi("0.8.36", (api) => { api.onAppEvent("page:changed", (data) => { const currentUser = container.lookup("service:current-user"); From 7861e37224c263d7ff8837e2888f0915943281be Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Wed, 12 Jul 2023 11:04:59 +0100 Subject: [PATCH 11/14] restore original redirect file --- .../initializers/custom-wizard-edits.js.es6 | 33 ++++----------- .../custom-wizard-redirect.js.es6 | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index c010145c..c974fafb 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -2,44 +2,25 @@ import DiscourseURL from "discourse/lib/url"; import { withPluginApi } from "discourse/lib/plugin-api"; import getUrl from "discourse-common/lib/get-url"; import { observes } from "discourse-common/utils/decorators"; -import { dasherize } from "@ember/string"; export default { name: "custom-wizard-edits", initialize(container) { - const messageBus = container.lookup("service:message-bus"); const siteSettings = container.lookup("service:site-settings"); if (!siteSettings.custom_wizard_enabled) { return; } - messageBus.subscribe("/redirect_to_wizard", function (wizardId) { - const wizardUrl = window.location.origin + "/w/" + wizardId; - window.location.href = wizardUrl; - }); + const existing = DiscourseURL.routeTo; + DiscourseURL.routeTo = function (path, opts) { + if (path && path.indexOf("/w/") > -1) { + return (window.location = path); + } + return existing.apply(this, [path, opts]); + }; withPluginApi("0.8.36", (api) => { - api.onAppEvent("page:changed", (data) => { - const currentUser = container.lookup("service:current-user"); - const settings = container.lookup("service:site-settings"); - if (currentUser) { - const redirectToWizard = currentUser.redirect_to_wizard; - const excludedPaths = settings.wizard_redirect_exclude_paths - .split("|") - .concat(["loading"]); - if ( - redirectToWizard && - data.currentRouteName !== "customWizardStep" && - !excludedPaths.find((p) => { - return data.currentRouteName.indexOf(p) > -1; - }) - ) { - DiscourseURL.routeTo(`/w/${dasherize(redirectToWizard)}`); - } - } - }); - api.modifyClass("component:d-navigation", { pluginId: "custom-wizard", actions: { diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 new file mode 100644 index 00000000..cfac123f --- /dev/null +++ b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 @@ -0,0 +1,42 @@ +import DiscourseURL from "discourse/lib/url"; +import { withPluginApi } from "discourse/lib/plugin-api"; +import { dasherize } from "@ember/string"; + +export default { + name: "custom-wizard-redirect", + initialize(container) { + const messageBus = container.lookup("service:message-bus"); + const siteSettings = container.lookup("service:site-settings"); + + if (!siteSettings.custom_wizard_enabled) { + return; + } + + messageBus.subscribe("/redirect_to_wizard", function (wizardId) { + const wizardUrl = window.location.origin + "/w/" + wizardId; + window.location.href = wizardUrl; + }); + + withPluginApi("0.8.36", (api) => { + api.onAppEvent("page:changed", (data) => { + const currentUser = container.lookup("service:current-user"); + + if (currentUser) { + const redirectToWizard = currentUser.redirect_to_wizard; + const excludedPaths = siteSettings.wizard_redirect_exclude_paths + .split("|") + .concat(["loading"]); + if ( + redirectToWizard && + data.currentRouteName !== "customWizardStep" && + !excludedPaths.find((p) => { + return data.currentRouteName.indexOf(p) > -1; + }) + ) { + DiscourseURL.routeTo(`/w/${dasherize(redirectToWizard)}`); + } + } + }); + }); + }, +}; From 5aefa9c544859f81c2259fac0a2db00fbfd75b9e Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Wed, 12 Jul 2023 12:53:20 +0100 Subject: [PATCH 12/14] restore after directive --- .../discourse/initializers/custom-wizard-redirect.js.es6 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 index cfac123f..e2984b36 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 @@ -4,6 +4,8 @@ import { dasherize } from "@ember/string"; export default { name: "custom-wizard-redirect", + after: "message-bus", + initialize(container) { const messageBus = container.lookup("service:message-bus"); const siteSettings = container.lookup("service:site-settings"); From f94df66613d3231de0a26a57915dd4773e5c0ed8 Mon Sep 17 00:00:00 2001 From: merefield Date: Thu, 13 Jul 2023 16:04:31 +0100 Subject: [PATCH 13/14] move current user determination to api & remove redundant line --- .../discourse/initializers/custom-wizard-redirect.js.es6 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 index e2984b36..e89a7259 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 @@ -21,7 +21,7 @@ export default { withPluginApi("0.8.36", (api) => { api.onAppEvent("page:changed", (data) => { - const currentUser = container.lookup("service:current-user"); + const currentUser = api.getCurrentUser(); if (currentUser) { const redirectToWizard = currentUser.redirect_to_wizard; @@ -30,7 +30,6 @@ export default { .concat(["loading"]); if ( redirectToWizard && - data.currentRouteName !== "customWizardStep" && !excludedPaths.find((p) => { return data.currentRouteName.indexOf(p) > -1; }) From a13f7be6a3d29b72f786f0ccb1a0c71a7c07eade Mon Sep 17 00:00:00 2001 From: merefield Date: Thu, 13 Jul 2023 16:07:36 +0100 Subject: [PATCH 14/14] revert remove redundant line as not redundant --- .../discourse/initializers/custom-wizard-redirect.js.es6 | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 index e89a7259..70676bb0 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 @@ -30,6 +30,7 @@ export default { .concat(["loading"]); if ( redirectToWizard && + data.currentRouteName !== "customWizardStep" && !excludedPaths.find((p) => { return data.currentRouteName.indexOf(p) > -1; })