This commit is contained in:
Peter Clement 2025-01-09 09:20:16 +00:00
parent a983292865
commit b6418c333c
2 changed files with 94 additions and 67 deletions

View File

@ -1,67 +0,0 @@
import { writable, derived } from "svelte/store"
import { tables } from "./tables"
import { API } from "@/api"
export function createViewsStore() {
const store = writable({
selectedViewName: null,
})
const derivedStore = derived([store, tables], ([$store, $tables]) => {
let list = []
$tables.list?.forEach(table => {
const views = Object.values(table?.views || {}).filter(view => {
return view.version !== 2
})
list = list.concat(views)
})
return {
...$store,
list,
selected: list.find(view => view.name === $store.selectedViewName),
}
})
const select = name => {
store.update(state => ({
...state,
selectedViewName: name,
}))
}
const deleteView = async view => {
await API.deleteView(view.name)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
delete table.views[view.name]
return { ...state }
})
}
const save = async view => {
const savedView = await API.saveView(view)
select(view.name)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
if (table) {
if (view.originalName) {
delete table.views[view.originalName]
}
table.views[view.name] = savedView
}
return { ...state }
})
}
return {
subscribe: derivedStore.subscribe,
select,
delete: deleteView,
save,
}
}
export const views = createViewsStore()

View File

@ -0,0 +1,94 @@
import { DerivedBudiStore } from "../BudiStore"
import { tables } from "./tables"
import { API } from "@/api"
import { View } from "@budibase/types"
import { helpers } from "@budibase/shared-core"
import { derived, Writable } from "svelte/store"
interface BuilderViewStore {
selectedViewName: string | null
}
interface DerivedViewStore extends BuilderViewStore {
list: View[]
selected?: View
}
export class ViewsStore extends DerivedBudiStore<
BuilderViewStore,
DerivedViewStore
> {
constructor() {
const makeDerivedStore = (store: Writable<BuilderViewStore>) => {
return derived([store, tables], ([$store, $tables]): DerivedViewStore => {
let list: View[] = []
$tables.list?.forEach(table => {
const views = Object.values(table?.views || {}).filter(
(view): view is View => !helpers.views.isV2(view)
)
list = list.concat(views)
})
return {
selectedViewName: $store.selectedViewName,
list,
selected: list.find(view => view.name === $store.selectedViewName),
}
})
}
super(
{
selectedViewName: null,
},
makeDerivedStore
)
this.select = this.select.bind(this)
}
select = (name: string) => {
this.store.update(state => ({
...state,
selectedViewName: name,
}))
}
delete = async (view: View) => {
if (!view.name) {
return
}
await API.deleteView(view.name)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
if (table?.views && view.name) {
delete table.views[view.name]
}
return { ...state }
})
}
save = async (view: View & { originalName?: string }) => {
if (!view.name) {
return
}
const savedView = await API.saveView(view)
this.select(view.name)
// Update tables
tables.update(state => {
const table = state.list.find(table => table._id === view.tableId)
if (table?.views && view.name) {
if (view.originalName) {
delete table.views[view.originalName]
}
table.views[view.name] = savedView
}
return { ...state }
})
}
}
export const views = new ViewsStore()