Make update forms work with view V2s

This commit is contained in:
Andrew Kingston 2023-09-18 19:07:19 +01:00
parent 917e837ffc
commit 3ae5216fd5
3 changed files with 28 additions and 12 deletions

View File

@ -63,7 +63,18 @@
// ancestor that matches our datasource. This is for backwards compatibility // ancestor that matches our datasource. This is for backwards compatibility
// as previously we could use the "closest" context. // as previously we could use the "closest" context.
for (let id of path.reverse().slice(1)) { 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] return context[id]
} }
} }

View File

@ -80,20 +80,21 @@ const saveRowHandler = async (action, context) => {
} }
} }
if (tableId) { if (tableId) {
if (tableId.startsWith("view")) {
payload._viewId = tableId
} else {
payload.tableId = tableId payload.tableId = tableId
} }
}
try { try {
const row = await API.saveRow(payload) const row = await API.saveRow(payload)
if (!notificationOverride) { if (!notificationOverride) {
notificationStore.actions.success("Row saved") notificationStore.actions.success("Row saved")
} }
// Refresh related datasources // Refresh related datasources
await dataSourceStore.actions.invalidateDataSource(tableId, { await dataSourceStore.actions.invalidateDataSource(tableId, {
invalidateRelationships: true, invalidateRelationships: true,
}) })
return { row } return { row }
} catch (error) { } catch (error) {
// Abort next actions // Abort next actions
@ -112,8 +113,12 @@ const duplicateRowHandler = async (action, context) => {
} }
} }
if (tableId) { if (tableId) {
if (tableId.startsWith("view")) {
payload._viewId = tableId
} else {
payload.tableId = tableId payload.tableId = tableId
} }
}
delete payload._id delete payload._id
delete payload._rev delete payload._rev
try { try {
@ -121,12 +126,10 @@ const duplicateRowHandler = async (action, context) => {
if (!notificationOverride) { if (!notificationOverride) {
notificationStore.actions.success("Row saved") notificationStore.actions.success("Row saved")
} }
// Refresh related datasources // Refresh related datasources
await dataSourceStore.actions.invalidateDataSource(tableId, { await dataSourceStore.actions.invalidateDataSource(tableId, {
invalidateRelationships: true, invalidateRelationships: true,
}) })
return { row } return { row }
} catch (error) { } catch (error) {
// Abort next actions // Abort next actions

View File

@ -19,11 +19,12 @@ export const buildRowEndpoints = API => ({
* @param suppressErrors whether or not to suppress error notifications * @param suppressErrors whether or not to suppress error notifications
*/ */
saveRow: async (row, suppressErrors = false) => { saveRow: async (row, suppressErrors = false) => {
if (!row?.tableId) { const resourceId = row?._viewId || row?.tableId
if (!resourceId) {
return return
} }
return await API.post({ return await API.post({
url: `/api/${row._viewId || row.tableId}/rows`, url: `/api/${resourceId}/rows`,
body: row, body: row,
suppressErrors, suppressErrors,
}) })
@ -35,11 +36,12 @@ export const buildRowEndpoints = API => ({
* @param suppressErrors whether or not to suppress error notifications * @param suppressErrors whether or not to suppress error notifications
*/ */
patchRow: async (row, suppressErrors = false) => { patchRow: async (row, suppressErrors = false) => {
if (!row?.tableId && !row?._viewId) { const resourceId = row?._viewId || row?.tableId
if (!resourceId) {
return return
} }
return await API.patch({ return await API.patch({
url: `/api/${row._viewId || row.tableId}/rows`, url: `/api/${resourceId}/rows`,
body: row, body: row,
suppressErrors, suppressErrors,
}) })