From 5f353a85f7f9668d59e6cd9fa1e01a0a8e7891ff Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Wed, 8 Jan 2025 10:55:11 +0000 Subject: [PATCH 1/4] permissions store --- .../builder/src/stores/builder/permissions.js | 27 ----------- .../builder/src/stores/builder/permissions.ts | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 27 deletions(-) delete mode 100644 packages/builder/src/stores/builder/permissions.js create mode 100644 packages/builder/src/stores/builder/permissions.ts diff --git a/packages/builder/src/stores/builder/permissions.js b/packages/builder/src/stores/builder/permissions.js deleted file mode 100644 index a303cd713b..0000000000 --- a/packages/builder/src/stores/builder/permissions.js +++ /dev/null @@ -1,27 +0,0 @@ -import { writable } from "svelte/store" -import { API } from "@/api" - -export function createPermissionStore() { - const { subscribe } = writable([]) - - return { - subscribe, - save: async ({ level, role, resource }) => { - return await API.updatePermissionForResource(resource, role, level) - }, - remove: async ({ level, role, resource }) => { - return await API.removePermissionFromResource(resource, role, level) - }, - forResource: async resourceId => { - return (await API.getPermissionForResource(resourceId)).permissions - }, - forResourceDetailed: async resourceId => { - return await API.getPermissionForResource(resourceId) - }, - getDependantsInfo: async resourceId => { - return await API.getDependants(resourceId) - }, - } -} - -export const permissions = createPermissionStore() diff --git a/packages/builder/src/stores/builder/permissions.ts b/packages/builder/src/stores/builder/permissions.ts new file mode 100644 index 0000000000..002b73893e --- /dev/null +++ b/packages/builder/src/stores/builder/permissions.ts @@ -0,0 +1,47 @@ +import { BudiStore } from "../BudiStore" +import { API } from "@/api" +import { + PermissionLevel, + GetResourcePermsResponse, + GetDependantResourcesResponse, +} from "@budibase/types" + +interface Permission { + level: PermissionLevel + role: string + resource: string +} + +export class PermissionStore extends BudiStore { + 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 => { + return (await API.getPermissionForResource(resourceId)).permissions + } + + forResourceDetailed = async ( + resourceId: string + ): Promise => { + return await API.getPermissionForResource(resourceId) + } + + getDependantsInfo = async ( + resourceId: string + ): Promise => { + return await API.getDependants(resourceId) + } +} + +export const permissions = new PermissionStore() From a983292865fee9980342d23af16a8d9ed20c6a64 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Wed, 8 Jan 2025 11:14:56 +0000 Subject: [PATCH 2/4] fix return type --- packages/builder/src/stores/builder/permissions.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/stores/builder/permissions.ts b/packages/builder/src/stores/builder/permissions.ts index 002b73893e..6056449150 100644 --- a/packages/builder/src/stores/builder/permissions.ts +++ b/packages/builder/src/stores/builder/permissions.ts @@ -4,6 +4,7 @@ import { PermissionLevel, GetResourcePermsResponse, GetDependantResourcesResponse, + ResourcePermissionInfo, } from "@budibase/types" interface Permission { @@ -27,7 +28,9 @@ export class PermissionStore extends BudiStore { return await API.removePermissionFromResource(resource, role, level) } - forResource = async (resourceId: string): Promise => { + forResource = async ( + resourceId: string + ): Promise> => { return (await API.getPermissionForResource(resourceId)).permissions } From b6418c333cf422857f64db84f963f96e12a061d4 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Thu, 9 Jan 2025 09:20:16 +0000 Subject: [PATCH 3/4] views v1 --- packages/builder/src/stores/builder/views.js | 67 -------------- packages/builder/src/stores/builder/views.ts | 94 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 67 deletions(-) delete mode 100644 packages/builder/src/stores/builder/views.js create mode 100644 packages/builder/src/stores/builder/views.ts diff --git a/packages/builder/src/stores/builder/views.js b/packages/builder/src/stores/builder/views.js deleted file mode 100644 index 07c356f56d..0000000000 --- a/packages/builder/src/stores/builder/views.js +++ /dev/null @@ -1,67 +0,0 @@ -import { writable, derived } from "svelte/store" -import { tables } from "./tables" -import { API } from "@/api" - -export function createViewsStore() { - const store = writable({ - selectedViewName: 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.name === $store.selectedViewName), - } - }) - - const select = name => { - store.update(state => ({ - ...state, - selectedViewName: name, - })) - } - - const deleteView = async view => { - await API.deleteView(view.name) - - // Update tables - tables.update(state => { - const table = state.list.find(table => table._id === view.tableId) - delete table.views[view.name] - return { ...state } - }) - } - - const save = async view => { - const savedView = await API.saveView(view) - select(view.name) - - // Update tables - tables.update(state => { - const table = state.list.find(table => table._id === view.tableId) - if (table) { - if (view.originalName) { - delete table.views[view.originalName] - } - table.views[view.name] = savedView - } - return { ...state } - }) - } - - return { - subscribe: derivedStore.subscribe, - select, - delete: deleteView, - save, - } -} - -export const views = createViewsStore() diff --git a/packages/builder/src/stores/builder/views.ts b/packages/builder/src/stores/builder/views.ts new file mode 100644 index 0000000000..4e309e180d --- /dev/null +++ b/packages/builder/src/stores/builder/views.ts @@ -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) => { + 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) { + return + } + 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) { + return + } + + 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() From 0bc1e33ef3e79c3edc9485c0017674f87e2f6d16 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Mon, 13 Jan 2025 18:20:22 +0000 Subject: [PATCH 4/4] pr comments --- packages/builder/src/stores/builder/views.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/stores/builder/views.ts b/packages/builder/src/stores/builder/views.ts index 4e309e180d..81085fcb42 100644 --- a/packages/builder/src/stores/builder/views.ts +++ b/packages/builder/src/stores/builder/views.ts @@ -55,7 +55,7 @@ export class ViewsStore extends DerivedBudiStore< delete = async (view: View) => { if (!view.name) { - return + throw new Error("View name is required") } await API.deleteView(view.name) @@ -71,7 +71,7 @@ export class ViewsStore extends DerivedBudiStore< save = async (view: View & { originalName?: string }) => { if (!view.name) { - return + throw new Error("View name is required") } const savedView = await API.saveView(view)