split out views to separate store

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-23 12:04:11 +01:00
parent 0afc55f4cb
commit 071522c43f
2 changed files with 50 additions and 0 deletions

View File

@ -1,5 +1,6 @@
export { database } from "./database"
export { tables } from "./tables"
export { views } from "./views"
export { permissions } from "./permissions"
export { roles } from "./roles"
export { datasources } from "./datasources"

View File

@ -0,0 +1,49 @@
import { writable, get } from "svelte/store"
import { tables } from "./"
import api from "../../api"
function createViewsStore() {
const { subscribe, set, update } = writable({
list: [],
selected: null
})
return {
subscribe,
select: view => {
update(state => ({
...state,
selected: view,
}))
tables.select()
},
delete: async view => {
await api.delete(`/api/views/${view}`)
await tables.fetch()
},
save: async view => {
const response = await api.post(`/api/views`, view)
const json = await response.json()
const viewMeta = {
name: view.name,
...json,
}
update(state => {
const viewTable = get(tables).list.find(
table => table._id === view.tableId
)
if (view.originalName) delete viewTable.views[view.originalName]
viewTable.views[view.name] = viewMeta
state.tables = state.tables
state.selectedView = viewMeta
return state
})
},
}
}
export const views = createViewsStore()