Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing viewV2 store #15224

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 0 additions & 102 deletions packages/builder/src/stores/builder/viewsV2.js

This file was deleted.

122 changes: 122 additions & 0 deletions packages/builder/src/stores/builder/viewsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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<BuilderViewV2Store>) => {
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(
{
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()
4 changes: 2 additions & 2 deletions packages/server/src/sdk/app/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ 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 function isV2(view: View | ViewV2) {
return helpers.views.isV2(view)
}

export async function remove(viewId: string): Promise<ViewV2> {
Expand Down
5 changes: 5 additions & 0 deletions packages/shared-core/src/helpers/views.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BasicViewFieldMetadata,
View,
ViewCalculationFieldMetadata,
ViewFieldMetadata,
ViewV2,
Expand Down Expand Up @@ -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
}
Loading