Skip to content

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

FIX: Track nested settings so newly added items render (#43720)

Régis Hanola064253c5d6b
diff --git a/frontend/discourse/admin/components/schema-setting/editor.gjs b/frontend/discourse/admin/components/schema-setting/editor.gjs
index d7c2e1f63a7..cfd4631a8e3 100644
--- a/frontend/discourse/admin/components/schema-setting/editor.gjs
+++ b/frontend/discourse/admin/components/schema-setting/editor.gjs
@@ -1,9 +1,8 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { isArray } from "@ember/array";
-import { fn } from "@ember/helper";
+import { fn, get } from "@ember/helper";
import { action } from "@ember/object";
-import { trackedArray } from "@ember/reactive/collections";
+import { trackedArray, trackedObject } from "@ember/reactive/collections";
import { service } from "@ember/service";
import Tree from "discourse/admin/components/schema-setting/editor/tree";
import FieldInput from "discourse/admin/components/schema-setting/field";
@@ -26,9 +25,12 @@ export default class SchemaSettingNewEditor extends Component {
@autoTrackedArray activeSchemaPaths = [];
@autoTrackedArray history = [];
- inputFieldObserver = new Map();
- data = this.#trackNestedArrays(cloneJSON(this.args.setting.value));
schema = this.args.schema;
+ data = trackedArray(
+ cloneJSON(this.args.setting.value).map((object) =>
+ this.#prepareObject(object, this.schema)
+ )
+ );
get backButtonText() {
if (this.history.length === 0) {
@@ -50,28 +52,32 @@ export default class SchemaSettingNewEditor extends Component {
return this.#resolveDataFromPaths(this.activeDataPaths);
}
+ get activeObject() {
+ return this.activeData[this.activeIndex];
+ }
+
get activeSchema() {
return this.#resolveSchemaFromPaths(this.activeSchemaPaths);
}
get fields() {
const list = [];
- const activeObject = this.activeData[this.activeIndex];
- if (activeObject) {
- for (const [name, spec] of Object.entries(this.activeSchema.properties)) {
- if (spec.type === "objects") {
- continue;
- }
+ if (!this.activeObject) {
+ return list;
+ }
- list.push({
- name,
- spec,
- value: activeObject[name],
- description: this.fieldDescription(name, spec),
- label: this.fieldLabel(name, spec),
- });
+ for (const [name, spec] of Object.entries(this.activeSchema.properties)) {
+ if (spec.type === "objects") {
+ continue;
}
+
+ list.push({
+ name,
+ spec,
+ description: this.fieldDescription(name, spec),
+ label: this.fieldLabel(name, spec),
+ });
}
return list;
@@ -96,7 +102,6 @@ export default class SchemaSettingNewEditor extends Component {
this.activeIndex = index;
this.activeDataPaths.push(parentNodeIndex, propertyName);
this.activeSchemaPaths.push(propertyName);
- this.inputFieldObserver.clear();
}
@action
@@ -109,7 +114,7 @@ export default class SchemaSettingNewEditor extends Component {
let title;
if (schema.properties[schema.identifier]?.type === "categories") {
- title = this.activeData[index][schema.identifier]
+ title = object[schema.identifier]
?.map((categoryId) => {
return (
this.args.setting.metadata?.categories?.[categoryId]?.name ||
@@ -125,11 +130,6 @@ export default class SchemaSettingNewEditor extends Component {
return title || `${schema.name} ${index + 1}`;
}
- @action
- registerInputFieldObserver(index, callback) {
- this.inputFieldObserver[index] = callback;
- }
-
descriptions(fieldName, key) {
// The `property_descriptions` metadata is an object with keys in the following format as an example:
//
@@ -173,12 +173,13 @@ export default class SchemaSettingNewEditor extends Component {
this.activeDataPaths = lastDataPaths;
this.activeSchemaPaths = lastSchemaPaths;
this.activeIndex = lastIndex;
- this.inputFieldObserver.clear();
}
@action
addChildItem(propertyName, parentNodeIndex) {
- this.activeData[parentNodeIndex][propertyName].push({});
+ this.activeData[parentNodeIndex][propertyName].push(
+ this.#prepareObject({}, this.activeSchema.properties[propertyName].schema)
+ );
this.onChildClick(
this.activeData[parentNodeIndex][propertyName].length - 1,
@@ -189,7 +190,7 @@ export default class SchemaSettingNewEditor extends Component {
@action
addItem() {
- this.activeData.push({});
+ this.activeData.push(this.#prepareObject({}, this.activeSchema));
this.activeIndex = this.activeData.length - 1;
}
@@ -218,11 +219,7 @@ export default class SchemaSettingNewEditor extends Component {
@action
inputFieldChanged(field, newVal) {
- this.activeData[this.activeIndex][field.name] = newVal;
-
- if (field.name === this.activeSchema.identifier) {
- this.inputFieldObserver[this.activeIndex]();
- }
+ this.activeObject[field.name] = newVal;
}
@action
@@ -292,46 +289,25 @@ export default class SchemaSettingNewEditor extends Component {
#swapAdjacentItems(fromIndex, toIndex) {
const item = this.activeData[fromIndex];
- const fromCallback = this.inputFieldObserver[fromIndex];
- const toCallback = this.inputFieldObserver[toIndex];
- // Move the data
this.activeData.splice(fromIndex, 1);
this.activeData.splice(toIndex, 0, item);
-
- // Swap the observer callbacks to match new positions
- this.inputFieldObserver[toIndex] = fromCallback;
- this.inputFieldObserver[fromIndex] = toCallback;
}
- /**
- * Recursively converts nested arrays to TrackedArrays for reactivity
- *
- * @param {*} input - The input value to convert
- * @returns {TrackedArray|*} The converted value with TrackedArrays
- *
- * @private
- */
- #trackNestedArrays(input) {
- // Return early if input is null/undefined/empty
- if (!input) {
- return input;
- }
-
- // If input is an array, convert it to a TrackedArray and recursively convert its items
- if (isArray(input)) {
- return trackedArray(input.map((item) => this.#trackNestedArrays(item)));
- }
-
- // If input is an object, recursively convert its values
- if (typeof input === "object") {
- Object.keys(input).forEach((key) => {
- input[key] = this.#trackNestedArrays(input[key]);
- });
+ #prepareObject(object, schema) {
+ for (const [name, spec] of Object.entries(schema.properties)) {
+ if (spec.type === "objects") {
+ object[name] = trackedArray(
+ (object[name] || []).map((child) =>
+ this.#prepareObject(child, spec.schema)
+ )
+ );
+ } else if (Array.isArray(object[name])) {
+ object[name] = trackedArray(object[name]);
+ }
}
- // Return the input after converting any arrays to TrackedArrays
- return input;
+ return trackedObject(object);
}
async _confirmRemove(warning) {
@@ -366,7 +342,6 @@ export default class SchemaSettingNewEditor extends Component {
@data={{this.activeData}}
@generateSchemaTitle={{this.generateSchemaTitle}}
@onChildClick={{this.onChildClick}}
- @registerInputFieldObserver={{this.registerInputFieldObserver}}
@schema={{this.activeSchema}}
@updateIndex={{this.updateIndex}}
/>
@@ -390,7 +365,7 @@ export default class SchemaSettingNewEditor extends Component {
@onValueChange={{fn this.inputFieldChanged field}}
@setting={{@setting}}
@spec={{field.spec}}
- @value={{field.value}}
+ @value={{get this.activeObject field.name}}
/>
{{/each}}
diff --git a/frontend/discourse/admin/components/schema-setting/editor/tree-node.gjs b/frontend/discourse/admin/components/schema-setting/editor/tree-node.gjs
index 0dae300f75b..2e400979252 100644
--- a/frontend/discourse/admin/components/schema-setting/editor/tree-node.gjs
+++ b/frontend/discourse/admin/components/schema-setting/editor/tree-node.gjs
@@ -1,52 +1,26 @@
import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
import { get } from "@ember/helper";
import { on } from "@ember/modifier";
import ChildTree from "discourse/admin/components/schema-setting/editor/child-tree";
-import { bind } from "discourse/lib/decorators";
import { gt } from "discourse/truth-helpers";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
import dIcon from "discourse/ui-kit/helpers/d-icon";
export default class SchemaSettingNewEditorTreeNode extends Component {
- @tracked text;
-
- childObjectsProperties = this.findChildObjectsProperties(
- this.args.schema.properties
- );
-
- constructor() {
- super(...arguments);
- this.setText();
- this.args.registerInputFieldObserver(this.args.index, this.setText);
+ get childObjectsProperties() {
+ return Object.entries(this.args.schema.properties)
+ .filter(([, spec]) => spec.type === "objects")
+ .map(([name, spec]) => ({ name, schema: spec.schema }));
}
- @bind
- setText() {
- this.text = this.args.generateSchemaTitle(
+ get text() {
+ return this.args.generateSchemaTitle(
this.args.object,
this.args.schema,
this.args.index
);
}
- findChildObjectsProperties(properties) {
- const list = [];
-
- for (const [name, spec] of Object.entries(properties)) {
- if (spec.type === "objects") {
- this.args.object[name] ||= [];
-
- list.push({
- name,
- schema: spec.schema,
- });
- }
- }
-
- return list;
- }
-
<template>
<li
class={{dConcatClass
diff --git a/frontend/discourse/admin/components/schema-setting/editor/tree.gjs b/frontend/discourse/admin/components/schema-setting/editor/tree.gjs
index 6fe62588f8a..001a42aaf60 100644
--- a/frontend/discourse/admin/components/schema-setting/editor/tree.gjs
+++ b/frontend/discourse/admin/components/schema-setting/editor/tree.gjs
@@ -29,7 +29,6 @@ export default <template>
@object={{object}}
@onChildClick={{@onChildClick}}
@onClick={{fn @updateIndex index}}
- @registerInputFieldObserver={{@registerInputFieldObserver}}
@schema={{@schema}}
/>
{{/each}}
diff --git a/frontend/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs b/frontend/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs
index f0122af46e7..d58cc9011a6 100644
--- a/frontend/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs
+++ b/frontend/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs
@@ -1,4 +1,11 @@
-import { click, fillIn, findAll, render, waitFor } from "@ember/test-helpers";
+import {
+ click,
+ fillIn,
+ findAll,
+ focus,
+ render,
+ waitFor,
+} from "@ember/test-helpers";
import { module, test } from "qunit";
import AdminSchemaSettingEditor from "discourse/admin/components/schema-setting/editor";
import SiteSetting from "discourse/admin/models/site-setting";
@@ -1018,6 +1025,14 @@ module(
name: "category",
identifier: "category",
properties: {
+ children: {
+ type: "objects",
+ schema: {
+ name: "child",
+ identifier: "category",
+ properties: { category: { type: "categories" } },
+ },
+ },
category: {
type: "categories",
required: true,
@@ -1039,6 +1054,7 @@ module(
value: [
{
category: [6, 7],
+ children: [{ category: [7] }],
},
],
});
@@ -1058,6 +1074,19 @@ module(
assert.dom(tree.nodes[0].textElement).hasText("support, something");
+ assert
+ .dom(tree.nodes[0].children[0].textElement)
+ .hasText("something", "the child uses its own category identifier");
+ await click(tree.nodes[0].children[0].element);
+ assert.dom(".--back-btn").hasText(
+ i18n("admin.customize.schema.back_button", {
+ name: "support, something",
+ }),
+ "the back button uses the parent's category identifier"
+ );
+ await click(".--back-btn");
+ tree.refresh();
+
const inputFields = new InputFieldsFromDOM();
const categorySelector = selectKit(
@@ -1400,6 +1429,13 @@ module(
tree.refresh();
assert.dom(tree.nodes[1].children[0].textElement).hasText("link 1");
+
+ await click(tree.nodes[0].element);
+ await click(REMOVE_ITEM_BTN);
+ tree.refresh();
+ assert
+ .dom(tree.nodes[0].textElement)
+ .hasText("section 1", "the remaining item is renumbered");
});
test("identifier field instantly updates in the navigation tree when the input field is changed", async function (assert) {
@@ -1419,15 +1455,25 @@ module(
const inputFields = new InputFieldsFromDOM();
const tree = new TreeFromDOM();
+ await focus(inputFields.fields.name.inputElement);
await fillIn(
inputFields.fields.name.inputElement,
"nice section is really nice"
);
+ assert
+ .dom(inputFields.fields.name.inputElement)
+ .isFocused("editing preserves the input and its focus");
assert
.dom(tree.nodes[0].textElement)
.hasText("nice section is really nice");
+ assert.strictEqual(
+ setting.value[0].name,
+ "nice section",
+ "unsaved edits do not mutate the setting"
+ );
+
await click(tree.nodes[0].children[0].element);
inputFields.refresh();
@@ -1550,7 +1596,7 @@ module(
assert.dom(inputFields.fields.name.labelElement).hasText("name");
});
- test("adding an object to the root list of objects", async function (assert) {
+ test("adding a parent and multiple nested objects", async function (assert) {
const setting = schemaAndData(1);
await render(
@@ -1577,6 +1623,20 @@ module(
assert.true(tree.nodes[2].active);
assert.dom(tree.nodes[2].textElement).hasText("level1 3");
assert.dom(TOP_LEVEL_ADD_BTN).hasText("level1");
+
+ await click(tree.nodes[2].addButtons[0]);
+ await click(TOP_LEVEL_ADD_BTN);
+ tree.refresh();
+
+ assert.strictEqual(
+ tree.nodes.length,
+ 3,
+ "both children and the add button are visible"
+ );
+ assert
+ .dom(tree.nodes[1].textElement)
+ .hasText("level2 2", "the second child appears without reopening");
+ assert.true(tree.nodes[1].active, "the second child is selected");
});
test("adding an object to a child list of objects when an object has multiple objects properties", async function (assert) {
@@ -1708,7 +1768,7 @@ module(
assert.dom(tree.nodes[3].textElement).hasText("level2 4");
});
- test("navigating 1 level deep and adding an object to a grandchild list of objects", async function (assert) {
+ test("adding multiple grandchildren to a newly added child", async function (assert) {
const setting = schemaAndData(1);
await render(
@@ -1724,17 +1784,30 @@ module(
const tree = new TreeFromDOM();
- await click(tree.nodes[0].children[0].element);
+ await click(tree.nodes[0].addButtons[0]);
tree.refresh();
- assert.dom(tree.nodes[0].addButtons[0]).hasText("level3");
- assert.strictEqual(tree.nodes[0].children.length, 2);
+ assert.dom(tree.nodes[2].addButtons[0]).hasText("level3");
+ assert.strictEqual(tree.nodes[2].children.length, 0);
- await click(tree.nodes[0].addButtons[0]);
+ await click(tree.nodes[2].addButtons[0]);
+ await click(TOP_LEVEL_ADD_BTN);
tree.refresh();
- assert.dom(tree.nodes[2].textElement).hasText("level3 3");
+ assert.strictEqual(
+ tree.nodes.length,
+ 3,
+ "both grandchildren and the add button are visible"
+ );
+ const inputFields = new InputFieldsFromDOM();
+ await fillIn(inputFields.fields.name.inputElement, "Second grandchild");
+ assert
+ .dom(tree.nodes[1].textElement)
+ .hasText(
+ "Second grandchild",
+ "the new grandchild is editable and updates its title"
+ );
});
test("removing an object from the root list of objects", async function (assert) {