convert selectedLayout derived store

This commit is contained in:
Peter Clement 2025-01-02 11:04:30 +00:00
parent 8047ff99c6
commit 7c36d8dac5
1 changed files with 28 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import { derived, get } from "svelte/store" import { derived, get } from "svelte/store"
import { componentStore } from "@/stores/builder" import { componentStore } from "@/stores/builder"
import { API } from "@/api" import { API } from "@/api"
import { BudiStore } from "../BudiStore" import { BudiStore, DerivedBudiStore } from "../BudiStore"
import { Layout } from "@budibase/types" import { Layout } from "@budibase/types"
interface LayoutState { interface LayoutState {
@ -9,6 +9,10 @@ interface LayoutState {
selectedLayoutId: string | null selectedLayoutId: string | null
} }
interface DerivedLayoutState extends LayoutState {
selectedLayout: Layout | null
}
export const INITIAL_LAYOUT_STATE: LayoutState = { export const INITIAL_LAYOUT_STATE: LayoutState = {
layouts: [], layouts: [],
selectedLayoutId: null, selectedLayoutId: null,
@ -62,10 +66,10 @@ export class LayoutStore extends BudiStore<LayoutState> {
} }
async deleteLayout(layout: Layout) { async deleteLayout(layout: Layout) {
if (!layout?._id) { if (!layout?._id || !layout?._rev) {
return return
} }
await API.deleteLayout(layout._id, layout._rev!) await API.deleteLayout(layout._id, layout._rev)
this.update(state => { this.update(state => {
state.layouts = state.layouts.filter(x => x._id !== layout._id) state.layouts = state.layouts.filter(x => x._id !== layout._id)
return state return state
@ -75,6 +79,24 @@ export class LayoutStore extends BudiStore<LayoutState> {
export const layoutStore = new LayoutStore() export const layoutStore = new LayoutStore()
export const selectedLayout = derived(layoutStore, $store => { export class SelectedLayoutStore extends DerivedBudiStore<
return $store.layouts?.find(layout => layout._id === $store.selectedLayoutId) LayoutState,
DerivedLayoutState
> {
constructor(layoutStore: LayoutStore) {
const makeDerivedStore = () => {
return derived(layoutStore, $store => {
return {
...$store,
selectedLayout:
$store.layouts?.find(
layout => layout._id === $store.selectedLayoutId
) || null,
}
}) })
}
super(INITIAL_LAYOUT_STATE, makeDerivedStore)
}
}
export const selectedLayout = new SelectedLayoutStore(layoutStore)