diff --git a/templates/content/actions/update-content-database-view.test.ts b/templates/content/actions/update-content-database-view.test.ts index b65c34d2e4..cc7ad3a7fe 100644 --- a/templates/content/actions/update-content-database-view.test.ts +++ b/templates/content/actions/update-content-database-view.test.ts @@ -56,6 +56,7 @@ describe("update content database view", () => { }, ], collapsedGroupIds: ["status:done"], + propertyOrderIds: ["name", "status"], hideEmptyGroups: true, calculations: { status: "count_values" }, wrapCells: true, diff --git a/templates/content/app/components/editor/database/DatabaseView.test.ts b/templates/content/app/components/editor/database/DatabaseView.test.ts index 03f26fd4ee..6c716da91d 100644 --- a/templates/content/app/components/editor/database/DatabaseView.test.ts +++ b/templates/content/app/components/editor/database/DatabaseView.test.ts @@ -54,6 +54,8 @@ import { databaseNextBuilderHydrationSource, databasePreviewItem, databaseItemPagePath, + orderDatabasePropertiesForView, + reorderDatabaseViewProperty, databaseRecordBuilderContinuationAttempt, databaseSourceOperationIsPending, databaseSourceChangeSetsAreComplete, @@ -1270,6 +1272,87 @@ const baseProperty = ( editable: true, }); +describe("database property column order", () => { + const view = { + id: "table", + name: "Table", + type: "table" as const, + sorts: [], + filters: [], + columnWidths: {}, + }; + const propertyIds = (properties: DocumentProperty[]) => + properties.map((property) => property.definition.id); + + it("moves a visible property before another while preserving hidden columns", () => { + const allProperties = [ + baseProperty("alpha"), + baseProperty("hidden"), + baseProperty("bravo"), + baseProperty("charlie"), + ]; + const visibleProperties = [ + allProperties[0], + allProperties[2], + allProperties[3], + ]; + + const reordered = reorderDatabaseViewProperty( + view, + "charlie", + "alpha", + { allProperties, visibleProperties }, + "before", + ); + + expect(reordered.propertyOrderIds).toEqual([ + "charlie", + "alpha", + "hidden", + "bravo", + ]); + expect( + propertyIds(orderDatabasePropertiesForView(allProperties, reordered)), + ).toEqual(["charlie", "alpha", "hidden", "bravo"]); + }); + + it("keeps surviving explicit order and appends new properties", () => { + const properties = [ + baseProperty("alpha"), + baseProperty("bravo"), + baseProperty("charlie"), + baseProperty("delta"), + ]; + + expect( + propertyIds( + orderDatabasePropertiesForView(properties, { + propertyOrderIds: ["deleted", "charlie", "alpha", "bravo"], + }), + ), + ).toEqual(["charlie", "alpha", "bravo", "delta"]); + }); + + it("does not reorder from or onto a hidden property", () => { + const allProperties = [ + baseProperty("alpha"), + baseProperty("hidden"), + baseProperty("bravo"), + ]; + const visibleProperties = [allProperties[0], allProperties[2]]; + + expect( + reorderDatabaseViewProperty( + view, + "hidden", + "alpha", + { allProperties, visibleProperties }, + "before", + ), + ).toBe(view); + }); +}); + const builderRowItem = (id: string): ContentDatabaseItem => ({ id: `item-${id}`, databaseId: "database", diff --git a/templates/content/app/components/editor/database/DatabaseView.tsx b/templates/content/app/components/editor/database/DatabaseView.tsx index f5e9228ffb..8eceb128c2 100644 --- a/templates/content/app/components/editor/database/DatabaseView.tsx +++ b/templates/content/app/components/editor/database/DatabaseView.tsx @@ -2431,6 +2431,14 @@ function DatabaseTable({ : nextViewConfig, ); }, + onError: (err) => { + toast.error(dbText("failedToSaveView"), { + description: + err instanceof Error + ? err.message + : dbText("somethingWentWrong"), + }); + }, }, ); }, 350);