Skip to content

A platform for community discussion. Free, open, simple.

FIX: ensure changes to topic template are tracked (#38538)

Kris83657d4b3cfb
diff --git a/frontend/discourse/admin/components/edit-category-topic-template.gjs b/frontend/discourse/admin/components/edit-category-topic-template.gjs
index b3b6809573c..e916f876e21 100644
--- a/frontend/discourse/admin/components/edit-category-topic-template.gjs
+++ b/frontend/discourse/admin/components/edit-category-topic-template.gjs
@@ -1,10 +1,14 @@
import { buildCategoryPanel } from "discourse/admin/components/edit-category-panel";
import CategoryTopicTemplateEditor from "discourse/components/category-topic-template-editor";
+import { or } from "discourse/truth-helpers";
export default class EditCategoryTopicTemplate extends buildCategoryPanel(
"topic-template"
) {
<template>
- <CategoryTopicTemplateEditor @category={{this.category}} />
+ <CategoryTopicTemplateEditor
+ @category={{or this.transientData this.category}}
+ @onChange={{this.form.set}}
+ />
</template>
}
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index 835fa4c53f1..f4c0b556bf8 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -29,6 +29,8 @@ const LEGACY_FORMKIT_FIELDS = [
"email_in",
"email_in_allow_strangers",
"mailinglist_mirror",
+ "topic_template",
+ "form_template_ids",
];
// All fields managed through FormKit in the simplified creation flow.
@@ -72,6 +74,8 @@ const SIMPLIFIED_FIELD_LIST = [
"minimum_required_tags",
"allow_global_tags",
"default_slow_mode_seconds",
+ "topic_template",
+ "form_template_ids",
];
const SHOW_ADVANCED_TABS_KEY = "category_edit_show_advanced_tabs";
@@ -129,7 +133,7 @@ export default class EditCategoryTabsController extends Controller {
data.category_type_site_settings = {};
- Object.values(this.model.category_types).forEach((categoryType) => {
+ Object.values(this.model.category_types ?? {}).forEach((categoryType) => {
categoryType.configuration_schema.category_custom_fields?.forEach(
(field) => {
data.custom_fields[field.key] ??= field.default;
diff --git a/frontend/discourse/app/components/category-topic-template-editor.gjs b/frontend/discourse/app/components/category-topic-template-editor.gjs
index 0f555db18c1..0cb76d523b7 100644
--- a/frontend/discourse/app/components/category-topic-template-editor.gjs
+++ b/frontend/discourse/app/components/category-topic-template-editor.gjs
@@ -14,6 +14,15 @@ export default class CategoryTopicTemplateEditor extends Component {
@service siteSettings;
@tracked _showFormTemplateOverride;
+ @tracked _localTopicTemplate;
+
+ get topicTemplate() {
+ return this._localTopicTemplate ?? this.args.category?.topic_template;
+ }
+
+ set topicTemplate(value) {
+ this._localTopicTemplate = value;
+ }
get showInsertLinkButton() {
if (this.args.showInsertLinkButton === undefined) {
@@ -26,9 +35,9 @@ export default class CategoryTopicTemplateEditor extends Component {
if (this._showFormTemplateOverride !== undefined) {
return this._showFormTemplateOverride;
}
- return Boolean(
- this.args.category && this.args.category.get("form_template_ids.length")
- );
+
+ const formTemplateIds = this.args.category?.form_template_ids;
+ return Boolean(formTemplateIds && formTemplateIds.length > 0);
}
set showFormTemplate(value) {
@@ -49,19 +58,31 @@ export default class CategoryTopicTemplateEditor extends Component {
if (!this.showFormTemplate) {
// Clear associated form templates if switching to freeform
- this.args.category.set("form_template_ids", []);
+ if (this.args.onChange) {
+ this.args.onChange("form_template_ids", []);
+ } else {
+ this.args.category.set("form_template_ids", []);
+ }
}
+ }
+ @action
+ handleFormTemplateChange(value) {
if (this.args.onChange) {
- this.args.onChange();
+ this.args.onChange("form_template_ids", value);
+ } else {
+ this.args.category.set("form_template_ids", value);
}
}
@action
- handleFormTemplateChange(value) {
- this.args.category.set("form_template_ids", value);
+ handleTopicTemplateChange(event) {
+ const value = event.target.value;
+ this.topicTemplate = value;
if (this.args.onChange) {
- this.args.onChange();
+ this.args.onChange("topic_template", value);
+ } else {
+ this.args.category.set("topic_template", value);
}
}
@@ -96,14 +117,16 @@ export default class CategoryTopicTemplateEditor extends Component {
</div>
{{else}}
<DEditor
- @value={{@category.topic_template}}
+ @value={{this.topicTemplate}}
@showLink={{this.showInsertLinkButton}}
+ @change={{this.handleTopicTemplateChange}}
/>
{{/if}}
{{else}}
<DEditor
- @value={{@category.topic_template}}
+ @value={{this.topicTemplate}}
@showLink={{this.showInsertLinkButton}}
+ @change={{this.handleTopicTemplateChange}}
/>
{{/if}}
</template>
diff --git a/frontend/discourse/select-kit/components/multi-select/format-selected-content.gjs b/frontend/discourse/select-kit/components/multi-select/format-selected-content.gjs
index a3e56a6f1e9..ab5af3d940b 100644
--- a/frontend/discourse/select-kit/components/multi-select/format-selected-content.gjs
+++ b/frontend/discourse/select-kit/components/multi-select/format-selected-content.gjs
@@ -15,7 +15,7 @@ export default class FormatSelectedContent extends Component {
get formattedContent() {
if (this.content) {
return makeArray(this.content)
- .map((c) => this.getName(c))
+ .map((c) => this.getName(c)?.trim())
.join(", ");
} else {
return this.getName(this.selectKit.noneItem);
diff --git a/frontend/discourse/tests/acceptance/edit-category-topic-template-test.js b/frontend/discourse/tests/acceptance/edit-category-topic-template-test.js
new file mode 100644
index 00000000000..950284e771f
--- /dev/null
+++ b/frontend/discourse/tests/acceptance/edit-category-topic-template-test.js
@@ -0,0 +1,161 @@
+import { click, fillIn, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
+
+acceptance(
+ "Admin - Edit category topic template with simplified category creation",
+ function (needs) {
+ let putData;
+
+ needs.user({ admin: true });
+ needs.settings({
+ enable_simplified_category_creation: true,
+ enable_form_templates: true,
+ });
+
+ needs.pretender((server, helper) => {
+ server.get("/form-templates.json", () => {
+ return helper.response(200, {
+ form_templates: [
+ { id: 1, name: "Template 1" },
+ { id: 2, name: "Template 2" },
+ ],
+ });
+ });
+
+ server.get("/c/bug/find_by_slug.json", () => {
+ return helper.response(200, {
+ category: {
+ id: 11,
+ name: "bug",
+ slug: "bug",
+ can_edit: true,
+ category_type_site_settings: {},
+ permissions: [],
+ },
+ });
+ });
+
+ server.put("/categories/:id", (request) => {
+ putData = JSON.parse(request.requestBody);
+ return helper.response(200, {
+ category: {
+ id: request.params.id,
+ name: "bug",
+ can_edit: true,
+ category_type_site_settings: {},
+ permissions: [],
+ ...putData,
+ },
+ });
+ });
+ });
+
+ test("Changing topic template shows the unsaved changes banner", async function (assert) {
+ putData = null;
+ await visit("/c/bug/edit/topic-template");
+
+ await fillIn(".d-editor-input", "New topic template content");
+
+ assert
+ .dom(".admin-changes-banner")
+ .exists("banner is shown after changing topic template");
+
+ await click(".admin-changes-banner .btn-primary");
+
+ assert.strictEqual(
+ putData.topic_template,
+ "New topic template content",
+ "it sends the updated topic template"
+ );
+ });
+
+ test("Selecting a form template shows the unsaved changes banner", async function (assert) {
+ putData = null;
+ await visit("/c/bug/edit/topic-template");
+
+ await click(".toggle-template-type");
+
+ const formTemplateChooser = selectKit(".form-template-chooser");
+ await formTemplateChooser.expand();
+ await formTemplateChooser.selectRowByValue(1);
+
+ assert
+ .dom(".admin-changes-banner")
+ .exists("banner is shown after selecting a form template");
+
+ await click(".admin-changes-banner .btn-primary");
+
+ assert.deepEqual(
+ putData.form_template_ids,
+ [1],
+ "it sends the updated form template ids"
+ );
+ assert
+ .dom(".admin-changes-banner")
+ .doesNotExist("banner is gone after saving");
+ });
+ }
+);
+
+acceptance("Admin - Legacy edit category topic template", function (needs) {
+ let putData;
+
+ needs.user({ admin: true });
+ needs.settings({
+ enable_simplified_category_creation: false,
+ });
+
+ needs.pretender((server, helper) => {
+ server.get("/c/bug/find_by_slug.json", () => {
+ return helper.response(200, {
+ category: {
+ id: 11,
+ name: "bug",
+ slug: "bug",
+ can_edit: true,
+ topic_template: "Existing template content",
+ category_type_site_settings: {},
+ permissions: [],
+ },
+ });
+ });
+
+ server.put("/categories/:id", (request) => {
+ putData = JSON.parse(request.requestBody);
+ return helper.response(200, {
+ category: {
+ id: request.params.id,
+ name: "bug",
+ can_edit: true,
+ category_type_site_settings: {},
+ permissions: [],
+ ...putData,
+ },
+ });
+ });
+ });
+
+ test("Changing topic template saves correctly", async function (assert) {
+ putData = null;
+ await visit("/c/bug/edit/topic-template");
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue(
+ "Existing template content",
+ "it loads the existing template content"
+ );
+
+ await fillIn(".d-editor-input", "Legacy flow template content");
+
+ await click("#save-category");
+
+ assert.strictEqual(
+ putData.topic_template,
+ "Legacy flow template content",
+ "it sends the updated topic template in legacy flow"
+ );
+ });
+});