From 3ae5216fd5be78a36f0ffe3c1765af723d576c95 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 18 Sep 2023 19:07:19 +0100 Subject: [PATCH] Make update forms work with view V2s --- .../client/src/components/app/forms/Form.svelte | 13 ++++++++++++- packages/client/src/utils/buttonActions.js | 17 ++++++++++------- packages/frontend-core/src/api/rows.js | 10 ++++++---- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/client/src/components/app/forms/Form.svelte b/packages/client/src/components/app/forms/Form.svelte index c973ff000d..3845d50748 100644 --- a/packages/client/src/components/app/forms/Form.svelte +++ b/packages/client/src/components/app/forms/Form.svelte @@ -63,7 +63,18 @@ // ancestor that matches our datasource. This is for backwards compatibility // as previously we could use the "closest" context. for (let id of path.reverse().slice(1)) { - if (context[id]?.tableId === dataSource.tableId) { + // Check for matching view datasource + if ( + dataSource.type === "viewV2" && + context[id]?._viewId === dataSource.id + ) { + return context[id] + } + // Check for matching table datasource + if ( + dataSource.type === "table" && + context[id]?.tableId === dataSource.tableId + ) { return context[id] } } diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index cec47dad35..87f6bb0170 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -80,20 +80,21 @@ const saveRowHandler = async (action, context) => { } } if (tableId) { - payload.tableId = tableId + if (tableId.startsWith("view")) { + payload._viewId = tableId + } else { + payload.tableId = tableId + } } try { const row = await API.saveRow(payload) - if (!notificationOverride) { notificationStore.actions.success("Row saved") } - // Refresh related datasources await dataSourceStore.actions.invalidateDataSource(tableId, { invalidateRelationships: true, }) - return { row } } catch (error) { // Abort next actions @@ -112,7 +113,11 @@ const duplicateRowHandler = async (action, context) => { } } if (tableId) { - payload.tableId = tableId + if (tableId.startsWith("view")) { + payload._viewId = tableId + } else { + payload.tableId = tableId + } } delete payload._id delete payload._rev @@ -121,12 +126,10 @@ const duplicateRowHandler = async (action, context) => { if (!notificationOverride) { notificationStore.actions.success("Row saved") } - // Refresh related datasources await dataSourceStore.actions.invalidateDataSource(tableId, { invalidateRelationships: true, }) - return { row } } catch (error) { // Abort next actions diff --git a/packages/frontend-core/src/api/rows.js b/packages/frontend-core/src/api/rows.js index 475ef0e2a4..79f837e864 100644 --- a/packages/frontend-core/src/api/rows.js +++ b/packages/frontend-core/src/api/rows.js @@ -19,11 +19,12 @@ export const buildRowEndpoints = API => ({ * @param suppressErrors whether or not to suppress error notifications */ saveRow: async (row, suppressErrors = false) => { - if (!row?.tableId) { + const resourceId = row?._viewId || row?.tableId + if (!resourceId) { return } return await API.post({ - url: `/api/${row._viewId || row.tableId}/rows`, + url: `/api/${resourceId}/rows`, body: row, suppressErrors, }) @@ -35,11 +36,12 @@ export const buildRowEndpoints = API => ({ * @param suppressErrors whether or not to suppress error notifications */ patchRow: async (row, suppressErrors = false) => { - if (!row?.tableId && !row?._viewId) { + const resourceId = row?._viewId || row?.tableId + if (!resourceId) { return } return await API.patch({ - url: `/api/${row._viewId || row.tableId}/rows`, + url: `/api/${resourceId}/rows`, body: row, suppressErrors, })