Merge pull request #15310 from Budibase/builder-store-roles

Convert role and published store to TS
This commit is contained in:
Peter Clement 2025-01-07 11:20:01 +00:00 committed by GitHub
commit eea4cecafc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 100 additions and 91 deletions

View File

@ -1,13 +1,16 @@
import { appStore } from "./app"
import { appsStore } from "@/stores/portal/apps"
import { deploymentStore } from "./deployments"
import { derived } from "svelte/store"
import { derived, type Readable } from "svelte/store"
import { DeploymentProgressResponse, DeploymentStatus } from "@budibase/types"
export const appPublished = derived(
export const appPublished: Readable<boolean> = derived(
[appStore, appsStore, deploymentStore],
([$appStore, $appsStore, $deploymentStore]) => {
const app = $appsStore.apps.find(app => app.devId === $appStore.appId)
const deployments = $deploymentStore.filter(x => x.status === "SUCCESS")
const deployments = $deploymentStore.filter(
(x: DeploymentProgressResponse) => x.status === DeploymentStatus.SUCCESS
)
return app?.status === "published" && deployments.length > 0
}
)

View File

@ -1,88 +0,0 @@
import { derived, writable, get } from "svelte/store"
import { API } from "@/api"
import { RoleUtils } from "@budibase/frontend-core"
export function createRolesStore() {
const store = writable([])
const enriched = derived(store, $store => {
return $store.map(role => ({
...role,
// Ensure we have new metadata for all roles
uiMetadata: {
displayName: role.uiMetadata?.displayName || role.name,
color:
role.uiMetadata?.color || "var(--spectrum-global-color-magenta-400)",
description: role.uiMetadata?.description || "Custom role",
},
}))
})
function setRoles(roles) {
store.set(
roles.sort((a, b) => {
const priorityA = RoleUtils.getRolePriority(a._id)
const priorityB = RoleUtils.getRolePriority(b._id)
if (priorityA !== priorityB) {
return priorityA > priorityB ? -1 : 1
}
const nameA = a.uiMetadata?.displayName || a.name
const nameB = b.uiMetadata?.displayName || b.name
return nameA < nameB ? -1 : 1
})
)
}
const actions = {
fetch: async () => {
const roles = await API.getRoles()
setRoles(roles)
},
fetchByAppId: async appId => {
const { roles } = await API.getRolesForApp(appId)
setRoles(roles)
},
delete: async role => {
await API.deleteRole(role._id, role._rev)
await actions.fetch()
},
save: async role => {
const savedRole = await API.saveRole(role)
await actions.fetch()
return savedRole
},
replace: (roleId, role) => {
// Handles external updates of roles
if (!roleId) {
return
}
// Handle deletion
if (!role) {
store.update(state => state.filter(x => x._id !== roleId))
return
}
// Add new role
const index = get(store).findIndex(x => x._id === role._id)
if (index === -1) {
store.update(state => [...state, role])
}
// Update existing role
else if (role) {
store.update(state => {
state[index] = role
return [...state]
})
}
},
}
return {
subscribe: enriched.subscribe,
...actions,
}
}
export const roles = createRolesStore()

View File

@ -0,0 +1,94 @@
import { derived, get, type Writable } from "svelte/store"
import { API } from "@/api"
import { RoleUtils } from "@budibase/frontend-core"
import { DerivedBudiStore } from "../BudiStore"
import { Role } from "@budibase/types"
export class RoleStore extends DerivedBudiStore<Role[], Role[]> {
constructor() {
const makeDerivedStore = (store: Writable<Role[]>) =>
derived(store, $store => {
return $store.map((role: Role) => ({
...role,
// Ensure we have new metadata for all roles
uiMetadata: {
displayName: role.uiMetadata?.displayName || role.name,
color:
role.uiMetadata?.color ||
"var(--spectrum-global-color-magenta-400)",
description: role.uiMetadata?.description || "Custom role",
},
}))
})
super([], makeDerivedStore)
}
setRoles = (roles: Role[]) => {
this.set(
roles.sort((a, b) => {
// Ensure we have valid IDs for priority comparison
const priorityA = RoleUtils.getRolePriority(a._id)
const priorityB = RoleUtils.getRolePriority(b._id)
if (priorityA !== priorityB) {
return priorityA > priorityB ? -1 : 1
}
const nameA = a.uiMetadata?.displayName || a.name
const nameB = b.uiMetadata?.displayName || b.name
return nameA < nameB ? -1 : 1
})
)
}
fetch = async () => {
const roles = await API.getRoles()
this.setRoles(roles)
}
fetchByAppId = async (appId: string) => {
const { roles } = await API.getRolesForApp(appId)
this.setRoles(roles)
}
delete = async (role: Role) => {
if (!role._id || !role._rev) {
return
}
await API.deleteRole(role._id, role._rev)
await this.fetch()
}
save = async (role: Role) => {
const savedRole = await API.saveRole(role)
await this.fetch()
return savedRole
}
replace = (roleId: string, role?: Role) => {
// Handles external updates of roles
if (!roleId) {
return
}
// Handle deletion
if (!role) {
this.update(state => state.filter(x => x._id !== roleId))
return
}
// Add new role
const index = get(this).findIndex(x => x._id === role._id)
if (index === -1) {
this.update(state => [...state, role])
}
// Update existing role
else if (role) {
this.update(state => {
state[index] = role
return [...state]
})
}
}
}
export const roles = new RoleStore()