Work around table API inconsistencies to handle table schema updates

This commit is contained in:
Andrew Kingston 2023-04-13 16:51:26 +01:00
parent f86c0ec36e
commit 84a5739c53
3 changed files with 23 additions and 14 deletions

View File

@ -140,8 +140,15 @@ export function createTablesStore() {
if (index === -1) { if (index === -1) {
return return
} }
// This function has to merge state as there discrepancies with the table
// API endpoints. The table list endpoint and get table endpoint use the
// "type" property to mean different things.
store.update(state => { store.update(state => {
state.list[index] = table state.list[index] = {
...table,
type: state.list[index].type,
}
return state return state
}) })
} }

View File

@ -18,9 +18,7 @@
dispatch, dispatch,
config, config,
ui, ui,
table, columns,
rows,
API,
} = getContext("sheet") } = getContext("sheet")
let anchor let anchor
@ -83,15 +81,7 @@
} }
const makeDisplayColumn = async () => { const makeDisplayColumn = async () => {
const tableDefinition = $table await columns.actions.changePrimaryDisplay(column.name)
if (!tableDefinition) {
return
}
await API.saveTable({
...tableDefinition,
primaryDisplay: column.name,
})
await rows.actions.refreshTableDefinition()
open = false open = false
} }
</script> </script>

View File

@ -126,6 +126,14 @@ export const deriveStores = context => {
}) })
}) })
// Updates the tables primary display column
const changePrimaryDisplay = async column => {
return await saveTable({
...get(table),
primaryDisplay: column,
})
}
// Persists column changes by saving metadata against table schema // Persists column changes by saving metadata against table schema
const saveChanges = async () => { const saveChanges = async () => {
const $columns = get(columns) const $columns = get(columns)
@ -153,8 +161,11 @@ export const deriveStores = context => {
} }
}) })
await saveTable({ ...$table, schema: newSchema })
}
const saveTable = async newTable => {
// Update local state // Update local state
const newTable = { ...$table, schema: newSchema }
table.set(newTable) table.set(newTable)
// Broadcast event so that we can keep sync with external state // Broadcast event so that we can keep sync with external state
@ -170,6 +181,7 @@ export const deriveStores = context => {
...columns, ...columns,
actions: { actions: {
saveChanges, saveChanges,
changePrimaryDisplay,
}, },
}, },
} }