From 1ab2262ca341aea4b45600f54acca902619dc9e4 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Feb 2024 09:22:28 +0200 Subject: [PATCH 1/5] remove unused warning system --- .../components/custom-wizard-step.js.es6 | 20 +------------------ .../models/custom-wizard-field.js.es6 | 1 - .../models/custom-wizard-step.js.es6 | 7 ------- 3 files changed, 1 insertion(+), 27 deletions(-) diff --git a/assets/javascripts/discourse/components/custom-wizard-step.js.es6 b/assets/javascripts/discourse/components/custom-wizard-step.js.es6 index 250f9140..cb34cd68 100644 --- a/assets/javascripts/discourse/components/custom-wizard-step.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-step.js.es6 @@ -197,25 +197,7 @@ export default Component.extend({ return; } - const step = this.step; - const result = step.validate(); - - if (result.warnings.length) { - const unwarned = result.warnings.filter((w) => !alreadyWarned[w]); - if (unwarned.length) { - unwarned.forEach((w) => (alreadyWarned[w] = true)); - return window.bootbox.confirm( - unwarned.map((w) => I18n.t(`wizard.${w}`)).join("\n"), - I18n.t("no_value"), - I18n.t("yes_value"), - (confirmed) => { - if (confirmed) { - this.advance(); - } - } - ); - } - } + this.step.validate(); if (step.get("valid")) { this.advance(); diff --git a/assets/javascripts/discourse/models/custom-wizard-field.js.es6 b/assets/javascripts/discourse/models/custom-wizard-field.js.es6 index 2afe79d9..9dbbb8f4 100644 --- a/assets/javascripts/discourse/models/custom-wizard-field.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-field.js.es6 @@ -25,7 +25,6 @@ export default EmberObject.extend(ValidState, { type: null, value: null, required: null, - warning: null, @discourseComputed("wizardId", "stepId", "id") i18nKey(wizardId, stepId, id) { diff --git a/assets/javascripts/discourse/models/custom-wizard-step.js.es6 b/assets/javascripts/discourse/models/custom-wizard-step.js.es6 index f7cdc497..5c3ce3ab 100644 --- a/assets/javascripts/discourse/models/custom-wizard-step.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-step.js.es6 @@ -35,19 +35,12 @@ export default EmberObject.extend(ValidState, { validate() { let allValid = true; - const result = { warnings: [] }; this.fields.forEach((field) => { allValid = allValid && field.check(); - const warning = field.get("warning"); - if (warning) { - result.warnings.push(warning); - } }); this.setValid(allValid); - - return result; }, fieldError(id, description) { From ca9e96ccf52b240b9b32394e25af914f0321ea56 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Feb 2024 11:33:42 +0200 Subject: [PATCH 2/5] Update resume on revisit dialog and add test --- .../discourse/models/custom-wizard.js.es6 | 5 +- .../discourse/routes/custom-wizard.js.es6 | 12 ++-- test/javascripts/acceptance/wizard-test.js | 55 ++++++++++++++++++- test/javascripts/helpers/wizard.js | 5 ++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index 77f439c7..bdb8dceb 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -5,6 +5,7 @@ import discourseComputed from "discourse-common/utils/decorators"; import getUrl from "discourse-common/lib/get-url"; import CustomWizardField from "./custom-wizard-field"; import CustomWizardStep from "./custom-wizard-step"; +import DiscourseURL from "discourse/lib/url"; const CustomWizard = EmberObject.extend({ @discourseComputed("steps.length") @@ -34,7 +35,7 @@ CustomWizard.reopenClass({ restart(wizardId) { ajax({ url: `/w/${wizardId}/skip`, type: "PUT" }) .then(() => { - window.location.href = `/w/${wizardId}`; + DiscourseURL.redirectTo(getUrl(`/w/${wizardId}`)); }) .catch(popupAjaxError); }, @@ -44,7 +45,7 @@ CustomWizard.reopenClass({ if (result.redirect_on_complete) { url = result.redirect_on_complete; } - window.location.href = getUrl(url); + DiscourseURL.redirectTo(getUrl(url)); }, build(wizardJson) { diff --git a/assets/javascripts/discourse/routes/custom-wizard.js.es6 b/assets/javascripts/discourse/routes/custom-wizard.js.es6 index 1a214a2d..a7c2c7ea 100644 --- a/assets/javascripts/discourse/routes/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/custom-wizard.js.es6 @@ -1,9 +1,11 @@ import { findCustomWizard, updateCachedWizard } from "../models/custom-wizard"; import I18n from "I18n"; import DiscourseRoute from "discourse/routes/discourse"; -import bootbox from "bootbox"; +import { inject as service } from "@ember/service"; export default DiscourseRoute.extend({ + dialog: service(), + titleToken() { const wizard = this.modelFor("custom-wizard"); return wizard ? wizard.name || wizard.id : I18n.t("wizard.custom_title"); @@ -30,7 +32,7 @@ export default DiscourseRoute.extend({ { label: I18n.t("wizard.incomplete_submission.restart"), class: "btn btn-default", - callback: () => { + action: () => { wizardModel.restart(); }, }, @@ -40,11 +42,7 @@ export default DiscourseRoute.extend({ }, ]; - const options = { - onEscape: false, - }; - - bootbox.dialog(title, buttons, options); + this.dialog.dialog({ title, buttons, type: 'confirm' }); }, afterModel(model) { diff --git a/test/javascripts/acceptance/wizard-test.js b/test/javascripts/acceptance/wizard-test.js index 0527cf0f..5e7281a1 100644 --- a/test/javascripts/acceptance/wizard-test.js +++ b/test/javascripts/acceptance/wizard-test.js @@ -1,4 +1,4 @@ -import { visit } from "@ember/test-helpers"; +import { click, visit } from "@ember/test-helpers"; import { test } from "qunit"; import { acceptance, @@ -12,9 +12,12 @@ import { wizardGuest, wizardNoUser, wizardNotPermitted, + wizardResumeOnRevisit, } from "../helpers/wizard"; import DiscourseURL from "discourse/lib/url"; import sinon from "sinon"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; +import I18n from "I18n"; acceptance("Wizard | Not logged in", function (needs) { needs.pretender((server, helper) => { @@ -194,3 +197,53 @@ acceptance("Wizard | Guest access", function (needs) { assert.strictEqual($("body.custom-wizard").length, 0); }); }); + +acceptance("Wizard | Resume on revisit", function (needs) { + needs.user(); + + test("Shows dialog", async function (assert) { + pretender.get("/w/wizard.json", () => { + return response(wizardResumeOnRevisit); + }); + + await visit("/w/wizard"); + + assert.strictEqual(count(".dialog-content:visible"), 1); + assert.strictEqual( + query(".dialog-header h3").textContent.trim(), + I18n.t("wizard.incomplete_submission.title", { + date: moment(wizardResumeOnRevisit.submission_last_updated_at).format( + "MMMM Do YYYY" + ), + }) + ); + }); + + test("Resumes when resumed", async function (assert) { + pretender.get("/w/wizard.json", () => { + return response(wizardResumeOnRevisit); + }); + await visit("/w/wizard"); + await click(".dialog-footer .btn-primary"); + assert.strictEqual(count(".dialog-content:visible"), 0); + }); + + test("Restarts when restarted", async function (assert) { + sinon.stub(DiscourseURL, "redirectTo"); + let skips = 0; + pretender.get("/w/wizard.json", () => { + return response(wizardResumeOnRevisit); + }); + pretender.put("/w/wizard/skip", () => { + skips++; + return response({}); + }); + await visit("/w/wizard"); + await click(".dialog-footer .btn-default"); + assert.strictEqual(skips, 1); + assert.ok( + DiscourseURL.redirectTo.calledWith("/w/wizard"), + "resuming wizard works" + ); + }); +}); diff --git a/test/javascripts/helpers/wizard.js b/test/javascripts/helpers/wizard.js index e02e2e99..ae672051 100644 --- a/test/javascripts/helpers/wizard.js +++ b/test/javascripts/helpers/wizard.js @@ -23,6 +23,10 @@ wizard.resume_on_revisit = false; wizard.submission_last_updated_at = "2022-03-11T20:00:18+01:00"; wizard.subscribed = false; +const wizardResumeOnRevisit = cloneJSON(wizard); +wizardResumeOnRevisit.start = "step_2"; +wizardResumeOnRevisit.resume_on_revisit = true; + const stepNotPermitted = cloneJSON(wizard); stepNotPermitted.steps[0].permitted = false; @@ -44,6 +48,7 @@ export { wizardNotPermitted, wizardCompleted, wizardGuest, + wizardResumeOnRevisit, stepNotPermitted, allFieldsWizard, wizard, From e5359aa6496d7cb778df4b156bd2089fab90cec1 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Feb 2024 11:34:18 +0200 Subject: [PATCH 3/5] Bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 708799ce..a3c510b1 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.6.1 +# version: 2.6.2 # 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 325feb78cc94fbc59bd00bbea9274d08e672b2ac Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Feb 2024 11:39:12 +0200 Subject: [PATCH 4/5] Minor fixes --- .../discourse/components/custom-wizard-step.js.es6 | 5 +---- assets/javascripts/discourse/routes/custom-wizard.js.es6 | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/discourse/components/custom-wizard-step.js.es6 b/assets/javascripts/discourse/components/custom-wizard-step.js.es6 index cb34cd68..44bef872 100644 --- a/assets/javascripts/discourse/components/custom-wizard-step.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-step.js.es6 @@ -1,6 +1,5 @@ import discourseComputed, { observes } from "discourse-common/utils/decorators"; import Component from "@ember/component"; -import I18n from "I18n"; import getUrl from "discourse-common/lib/get-url"; import { htmlSafe } from "@ember/template"; import { schedule } from "@ember/runloop"; @@ -11,8 +10,6 @@ import CustomWizard, { import { alias, not } from "@ember/object/computed"; import discourseLater from "discourse-common/lib/later"; -const alreadyWarned = {}; - export default Component.extend({ classNameBindings: [":wizard-step", "step.id"], saving: null, @@ -199,7 +196,7 @@ export default Component.extend({ this.step.validate(); - if (step.get("valid")) { + if (this.step.get("valid")) { this.advance(); } else { this.autoFocus(); diff --git a/assets/javascripts/discourse/routes/custom-wizard.js.es6 b/assets/javascripts/discourse/routes/custom-wizard.js.es6 index a7c2c7ea..e7871423 100644 --- a/assets/javascripts/discourse/routes/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/custom-wizard.js.es6 @@ -42,7 +42,7 @@ export default DiscourseRoute.extend({ }, ]; - this.dialog.dialog({ title, buttons, type: 'confirm' }); + this.dialog.dialog({ title, buttons, type: "confirm" }); }, afterModel(model) { From 996f953ac5ad0c152d11a29916b6f1b58760b7e0 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Feb 2024 12:02:04 +0200 Subject: [PATCH 5/5] Bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index c4bd4000..3e1814a9 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.6.2 +# version: 2.6.3 # 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