fix not being able to update roles twice

This commit is contained in:
Keviin Åberg Kultalahti 2021-05-17 13:23:19 +02:00
parent 24d84ee2b4
commit 4d9e4ca426
2 changed files with 22 additions and 10 deletions

View File

@ -31,16 +31,16 @@
// Here we need to merge the Apps list and the roles response to get something that makes sense for the table // Here we need to merge the Apps list and the roles response to get something that makes sense for the table
$: appList = $apps.map(app => ({ $: appList = $apps.map(app => ({
...app, ...app,
role: $request?.data?.roles?.[app._id], role: $roleFetch?.data?.roles?.[app._id],
})) }))
let selectedApp let selectedApp
const request = fetchData(`/api/admin/users/${userId}`) const roleFetch = fetchData(`/api/admin/users/${userId}`)
async function deleteUser() { async function deleteUser() {
const res = await users.del(userId) const res = await users.del(userId)
if (res.message) { if (res.message) {
notifications.success(`User ${$request?.data?.email} deleted.`) notifications.success(`User ${$roleFetch?.data?.email} deleted.`)
$goto("./") $goto("./")
} else { } else {
notifications.error("Failed to delete user.") notifications.error("Failed to delete user.")
@ -61,7 +61,7 @@
</div> </div>
<div class="heading"> <div class="heading">
<Layout noPadding gap="XS"> <Layout noPadding gap="XS">
<Heading>User: {$request?.data?.email}</Heading> <Heading>User: {$roleFetch?.data?.email}</Heading>
<Body <Body
>Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis porro >Lorem ipsum dolor sit amet consectetur adipisicing elit. Debitis porro
ut nesciunt ipsam perspiciatis aliquam et hic minus alias beatae. Odit ut nesciunt ipsam perspiciatis aliquam et hic minus alias beatae. Odit
@ -76,7 +76,7 @@
<div class="fields"> <div class="fields">
<div class="field"> <div class="field">
<Label size="L">Email</Label> <Label size="L">Email</Label>
<Input disabled thin value={$request?.data?.email} /> <Input disabled thin value={$roleFetch?.data?.email} />
</div> </div>
</div> </div>
<div class="regenerate"> <div class="regenerate">
@ -119,13 +119,17 @@
showCloseIcon={false} showCloseIcon={false}
> >
<Body <Body
>Are you sure you want to delete <strong>{$request?.data?.email}</strong >Are you sure you want to delete <strong>{$roleFetch?.data?.email}</strong
></Body ></Body
> >
</ModalContent> </ModalContent>
</Modal> </Modal>
<Modal bind:this={editRolesModal}> <Modal bind:this={editRolesModal}>
<UpdateRolesModal app={selectedApp} user={$request.data} /> <UpdateRolesModal
app={selectedApp}
user={$roleFetch.data}
on:update={roleFetch.refresh}
/>
</Modal> </Modal>
<style> <style>

View File

@ -1,4 +1,5 @@
<script> <script>
import { createEventDispatcher } from "svelte"
import { Body, Select, ModalContent, notifications } from "@budibase/bbui" import { Body, Select, ModalContent, notifications } from "@budibase/bbui"
import { fetchData } from "helpers" import { fetchData } from "helpers"
import { users } from "stores/portal" import { users } from "stores/portal"
@ -6,19 +7,26 @@
export let app export let app
export let user export let user
const dispatch = createEventDispatcher()
const roles = fetchData(`/api/admin/roles/${app._id}`) const roles = fetchData(`/api/admin/roles/${app._id}`)
$: options = $roles?.data?.roles?.map(role => role._id) $: options = $roles?.data?.roles?.map(role => role._id)
let selectedRole let selectedRole
function updateUserRoles() { async function updateUserRoles() {
users.updateRoles({ const res = await users.updateRoles({
...user, ...user,
roles: { roles: {
...user.roles, ...user.roles,
[app._id]: selectedRole, [app._id]: selectedRole,
}, },
}) })
notifications.success("Roles updated") if (res.status === 400) {
notifications.error("Failed to update role.")
} else {
notifications.success("Roles updated")
dispatch("update")
}
} }
</script> </script>