|
| 1 | +import { derived, get, Writable } from "svelte/store" |
| 2 | +import { tables } from "./tables" |
| 3 | +import { API } from "api" |
| 4 | +import { DerivedBudiStore } from "stores/BudiStore" |
| 5 | +import { CreateViewRequest, UpdateViewRequest, ViewV2 } from "@budibase/types" |
| 6 | +import { helpers } from "@budibase/shared-core" |
| 7 | + |
| 8 | +interface BuilderViewV2Store { |
| 9 | + selectedViewId: string | null |
| 10 | +} |
| 11 | + |
| 12 | +interface DerivedViewV2Store extends BuilderViewV2Store { |
| 13 | + list: ViewV2[] |
| 14 | + selected?: ViewV2 |
| 15 | +} |
| 16 | + |
| 17 | +export class ViewV2Store extends DerivedBudiStore< |
| 18 | + BuilderViewV2Store, |
| 19 | + DerivedViewV2Store |
| 20 | +> { |
| 21 | + constructor() { |
| 22 | + const makeDerivedStore = (store: Writable<BuilderViewV2Store>) => { |
| 23 | + return derived( |
| 24 | + [store, tables], |
| 25 | + ([$store, $tables]): DerivedViewV2Store => { |
| 26 | + let list: ViewV2[] = [] |
| 27 | + $tables.list?.forEach(table => { |
| 28 | + const views = Object.values(table?.views || {}).filter( |
| 29 | + helpers.views.isV2 |
| 30 | + ) |
| 31 | + list = list.concat(views) |
| 32 | + }) |
| 33 | + return { |
| 34 | + ...$store, |
| 35 | + list, |
| 36 | + selected: list.find(view => view.id === $store.selectedViewId), |
| 37 | + } |
| 38 | + } |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + super( |
| 43 | + { |
| 44 | + selectedViewId: null, |
| 45 | + }, |
| 46 | + makeDerivedStore |
| 47 | + ) |
| 48 | + |
| 49 | + this.select = this.select.bind(this) |
| 50 | + } |
| 51 | + |
| 52 | + select(id: string) { |
| 53 | + this.store.update(state => ({ |
| 54 | + ...state, |
| 55 | + selectedViewId: id, |
| 56 | + })) |
| 57 | + } |
| 58 | + |
| 59 | + async delete(view: { id: string }) { |
| 60 | + await API.viewV2.delete(view.id) |
| 61 | + this.replaceView(view.id, null) |
| 62 | + } |
| 63 | + |
| 64 | + async create(view: CreateViewRequest) { |
| 65 | + const savedViewResponse = await API.viewV2.create(view) |
| 66 | + const savedView = savedViewResponse.data |
| 67 | + this.replaceView(savedView.id, savedView) |
| 68 | + return savedView |
| 69 | + } |
| 70 | + |
| 71 | + async save(view: UpdateViewRequest) { |
| 72 | + const res = await API.viewV2.update(view) |
| 73 | + const savedView = res?.data |
| 74 | + this.replaceView(view.id, savedView) |
| 75 | + } |
| 76 | + |
| 77 | + // Handles external updates of tables |
| 78 | + replaceView(viewId: string, view: ViewV2 | null) { |
| 79 | + const existingView = get(this.derivedStore).list.find( |
| 80 | + view => view.id === viewId |
| 81 | + ) |
| 82 | + const tableIndex = get(tables).list.findIndex(table => { |
| 83 | + return table._id === view?.tableId || table._id === existingView?.tableId |
| 84 | + }) |
| 85 | + if (tableIndex === -1) { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + // Handle deletion |
| 90 | + if (!view && existingView) { |
| 91 | + tables.update(state => { |
| 92 | + delete state.list[tableIndex].views![existingView.name] |
| 93 | + return state |
| 94 | + }) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Add new view |
| 99 | + else if (!existingView && view) { |
| 100 | + tables.update(state => { |
| 101 | + state.list[tableIndex].views ??= {} |
| 102 | + state.list[tableIndex].views[view.name] = view |
| 103 | + return state |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + // Update existing view |
| 108 | + else if (existingView && view) { |
| 109 | + tables.update(state => { |
| 110 | + // Remove old view |
| 111 | + state.list[tableIndex].views ??= {} |
| 112 | + delete state.list[tableIndex].views[existingView.name] |
| 113 | + |
| 114 | + // Add new view |
| 115 | + state.list[tableIndex].views[view.name] = view |
| 116 | + return state |
| 117 | + }) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export const viewsV2 = new ViewV2Store() |
0 commit comments