A platform for community discussion. Free, open, simple.
DEV: Keep category form values in their server shapes (#45052)
Sérgio Saquetimd8c6bae231fd
diff --git a/frontend/discourse/admin/components/upsert-category/appearance.gjs b/frontend/discourse/admin/components/upsert-category/appearance.gjsindex a0610826d14..fb47bd11b70 100644--- a/frontend/discourse/admin/components/upsert-category/appearance.gjs+++ b/frontend/discourse/admin/components/upsert-category/appearance.gjs@@ -128,6 +128,12 @@ export default class UpsertCategoryAppearance extends Component {];}+ /** Converts the native select's string value back to the boolean option value. */+ @action+ async onSortAscendingSet(value, { name, set }) {+ await set(name, value === null ? null : value === "true");+ }+@actionasync onSortOrderSet(value, { name, set }) {await set(name, value);@@ -269,6 +275,7 @@ export default class UpsertCategoryAppearance extends Component {<@form.Field@format="max"@name="sort_ascending"+ @onSet={{this.onSortAscendingSet}}@title={{i18n "category.sort_direction"}}@type="select"as |field|diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjsindex a8724038d17..15bf878fae7 100644--- a/frontend/discourse/admin/components/upsert-category/general.gjs+++ b/frontend/discourse/admin/components/upsert-category/general.gjs@@ -626,7 +626,7 @@ export default class UpsertCategoryGeneral extends Component {{{#if (or this.isEditingExistingCategory @showAdvancedTabs)}}<@form.Field@format="max"- @name="category_types"+ @name="category_type_ids"@showOptional={{false}}@title={{i18n "category.category_types"}}@type="custom"diff --git a/frontend/discourse/admin/components/upsert-category/settings.gjs b/frontend/discourse/admin/components/upsert-category/settings.gjsindex 603310e1333..1c2edd2ee34 100644--- a/frontend/discourse/admin/components/upsert-category/settings.gjs+++ b/frontend/discourse/admin/components/upsert-category/settings.gjs@@ -1,4 +1,5 @@import Component from "@glimmer/component";+import { action } from "@ember/object";import { service } from "@ember/service";import { trustHTML } from "@ember/template";import PluginOutlet from "discourse/components/plugin-outlet";@@ -33,6 +34,12 @@ export default class UpsertCategorySettings extends Component {return options;}+ /** Converts the native select's string value back to the numeric priority. */+ @action+ async onSearchPrioritySet(value, { name, set }) {+ await set(name, value === null ? null : parseInt(value, 10));+ }+<template><@form.Field@format="max"@@ -75,6 +82,7 @@ export default class UpsertCategorySettings extends Component {<@form.Field@format="max"@name="search_priority"+ @onSet={{this.onSearchPrioritySet}}@title={{i18n "category.search_priority.label"}}@type="select"@validation="required"diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.jsindex a47e3a5c78e..77dba468cee 100644--- a/frontend/discourse/admin/controllers/edit-category/tabs.js+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js@@ -202,10 +202,10 @@ export default class EditCategoryTabsController extends Controller {...(this.model.category_type_settings ?? {}),};data.site_texts = {};- data.category_types = Object.keys(this.model.categoryTypes ?? {});+ data.category_type_ids = Object.keys(this.model.categoryTypes ?? {});- if (!data.category_types.includes(DISCUSSION_TYPE_ID)) {- data.category_types.unshift(DISCUSSION_TYPE_ID);+ if (!data.category_type_ids.includes(DISCUSSION_TYPE_ID)) {+ data.category_type_ids.unshift(DISCUSSION_TYPE_ID);}Object.values(this.model.categoryTypes ?? {}).forEach((categoryType) => {@@ -368,7 +368,7 @@ export default class EditCategoryTabsController extends Controller {}// eslint-disable-next-line no-unused-vars- const { visibility, ...categoryData } = data;+ const { visibility, category_type_ids, ...categoryData } = data;this.model.setProperties(categoryData);// Category serialization converts an empty permission list to staff-only access.@@ -400,7 +400,7 @@ export default class EditCategoryTabsController extends Controller {const previousTypes = new Set(Object.keys(this.model.categoryTypes ?? {}));- const result = await this.model.save();+ const result = await this.model.save(category_type_ids);const siteTextsChanged = await this._saveSiteTexts();const updatedModel = this.site.updateCategory(result.category);updatedModel.setupGroupsAndPermissions();diff --git a/frontend/discourse/app/models/category.js b/frontend/discourse/app/models/category.jsindex c614983d398..c83a8678ea2 100644--- a/frontend/discourse/app/models/category.js+++ b/frontend/discourse/app/models/category.js@@ -812,7 +812,11 @@ export default class Category extends RestModel {}}- save() {+ /**+ * @param {string[]} [categoryTypeIds] - ids of the category's types; when+ * omitted, the server leaves the category's types unchanged.+ */+ save(categoryTypeIds) {const id = this.id;const url = id ? `/categories/${id}` : "/categories";@@ -880,9 +884,9 @@ export default class Category extends RestModel {locale: this.locale,category_localizations: this.localizations,}),- ...this._categoryTypeSaveProperties(id),+ ...this._categoryTypeSaveProperties(id, categoryTypeIds),...this._pluginSaveProperties(),- category_types: this.category_types,+ category_types: categoryTypeIds,}),type: id ? "PUT" : "POST",});@@ -953,7 +957,7 @@ export default class Category extends RestModel {);}- _categoryTypeSaveProperties(id) {+ _categoryTypeSaveProperties(id, categoryTypeIds) {const props = {category_type_site_settings: this.category_type_site_settings,category_type_settings: this.category_type_settings,@@ -961,7 +965,7 @@ export default class Category extends RestModel {if (!id && this.categoryTypes) {const primaryType = Object.keys(this.categoryTypes)[0];- if (this.category_types?.includes(primaryType)) {+ if (categoryTypeIds?.includes(primaryType)) {props.category_type = primaryType;}}diff --git a/frontend/discourse/tests/acceptance/category-edit-test.js b/frontend/discourse/tests/acceptance/category-edit-test.jsindex ca3da9d2e03..2f4d92c5454 100644--- a/frontend/discourse/tests/acceptance/category-edit-test.js+++ b/frontend/discourse/tests/acceptance/category-edit-test.js@@ -655,3 +655,130 @@ acceptance("Category Edit - emoji disabled", function (needs) {.isChecked("emoji category resolves to square");});});++acceptance("Category Edit - saved value types", function (needs) {+ needs.user();++ const discussionType = {+ id: "discussion",+ name: "Discussion",+ configuration_schema: {},+ };++ let seed, failSave;++ needs.hooks.beforeEach(() => {+ seed = {};+ failSave = false;+ });++ needs.pretender((server, helper) => {+ server.get("/c/bug/find_by_slug.json", () =>+ helper.response(200, {+ category: seededCategory({+ sort_order: "views",+ category_types: { discussion: discussionType },+ ...seed,+ }),+ })+ );++ server.put("/categories/:id", () => {+ if (failSave) {+ return helper.response(422, { errors: ["Something went wrong"] });+ }+ return helper.response(200, {+ success: "OK",+ category: seededCategory({+ sort_order: "views",+ category_types: { discussion: discussionType },+ }),+ });+ });+ });++ test("saves the sort direction as a boolean", async function (assert) {+ seed = { sort_ascending: false };++ await visit("/c/bug/edit/images");+ await formKit().field("sort_ascending").select("true");+ await click(".admin-changes-banner .btn-primary");++ assert.true(+ latestCategorySavePayload().sort_ascending,+ "sends true, not the string the select reports"+ );+ assert.dom(".dialog-body").doesNotExist("the save succeeds");+ });++ test("saves a descending sort direction as false", async function (assert) {+ seed = { sort_ascending: true };++ await visit("/c/bug/edit/images");+ await formKit().field("sort_ascending").select("false");+ await click(".admin-changes-banner .btn-primary");++ assert.false(+ latestCategorySavePayload().sort_ascending,+ "sends false, not the string the select reports"+ );+ assert.dom(".dialog-body").doesNotExist("the save succeeds");+ });++ test("saves the search priority as a number", async function (assert) {+ seed = { search_priority: 0 };++ await visit("/c/bug/edit/settings");+ await formKit().field("search_priority").select(1);+ await click(".admin-changes-banner .btn-primary");++ assert.strictEqual(+ latestCategorySavePayload().search_priority,+ 1,+ "sends the number, not the string the select reports"+ );+ assert.dom(".dialog-body").doesNotExist("the save succeeds");+ });++ test("saves a zero search priority as 0", async function (assert) {+ seed = { search_priority: 2 };++ await visit("/c/bug/edit/settings");+ await formKit().field("search_priority").select(0);+ await click(".admin-changes-banner .btn-primary");++ assert.strictEqual(+ latestCategorySavePayload().search_priority,+ 0,+ "sends 0, not the string the select reports"+ );+ assert.dom(".dialog-body").doesNotExist("the save succeeds");+ });++ test("keeps the category's types when the save fails", async function (assert) {+ failSave = true;++ await visit("/c/bug/edit/general");+ const model = this.owner.lookup("controller:edit-category/tabs").model;+ const typesBefore = cloneJSON(model.category_types);++ await fillIn("input.category-name", "renamed");+ await click(".admin-changes-banner .btn-primary");++ assert.deepEqual(+ latestCategorySavePayload().category_types,+ ["discussion"],+ "still sends the selected type ids"+ );+ assert.deepEqual(+ typesBefore,+ { discussion: discussionType },+ "the category starts with its type map"+ );+ assert.deepEqual(+ cloneJSON(model.category_types),+ typesBefore,+ "the category keeps its type map instead of the request's id list"+ );+ });+});