Improve updating of viewV2 state and restore grid<>builder sync for datasource definitions

This commit is contained in:
Andrew Kingston 2023-08-04 14:54:45 +01:00
parent 46f16764db
commit cd2231630f
3 changed files with 50 additions and 28 deletions

View File

@ -13,7 +13,8 @@
} }
const handleGridViewUpdate = async e => { const handleGridViewUpdate = async e => {
viewsV2.replace(id, e.detail) console.log("update")
viewsV2.replaceView(id, e.detail)
} }
</script> </script>

View File

@ -1,4 +1,4 @@
import { writable, derived } from "svelte/store" import { writable, derived, get } from "svelte/store"
import { tables } from "./" import { tables } from "./"
import { API } from "api" import { API } from "api"
@ -30,44 +30,64 @@ export function createViewsV2Store() {
const deleteView = async view => { const deleteView = async view => {
await API.viewV2.delete(view.id) await API.viewV2.delete(view.id)
replaceView(view.id, null)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
delete table.views[view.name]
return { ...state }
})
} }
const create = async view => { const create = async view => {
const savedViewResponse = await API.viewV2.create(view) const savedViewResponse = await API.viewV2.create(view)
const savedView = savedViewResponse.data const savedView = savedViewResponse.data
replaceView(savedView.id, savedView)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
table.views[view.name] = savedView
return { ...state }
})
return savedView return savedView
} }
const save = async view => { const save = async view => {
const res = await API.viewV2.update(view) const res = await API.viewV2.update(view)
const savedView = res?.data const savedView = res?.data
replaceView(view.id, savedView)
}
// Update tables // Handles external updates of tables
tables.update(state => { const replaceView = (viewId, view) => {
const table = state.list.find(table => table._id === view.tableId) console.log("replace", viewId, view)
if (table) { if (!viewId) {
if (view.originalName) { return
delete table.views[view.originalName]
} }
table.views[view.name] = savedView const existingView = get(derivedStore).list.find(view => view.id === viewId)
} const tableIndex = get(tables).list.findIndex(table => {
return { ...state } return table._id === view?.tableId || table._id === existingView?.tableId
}) })
if (tableIndex === -1) {
return
}
// Handle deletion
if (!view) {
tables.update(state => {
delete state.list[tableIndex].views[existingView.name]
return state
})
return
}
// Add new view
if (!existingView) {
tables.update(state => {
state.list[tableIndex].views[view.name] = view
return state
})
}
// Update existing view
else {
tables.update(state => {
// Remove old view
delete state.list[tableIndex].views[existingView.name]
// Add new view
state.list[tableIndex].views[view.name] = view
return state
})
}
} }
return { return {
@ -76,6 +96,7 @@ export function createViewsV2Store() {
delete: deleteView, delete: deleteView,
create, create,
save, save,
replaceView,
} }
} }

View File

@ -80,7 +80,7 @@ export const createActions = context => {
// Broadcast change to external state can be updated, as this change // Broadcast change to external state can be updated, as this change
// will not be received by the builder websocket because we caused it ourselves // will not be received by the builder websocket because we caused it ourselves
dispatch("updatedefinition", newDefinition) dispatch("updatedatasource", newDefinition)
} }
// Adds a row to the datasource // Adds a row to the datasource