From db9cf128f0f0e5a93f18c150216fc03fc099799f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 19 Dec 2024 19:46:58 +0100 Subject: [PATCH 1/4] Is viewV2 to shared core --- packages/server/src/sdk/app/views/index.ts | 4 +--- packages/shared-core/src/helpers/views.ts | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index f483ebc0bc..ad3399d54b 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -324,9 +324,7 @@ export async function update( return pickApi(tableId).update(tableId, view) } -export function isV2(view: View | ViewV2): view is ViewV2 { - return (view as ViewV2).version === 2 -} +export const isV2 = helpers.views.isV2 export async function remove(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) diff --git a/packages/shared-core/src/helpers/views.ts b/packages/shared-core/src/helpers/views.ts index f0407eeec9..106f8cb6eb 100644 --- a/packages/shared-core/src/helpers/views.ts +++ b/packages/shared-core/src/helpers/views.ts @@ -1,5 +1,6 @@ import { BasicViewFieldMetadata, + View, ViewCalculationFieldMetadata, ViewFieldMetadata, ViewV2, @@ -43,3 +44,7 @@ export function basicFields(view: UnsavedViewV2, opts?: { visible?: boolean }) { return !isCalculationField(field) && (!visible || isVisible(field)) }) } + +export function isV2(view: View | ViewV2): view is ViewV2 { + return (view as ViewV2).version === 2 +} From dd29dcedc8e7d7b11f7e37f2f5ed001f33c36461 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 19 Dec 2024 21:09:49 +0100 Subject: [PATCH 2/4] Type viewv2 store --- .../builder/src/stores/builder/viewsV2.js | 102 --------------- .../builder/src/stores/builder/viewsV2.ts | 119 ++++++++++++++++++ 2 files changed, 119 insertions(+), 102 deletions(-) delete mode 100644 packages/builder/src/stores/builder/viewsV2.js create mode 100644 packages/builder/src/stores/builder/viewsV2.ts diff --git a/packages/builder/src/stores/builder/viewsV2.js b/packages/builder/src/stores/builder/viewsV2.js deleted file mode 100644 index 9bd32f4a24..0000000000 --- a/packages/builder/src/stores/builder/viewsV2.js +++ /dev/null @@ -1,102 +0,0 @@ -import { writable, derived, get } from "svelte/store" -import { tables } from "./tables" -import { API } from "api" - -export function createViewsV2Store() { - const store = writable({ - selectedViewId: 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.id === $store.selectedViewId), - } - }) - - const select = id => { - store.update(state => ({ - ...state, - selectedViewId: id, - })) - } - - const deleteView = async view => { - await API.viewV2.delete(view.id) - replaceView(view.id, null) - } - - const create = async view => { - const savedViewResponse = await API.viewV2.create(view) - const savedView = savedViewResponse.data - replaceView(savedView.id, savedView) - return savedView - } - - const save = async view => { - const res = await API.viewV2.update(view) - const savedView = res?.data - replaceView(view.id, savedView) - } - - // Handles external updates of tables - const replaceView = (viewId, view) => { - if (!viewId) { - return - } - const existingView = get(derivedStore).list.find(view => view.id === viewId) - const tableIndex = get(tables).list.findIndex(table => { - return table._id === view?.tableId || table._id === existingView?.tableId - }) - if (tableIndex === -1) { - return - } - - // Handle deletion - if (!view) { - tables.update(state => { - delete state.list[tableIndex].views[existingView.name] - return state - }) - return - } - - // Add new view - if (!existingView) { - tables.update(state => { - state.list[tableIndex].views[view.name] = view - return state - }) - } - - // Update existing view - else { - tables.update(state => { - // Remove old view - delete state.list[tableIndex].views[existingView.name] - - // Add new view - state.list[tableIndex].views[view.name] = view - return state - }) - } - } - - return { - subscribe: derivedStore.subscribe, - select, - delete: deleteView, - create, - save, - replaceView, - } -} - -export const viewsV2 = createViewsV2Store() diff --git a/packages/builder/src/stores/builder/viewsV2.ts b/packages/builder/src/stores/builder/viewsV2.ts new file mode 100644 index 0000000000..ed1bdeb008 --- /dev/null +++ b/packages/builder/src/stores/builder/viewsV2.ts @@ -0,0 +1,119 @@ +import { derived, get, Writable } from "svelte/store" +import { tables } from "./tables" +import { API } from "api" +import { DerivedBudiStore } from "stores/BudiStore" +import { CreateViewRequest, UpdateViewRequest, ViewV2 } from "@budibase/types" +import { helpers } from "@budibase/shared-core" + +interface BuilderViewV2Store { + selectedViewId: string | null +} + +interface DerivedViewV2Store extends BuilderViewV2Store { + list: ViewV2[] + selected: ViewV2 +} + +export class ViewV2Store extends DerivedBudiStore< + BuilderViewV2Store, + DerivedViewV2Store +> { + constructor() { + const makeDerivedStore = (store: Writable) => { + return derived([store, tables], ([$store, $tables]) => { + let list: ViewV2[] = [] + $tables.list?.forEach(table => { + const views = Object.values(table?.views || {}).filter( + helpers.views.isV2 + ) + list = list.concat(views) + }) + return { + ...$store, + list, + selected: list.find(view => view.id === $store.selectedViewId), + } + }) + } + + super( + { + selectedViewId: null, + }, + makeDerivedStore + ) + + this.select = this.select.bind(this) + } + + select(id: string) { + this.store.update(state => ({ + ...state, + selectedViewId: id, + })) + } + + async delete(view: { id: string }) { + await API.viewV2.delete(view.id) + this.replaceView(view.id, null) + } + + async create(view: CreateViewRequest) { + const savedViewResponse = await API.viewV2.create(view) + const savedView = savedViewResponse.data + this.replaceView(savedView.id, savedView) + return savedView + } + + async save(view: UpdateViewRequest) { + const res = await API.viewV2.update(view) + const savedView = res?.data + this.replaceView(view.id, savedView) + } + + // Handles external updates of tables + replaceView(viewId: string, view: ViewV2 | null) { + const existingView = get(this.derivedStore).list.find( + view => view.id === viewId + ) + const tableIndex = get(tables).list.findIndex(table => { + return table._id === view?.tableId || table._id === existingView?.tableId + }) + if (tableIndex === -1) { + return + } + + // Handle deletion + if (!view && existingView) { + tables.update(state => { + delete state.list[tableIndex].views![existingView.name] + return state + }) + return + } + + // Add new view + else if (!existingView && view) { + tables.update(state => { + state.list[tableIndex].views ??= {} + state.list[tableIndex].views[view.name] = view + return state + }) + } + + // Update existing view + else if (existingView && view) { + tables.update(state => { + // Remove old view + state.list[tableIndex].views ??= {} + delete state.list[tableIndex].views[existingView.name] + + // Add new view + state.list[tableIndex].views[view.name] = view + return state + }) + } + } +} + +export const viewsV2 = new ViewV2Store() From e00002757e9e48eaf7c14f41496b44ba31af1108 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 11:10:06 +0100 Subject: [PATCH 3/4] Fix types --- .../builder/src/stores/builder/viewsV2.ts | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/builder/src/stores/builder/viewsV2.ts b/packages/builder/src/stores/builder/viewsV2.ts index ed1bdeb008..07cb606eaf 100644 --- a/packages/builder/src/stores/builder/viewsV2.ts +++ b/packages/builder/src/stores/builder/viewsV2.ts @@ -11,7 +11,7 @@ interface BuilderViewV2Store { interface DerivedViewV2Store extends BuilderViewV2Store { list: ViewV2[] - selected: ViewV2 + selected?: ViewV2 } export class ViewV2Store extends DerivedBudiStore< @@ -20,20 +20,23 @@ export class ViewV2Store extends DerivedBudiStore< > { constructor() { const makeDerivedStore = (store: Writable) => { - return derived([store, tables], ([$store, $tables]) => { - let list: ViewV2[] = [] - $tables.list?.forEach(table => { - const views = Object.values(table?.views || {}).filter( - helpers.views.isV2 - ) - list = list.concat(views) - }) - return { - ...$store, - list, - selected: list.find(view => view.id === $store.selectedViewId), + return derived( + [store, tables], + ([$store, $tables]): DerivedViewV2Store => { + let list: ViewV2[] = [] + $tables.list?.forEach(table => { + const views = Object.values(table?.views || {}).filter( + helpers.views.isV2 + ) + list = list.concat(views) + }) + return { + ...$store, + list, + selected: list.find(view => view.id === $store.selectedViewId), + } } - }) + ) } super( From 15ad884ac8e832053e2fe7ebf5204e59c9e11e59 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 20 Dec 2024 11:21:25 +0100 Subject: [PATCH 4/4] Fix test order --- packages/server/src/sdk/app/views/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index ad3399d54b..7fc78b9085 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -324,7 +324,9 @@ export async function update( return pickApi(tableId).update(tableId, view) } -export const isV2 = helpers.views.isV2 +export function isV2(view: View | ViewV2) { + return helpers.views.isV2(view) +} export async function remove(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId)