A platform for community discussion. Free, open, simple.
FIX: Error encountered when adding child object in object setting editor (#26259)
Alan Guo Xiang Tane5566b851930
diff --git a/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs b/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjsindex d53aabf1546..b8d8a08829d 100644--- a/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs+++ b/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs@@ -69,7 +69,9 @@ export default class SchemaThemeSettingEditor extends Component {index,schema,object,- text: object[schema.identifier] || `${schema.name} ${index + 1}`,+ text:+ object[schema.identifier] ||+ this.defaultSchemaIdentifier(schema.name, index),parentTree: tree,});@@ -84,7 +86,7 @@ export default class SchemaThemeSettingEditor extends Component {const subtree = new Tree();subtree.propertyName = childObjectsProperty.name;subtree.schema = childObjectsProperty.schema;- subtree.data = data[index][childObjectsProperty.name];+ subtree.data = data[index][childObjectsProperty.name] ||= [];data[index][childObjectsProperty.name]?.forEach((childObj, childIndex) => {@@ -232,6 +234,7 @@ export default class SchemaThemeSettingEditor extends Component {const data = this.activeNode.parentTree.data;data.splice(this.activeIndex, 1);this.tree.nodes = this.tree.nodes.filter((n, i) => i !== this.activeIndex);+if (data.length > 0) {this.activeIndex = Math.max(this.activeIndex - 1, 0);} else if (this.history.length > 0) {@@ -257,10 +260,14 @@ export default class SchemaThemeSettingEditor extends Component {return descriptions[key];}+ defaultSchemaIdentifier(schemaName, index) {+ return `${schemaName} ${index + 1}`;+ }+createNodeFromSchema(schema, tree) {const object = {};const index = tree.nodes.length;- const defaultName = `${schema.name} ${index + 1}`;+ const defaultName = this.defaultSchemaIdentifier(schema.name, index);if (schema.identifier) {object[schema.identifier] = defaultName;diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-schema-theme-setting/editor-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/admin-schema-theme-setting/editor-test.gjsindex 1a25a64e48d..8706f75e769 100644--- a/app/assets/javascripts/discourse/tests/integration/components/admin-schema-theme-setting/editor-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/admin-schema-theme-setting/editor-test.gjs@@ -25,7 +25,7 @@ class TreeFromDOM {const children = [...queryAll(- `.schema-theme-setting-editor__tree-node.--child[data-test-parent-index="${index}"]:not(.--heading)`+ `.schema-theme-setting-editor__tree-node.--child[data-test-parent-index="${index}"]`),].map((child) => {return {@@ -824,6 +824,70 @@ module(assert.dom(tree.nodes[3].textElement).hasText("level1 4");});+ test("adding an object to a child list of objects when an object has multiple objects properties", async function (assert) {+ const setting = ThemeSettings.create({+ setting: "objects_setting",+ objects_schema: {+ name: "something",+ properties: {+ title: {+ type: "string",+ },+ links: {+ type: "objects",+ schema: {+ name: "link",+ properties: {+ url: {+ type: "string",+ },+ },+ },+ },+ chairs: {+ type: "objects",+ schema: {+ name: "chair",+ properties: {+ name: {+ type: "string",+ },+ },+ },+ },+ },+ },+ value: [+ {+ title: "some title",+ },+ ],+ });++ await render(<template>+ <AdminSchemaThemeSettingEditor @themeId="1" @setting={{setting}} />+ </template>);++ const tree = new TreeFromDOM();++ await click(tree.nodes[0].addButtons[0]);++ tree.refresh();++ assert.dom(tree.nodes[0].children[0].textElement).hasText("link 1");++ await click(tree.nodes[0].addButtons[1]);+ tree.refresh();++ assert.dom(tree.nodes[0].children[1].textElement).hasText("chair 1");++ await click(tree.nodes[0].children[0].element);++ const inputFields = new InputFieldsFromDOM();++ assert.dom(inputFields.fields.url.labelElement).hasText("url");+ });+test("adding an object to a child list of objects", async function (assert) {const setting = schemaAndData(1);