diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 index 1438f0d6..70676bb0 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 @@ -1,12 +1,16 @@ +import DiscourseURL from "discourse/lib/url"; +import { withPluginApi } from "discourse/lib/plugin-api"; +import { dasherize } from "@ember/string"; + export default { name: "custom-wizard-redirect", after: "message-bus", - initialize: function (container) { + initialize(container) { const messageBus = container.lookup("service:message-bus"); const siteSettings = container.lookup("service:site-settings"); - if (!siteSettings.custom_wizard_enabled || !messageBus) { + if (!siteSettings.custom_wizard_enabled) { return; } @@ -15,30 +19,26 @@ export default { window.location.href = wizardUrl; }); - const ApplicationRoute = requirejs("discourse/routes/application").default; + withPluginApi("0.8.36", (api) => { + api.onAppEvent("page:changed", (data) => { + const currentUser = api.getCurrentUser(); - ApplicationRoute.reopen({ - actions: { - willTransition(transition) { - const redirectToWizard = this.get("currentUser.redirect_to_wizard"); - const excludedPaths = this.siteSettings.wizard_redirect_exclude_paths + if (currentUser) { + const redirectToWizard = currentUser.redirect_to_wizard; + const excludedPaths = siteSettings.wizard_redirect_exclude_paths .split("|") .concat(["loading"]); - if ( redirectToWizard && - (!transition.intent.name || - !excludedPaths.find((p) => { - return transition.intent.name.indexOf(p) > -1; - })) + data.currentRouteName !== "customWizardStep" && + !excludedPaths.find((p) => { + return data.currentRouteName.indexOf(p) > -1; + }) ) { - transition.abort(); - window.location = "/w/" + redirectToWizard.dasherize(); + DiscourseURL.routeTo(`/w/${dasherize(redirectToWizard)}`); } - - return this._super(transition); - }, - }, + } + }); }); }, }; 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 diff --git a/test/javascripts/acceptance/wizard-test.js b/test/javascripts/acceptance/wizard-test.js index e2e6ce04..dd441649 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) => {