A platform for community discussion. Free, open, simple.
FIX: Keep the category save button busy while redirecting to the new category (#43955)
Régis Hanol06674ff3ce16
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.jsindex 7f9fe9989dd..23ce3cf1c61 100644--- a/frontend/discourse/admin/controllers/edit-category/tabs.js+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js@@ -1,6 +1,6 @@import { tracked } from "@glimmer/tracking";import Controller from "@ember/controller";-import { action, computed, getProperties } from "@ember/object";+import { action, getProperties } from "@ember/object";import { next } from "@ember/runloop";import { service } from "@ember/service";import { ajax } from "discourse/lib/ajax";@@ -10,7 +10,7 @@ import { registeredEditCategoryTabs } from "discourse/lib/edit-category-tabs";import getURL from "discourse/lib/get-url";import { homepageNavigationDestination } from "discourse/lib/homepage-router-overrides";import { autoTrackedArray } from "discourse/lib/tracked-tools";-import DiscourseURL from "discourse/lib/url";+import DiscourseURL, { getEditCategoryUrl } from "discourse/lib/url";import Category from "discourse/models/category";import { i18n } from "discourse-i18n";@@ -78,7 +78,7 @@ export default class EditCategoryTabsController extends Controller {@service keyValueStore;@service toasts;- @tracked breadcrumbCategories = this.site.get("categoriesList");+ @tracked breadcrumbCategories = this.site.categoriesList;@trackedshowAdvancedTabs =this.keyValueStore.getItem(SHOW_ADVANCED_TABS_KEY) === "true";@@ -93,11 +93,9 @@ export default class EditCategoryTabsController extends Controller {saving = false;deleting = false;showTooltip = false;- createdCategory = false;expandedMenu = false;parentParams = null;validators = [];- textColors = ["000000", "FFFFFF"];/*** Callbacks registered by tab components that are invoked when the form@@ -106,34 +104,10 @@ export default class EditCategoryTabsController extends Controller {*/afterResetCallbacks = [];- @computed("showTooltip", "model.cannot_delete_reason")- get showDeleteReason() {- return this.showTooltip && this.model?.cannot_delete_reason;- }-get availableLocales() {return this.siteSettings.available_locales;}- @computed("saving", "deleting")- get deleteDisabled() {- return this.deleting || this.saving || false;- }-- @computed("name")- get categoryName() {- const name = this.name || "";- return name.trim().length > 0 ? name : i18n("preview");- }-- @computed("saving", "model.id")- get saveLabel() {- if (this.saving) {- return "saving";- }- return this.model?.id ? "category.save" : "category.create_category";- }-get baseTitle() {if (this.model.id) {return i18n("category.edit_dialog_title", {@@ -182,13 +156,9 @@ export default class EditCategoryTabsController extends Controller {}get _siteTextEntries() {- const entries = [];- Object.values(this.model?.categoryTypes ?? {}).forEach((categoryType) => {- categoryType.configuration_schema.site_texts?.forEach((entry) =>- entries.push(entry)- );- });- return entries;+ return Object.values(this.model?.categoryTypes ?? {}).flatMap(+ (categoryType) => categoryType.configuration_schema.site_texts ?? []+ );}@action@@ -382,7 +352,7 @@ export default class EditCategoryTabsController extends Controller {this.model.setProperties(categoryData);// If permissions is empty or not set, ensure it's an empty array (public category)- if (!this.model.permissions || this.model.permissions.length === 0) {+ if (!this.model.permissions?.length) {this.model.set("permissions", []);}@@ -421,12 +391,11 @@ export default class EditCategoryTabsController extends Controller {if (this.model.id) {window.location.reload();} else {- window.location = this.router.urlFor(- "editCategory",- Category.slugFor(updatedModel)- );+ DiscourseURL.redirectAbsolute(getEditCategoryUrl(updatedModel));}- return;++ // Never resolves: keeps the form busy until the page unloads+ return new Promise(() => {});}this.set("saving", false);@@ -492,7 +461,7 @@ export default class EditCategoryTabsController extends Controller {@actiondeleteCategory() {- if (this.deleteDisabled) {+ if (this.deleting || this.saving) {return;}@@ -516,20 +485,6 @@ export default class EditCategoryTabsController extends Controller {});}- @action- toggleDeleteTooltip() {- if (this.deleteDisabled) {- return;- }-- this.toggleProperty("showTooltip");- }-- @action- goBack() {- DiscourseURL.routeTo(this.model.url);- }-@actiontoggleAdvancedTabs() {this.showAdvancedTabs = !this.showAdvancedTabs;@@ -541,31 +496,35 @@ export default class EditCategoryTabsController extends Controller {);// When collapsing, reset to general unless current tab is still visible- if (!this.showAdvancedTabs && this.selectedTab !== "general") {- const primaryTab = registeredEditCategoryTabs.find(- (tab) => tab.id === this.selectedTab && tab.primary- );- if (!primaryTab) {- next(() => {- this.selectedTab = "general";- if (this.router.currentRouteName?.startsWith("newCategory")) {- DiscourseURL.routeTo(getURL("/new-category/general"));- } else if (this.parentParams?.slug) {- DiscourseURL.routeTo(- getURL(`/c/${this.parentParams.slug}/edit/general`)- );- }- });- }+ if (this.showAdvancedTabs || this.selectedTab === "general") {+ return;}++ const primaryTab = registeredEditCategoryTabs.find(+ (tab) => tab.id === this.selectedTab && tab.primary+ );+ if (primaryTab) {+ return;+ }++ next(() => {+ this.selectedTab = "general";+ if (this.router.currentRouteName?.startsWith("newCategory")) {+ DiscourseURL.routeTo(getURL("/new-category/general"));+ } else if (this.parentParams?.slug) {+ DiscourseURL.routeTo(+ getURL(`/c/${this.parentParams.slug}/edit/general`)+ );+ }+ });}- _wouldLoseAccess(category = this.model) {+ _wouldLoseAccess() {if (this.currentUser.admin) {return false;}- const permissions = category.permissions;+ const permissions = this.model.permissions;if (!permissions?.length) {return false;}diff --git a/frontend/discourse/tests/acceptance/category-new-test.js b/frontend/discourse/tests/acceptance/category-new-test.jsindex e66cf3c9dcc..edf7741c296 100644--- a/frontend/discourse/tests/acceptance/category-new-test.js+++ b/frontend/discourse/tests/acceptance/category-new-test.js@@ -1,10 +1,13 @@-import { click, currentURL, fillIn, visit } from "@ember/test-helpers";+import { click, currentURL, fillIn, settled, visit } from "@ember/test-helpers";import { test } from "qunit";import sinon from "sinon";import { CATEGORY_TEXT_COLORS } from "discourse/lib/constants";import { cloneJSON } from "discourse/lib/object";import DiscourseURL from "discourse/lib/url";-import { fixturesByUrl } from "discourse/tests/helpers/create-pretender";+import pretender, {+ fixturesByUrl,+ response,+} from "discourse/tests/helpers/create-pretender";import formKit from "discourse/tests/helpers/form-kit-helper";import {acceptance,@@ -52,9 +55,7 @@ acceptance("Category New", function (needs) {await fillIn("input.category-name", "testing");assert.dom(".badge-category").hasText("testing");- if (!document.querySelector(".edit-category-topic-template")) {- await click(".category-show-advanced-tabs-toggle");- }+ await click(".category-show-advanced-tabs-toggle");assert.dom(".edit-category-topic-template")@@ -98,8 +99,28 @@ acceptance("Category New", function (needs) {.form().field("search_priority").exists("it can switch to the settings tab");+ });++ test("Keeps the save button busy while redirecting to the edit page", async function (assert) {+ const category = cloneJSON(fixturesByUrl["/c/11/show.json"]).category;+ category.category_types.support = category.available_category_types[0];+ pretender.post("/categories", () => response({ category }));+ const redirectAbsolute = sinon.stub(DiscourseURL, "redirectAbsolute");++ await visit("/new-category");+ await fillIn("input.category-name", "testing");+ await click("#save-category");+ document.querySelector("#save-category").click();+ await settled();- sinon.stub(DiscourseURL, "routeTo");+ assert.true(redirectAbsolute.calledWith("/c/testing/edit"));+ assert.dom("#save-category").isDisabled();+ assert.strictEqual(+ pretender.handledRequests.filter(+ ({ method, url }) => method === "POST" && url === "/categories"+ ).length,+ 1+ );});test("Specifying a parent category", async function (assert) {@@ -151,19 +172,12 @@ acceptance("Category type setup page", function (needs) {});});- test("Visiting /new-category redirects to setup page", async function (assert) {+ test("Picking a type card on the setup page opens the new category form", async function (assert) {await visit("/new-category");assert.strictEqual(currentURL(), "/new-category/setup");- });-- test("Setup page shows type cards", async function (assert) {- await visit("/new-category/setup");assert.dom(".category-type-cards__card").exists({ count: 2 });assert.dom(".category-type-cards__card-name").exists();- });- test("Clicking a type card transitions to new category form", async function (assert) {- await visit("/new-category/setup");await click(".category-type-cards__card:first-child .category-type-cards__card-select");@@ -171,27 +185,23 @@ acceptance("Category type setup page", function (needs) {});});-acceptance("Category text color", function (needs) {+acceptance("Category color", function (needs) {needs.user({ can_create_category: true });- needs.pretender((server, helper) => {- const category = cloneJSON(fixturesByUrl["/c/11/show.json"]).category;- server.get("/c/testing/find_by_slug.json", () => {- return helper.response(200, {- category: {- ...category,- color: "EEEEEE",- text_color: "000000",- },- });- });- });+ function previewBadgeColor() {+ return document+ .querySelector(".edit-category-tab-general .badge-category")+ .style.getPropertyValue("--category-badge-color")+ .trim();+ }- test("Category text color is set based on contrast", async function (assert) {+ test("Badge preview follows the color and text color follows its contrast", async function (assert) {await visit("/new-category");+ await click(".form-kit__control-radio[value='square']");+ assert.strictEqual(previewBadgeColor(), "#0088CC");+await click(".category-show-advanced-tabs-toggle");await click(".edit-category-images a");-assert.strictEqual(formKit().field("text_color").value(),CATEGORY_TEXT_COLORS[0],@@ -199,10 +209,10 @@ acceptance("Category text color", function (needs) {);await click(".edit-category-general a");- await fillIn("input.category-name", "testing");await formKit().field("color").fillIn("EEEEEE");- await click(".edit-category-images a");+ assert.strictEqual(previewBadgeColor(), "#EEEEEE");+ await click(".edit-category-images a");assert.strictEqual(formKit().field("text_color").value(),CATEGORY_TEXT_COLORS[1],@@ -210,28 +220,3 @@ acceptance("Category text color", function (needs) {);});});--acceptance("New category preview", function (needs) {- needs.user({ admin: true, can_create_category: true });-- test("Category badge color appears and updates", async function (assert) {- await visit("/new-category");- await click(".form-kit__control-radio[value='square']");-- let previewBadgeColor = document- .querySelector(".edit-category-tab-general .badge-category")- .style.getPropertyValue("--category-badge-color")- .trim();-- assert.strictEqual(previewBadgeColor, "#0088CC");-- await formKit().field("color").fillIn("FF00FF");-- previewBadgeColor = document- .querySelector(".edit-category-tab-general .badge-category")- .style.getPropertyValue("--category-badge-color")- .trim();-- assert.strictEqual(previewBadgeColor, "#FF00FF");- });-});