1
0
Fork 0
mirror of synced 2024-10-03 02:27:06 +13:00

moves roles in backendstore to a separate store

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-22 10:55:23 +01:00
parent 2ddb2e3fbb
commit 98607ca1bd
10 changed files with 50 additions and 43 deletions

View file

@ -1,12 +1,10 @@
import { writable, get } from "svelte/store" import { writable, get } from "svelte/store"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import api from "../api" import api from "../api"
import { permissions } from './backend/permissions'
const INITIAL_BACKEND_UI_STATE = { const INITIAL_BACKEND_UI_STATE = {
tables: [], tables: [],
views: [], views: [],
roles: [],
datasources: [], datasources: [],
queries: [], queries: [],
integrations: {}, integrations: {},
@ -50,11 +48,6 @@ export const getBackendUiStore = () => {
state.selectedView = state.selectedView state.selectedView = state.selectedView
return state return state
}), }),
select: row =>
store.update(state => {
state.selectedRow = row
return state
}),
}, },
datasources: { datasources: {
fetch: async () => { fetch: async () => {
@ -301,26 +294,6 @@ export const getBackendUiStore = () => {
}) })
}, },
}, },
roles: {
fetch: async () => {
const response = await api.get("/api/roles")
const roles = await response.json()
store.update(state => {
state.roles = roles
return state
})
},
delete: async role => {
const response = await api.delete(`/api/roles/${role._id}/${role._rev}`)
await store.actions.roles.fetch()
return response
},
save: async role => {
const response = await api.post("/api/roles", role)
await store.actions.roles.fetch()
return response
},
},
permissions: { permissions: {
fetchLevels: async () => { fetchLevels: async () => {
const response = await api.get("/api/permission/levels") const response = await api.get("/api/permission/levels")

View file

@ -0,0 +1,30 @@
import { writable } from "svelte/store"
import api from "../../api"
function createRolesStore() {
const { subscribe, set } = writable([])
return {
subscribe,
fetch: async () => {
set(await getRoles())
},
delete: async role => {
const response = await api.delete(`/api/roles/${role._id}/${role._rev}`)
set(await getRoles())
return response
},
save: async role => {
const response = await api.post("/api/roles", role)
set(await getRoles())
return response
},
}
}
async function getRoles() {
const response = await api.get("/api/roles")
return await response.json()
}
export const roles = createRolesStore()

View file

@ -1,9 +1,9 @@
<script> <script>
import { backendUiStore } from "builderStore" import { roles } from 'builderStore/store/backend/'
export let roleId export let roleId
$: role = $backendUiStore.roles.find(role => role._id === roleId) $: role = $roles.find(role => role._id === roleId)
$: roleName = role?.name ?? "Unknown role" $: roleName = role?.name ?? "Unknown role"
</script> </script>

View file

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { roles } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import RowFieldControl from "../RowFieldControl.svelte" import RowFieldControl from "../RowFieldControl.svelte"
import * as backendApi from "../api" import * as backendApi from "../api"
@ -91,7 +92,7 @@
data-cy="roleId-select" data-cy="roleId-select"
bind:value={row.roleId}> bind:value={row.roleId}>
<option value="">Choose an option</option> <option value="">Choose an option</option>
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -4,14 +4,14 @@
import api from "builderStore/api" import api from "builderStore/api"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import ErrorsBox from "components/common/ErrorsBox.svelte" import ErrorsBox from "components/common/ErrorsBox.svelte"
import { backendUiStore } from "builderStore" import { roles } from 'builderStore/store/backend/'
let basePermissions = [] let basePermissions = []
let selectedRole = {} let selectedRole = {}
let errors = [] let errors = []
let builtInRoles = ["Admin", "Power", "Basic", "Public"] let builtInRoles = ["Admin", "Power", "Basic", "Public"]
$: selectedRoleId = selectedRole._id $: selectedRoleId = selectedRole._id
$: otherRoles = $backendUiStore.roles.filter( $: otherRoles = $roles.filter(
role => role._id !== selectedRoleId role => role._id !== selectedRoleId
) )
$: isCreating = selectedRoleId == null || selectedRoleId === "" $: isCreating = selectedRoleId == null || selectedRoleId === ""
@ -24,7 +24,7 @@
// Changes the selected role // Changes the selected role
const changeRole = event => { const changeRole = event => {
const id = event?.target?.value const id = event?.target?.value
const role = $backendUiStore.roles.find(role => role._id === id) const role = $roles.find(role => role._id === id)
if (role) { if (role) {
selectedRole = { selectedRole = {
...role, ...role,
@ -61,7 +61,7 @@
} }
// Save/create the role // Save/create the role
const response = await backendUiStore.actions.roles.save(selectedRole) const response = await roles.save(selectedRole)
if (response.status === 200) { if (response.status === 200) {
notifier.success("Role saved successfully.") notifier.success("Role saved successfully.")
} else { } else {
@ -72,7 +72,7 @@
// Deletes the selected role // Deletes the selected role
const deleteRole = async () => { const deleteRole = async () => {
const response = await backendUiStore.actions.roles.delete(selectedRole) const response = await roles.delete(selectedRole)
if (response.status === 200) { if (response.status === 200) {
changeRole() changeRole()
notifier.success("Role deleted successfully.") notifier.success("Role deleted successfully.")
@ -98,7 +98,7 @@
value={selectedRoleId} value={selectedRoleId}
on:change={changeRole}> on:change={changeRole}>
<option value="">Create new role</option> <option value="">Create new role</option>
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { roles } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { Button, Label, Input, Select, Spacer } from "@budibase/bbui" import { Button, Label, Input, Select, Spacer } from "@budibase/bbui"
@ -42,7 +43,7 @@
thin thin
value={permissions[level]} value={permissions[level]}
on:change={e => changePermission(level, e.target.value)}> on:change={e => changePermission(level, e.target.value)}>
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -4,10 +4,10 @@
import { import {
store, store,
allScreens, allScreens,
backendUiStore,
selectedAccessRole, selectedAccessRole,
screenSearchString, screenSearchString,
} from "builderStore" } from "builderStore"
import { roles } from 'builderStore/store/backend/'
import { FrontendTypes } from "constants" import { FrontendTypes } from "constants"
import ComponentNavigationTree from "components/design/NavigationPanel/ComponentNavigationTree/index.svelte" import ComponentNavigationTree from "components/design/NavigationPanel/ComponentNavigationTree/index.svelte"
import Layout from "components/design/NavigationPanel/Layout.svelte" import Layout from "components/design/NavigationPanel/Layout.svelte"
@ -81,7 +81,7 @@
on:change={updateAccessRole} on:change={updateAccessRole}
value={$selectedAccessRole} value={$selectedAccessRole}
label="Filter by Access"> label="Filter by Access">
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -1,5 +1,6 @@
<script> <script>
import { store, backendUiStore, allScreens } from "builderStore" import { store, backendUiStore, allScreens } from "builderStore"
import { roles } from 'builderStore/store/backend/'
import { Input, Select, ModalContent, Toggle } from "@budibase/bbui" import { Input, Select, ModalContent, Toggle } from "@budibase/bbui"
import getTemplates from "builderStore/store/screenTemplates" import getTemplates from "builderStore/store/screenTemplates"
import analytics from "analytics" import analytics from "analytics"
@ -105,7 +106,7 @@
bind:value={route} bind:value={route}
on:change={routeChanged} /> on:change={routeChanged} />
<Select label="Access" bind:value={roleId} secondary> <Select label="Access" bind:value={roleId} secondary>
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -1,13 +1,13 @@
<script> <script>
import { Select } from "@budibase/bbui" import { Select } from "@budibase/bbui"
import { backendUiStore } from "builderStore" import { roles } from 'builderStore/store/backend/'
export let value export let value
</script> </script>
<Select bind:value extraThin secondary on:change> <Select bind:value extraThin secondary on:change>
<option value="">Choose an option</option> <option value="">Choose an option</option>
{#each $backendUiStore.roles as role} {#each $roles as role}
<option value={role._id}>{role.name}</option> <option value={role._id}>{role.name}</option>
{/each} {/each}
</Select> </Select>

View file

@ -1,5 +1,6 @@
<script> <script>
import { store, automationStore, backendUiStore } from "builderStore" import { store, automationStore, backendUiStore } from "builderStore"
import { roles } from 'builderStore/store/backend/'
import { Button } from "@budibase/bbui" import { Button } from "@budibase/bbui"
import SettingsLink from "components/settings/Link.svelte" import SettingsLink from "components/settings/Link.svelte"
import ThemeEditorDropdown from "components/settings/ThemeEditorDropdown.svelte" import ThemeEditorDropdown from "components/settings/ThemeEditorDropdown.svelte"
@ -20,7 +21,7 @@
backendUiStore.actions.reset() backendUiStore.actions.reset()
await store.actions.initialise(pkg) await store.actions.initialise(pkg)
await automationStore.actions.fetch() await automationStore.actions.fetch()
await backendUiStore.actions.roles.fetch() await roles.fetch()
return pkg return pkg
} else { } else {
throw new Error(pkg) throw new Error(pkg)