Skip to content

Commit

Permalink
Merge pull request #15330 from Budibase/builder-store-conversions-pc
Browse files Browse the repository at this point in the history
Convert views and published stores to TS
  • Loading branch information
PClmnt authored Jan 15, 2025
2 parents 4c1013e + 1c25113 commit bd9a26b
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 94 deletions.
27 changes: 0 additions & 27 deletions packages/builder/src/stores/builder/permissions.js

This file was deleted.

50 changes: 50 additions & 0 deletions packages/builder/src/stores/builder/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BudiStore } from "../BudiStore"
import { API } from "@/api"
import {
PermissionLevel,
GetResourcePermsResponse,
GetDependantResourcesResponse,
ResourcePermissionInfo,
} from "@budibase/types"

interface Permission {
level: PermissionLevel
role: string
resource: string
}

export class PermissionStore extends BudiStore<Permission[]> {
constructor() {
super([])
}

save = async (permission: Permission) => {
const { level, role, resource } = permission
return await API.updatePermissionForResource(resource, role, level)
}

remove = async (permission: Permission) => {
const { level, role, resource } = permission
return await API.removePermissionFromResource(resource, role, level)
}

forResource = async (
resourceId: string
): Promise<Record<string, ResourcePermissionInfo>> => {
return (await API.getPermissionForResource(resourceId)).permissions
}

forResourceDetailed = async (
resourceId: string
): Promise<GetResourcePermsResponse> => {
return await API.getPermissionForResource(resourceId)
}

getDependantsInfo = async (
resourceId: string
): Promise<GetDependantResourcesResponse> => {
return await API.getDependants(resourceId)
}
}

export const permissions = new PermissionStore()
67 changes: 0 additions & 67 deletions packages/builder/src/stores/builder/views.js

This file was deleted.

94 changes: 94 additions & 0 deletions packages/builder/src/stores/builder/views.ts
Original file line number Diff line number Diff line change
@@ -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) {
throw new Error("View name is required")
}
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) {
throw new Error("View name is required")
}

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()

0 comments on commit bd9a26b

Please sign in to comment.