split out views to separate store
This commit is contained in:
parent
0afc55f4cb
commit
071522c43f
|
@ -1,5 +1,6 @@
|
||||||
export { database } from "./database"
|
export { database } from "./database"
|
||||||
export { tables } from "./tables"
|
export { tables } from "./tables"
|
||||||
|
export { views } from "./views"
|
||||||
export { permissions } from "./permissions"
|
export { permissions } from "./permissions"
|
||||||
export { roles } from "./roles"
|
export { roles } from "./roles"
|
||||||
export { datasources } from "./datasources"
|
export { datasources } from "./datasources"
|
||||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue