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) {
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 => {
state.list[index] = table
state.list[index] = {
...table,
type: state.list[index].type,
}
return state
})
}

View File

@ -18,9 +18,7 @@
dispatch,
config,
ui,
table,
rows,
API,
columns,
} = getContext("sheet")
let anchor
@ -83,15 +81,7 @@
}
const makeDisplayColumn = async () => {
const tableDefinition = $table
if (!tableDefinition) {
return
}
await API.saveTable({
...tableDefinition,
primaryDisplay: column.name,
})
await rows.actions.refreshTableDefinition()
await columns.actions.changePrimaryDisplay(column.name)
open = false
}
</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
const saveChanges = async () => {
const $columns = get(columns)
@ -153,8 +161,11 @@ export const deriveStores = context => {
}
})
await saveTable({ ...$table, schema: newSchema })
}
const saveTable = async newTable => {
// Update local state
const newTable = { ...$table, schema: newSchema }
table.set(newTable)
// Broadcast event so that we can keep sync with external state
@ -170,6 +181,7 @@ export const deriveStores = context => {
...columns,
actions: {
saveChanges,
changePrimaryDisplay,
},
},
}