From f5f510a482424a18f0e2e02d2828093ac3dcb637 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 4 Aug 2022 15:33:51 +0100 Subject: [PATCH] Fix multiple issues in user details page --- .../settings/UserGroupPicker.svelte | 2 +- .../portal/manage/groups/[groupId].svelte | 53 +------ .../portal/manage/users/[userId].svelte | 139 ++++++++---------- packages/builder/src/stores/portal/groups.js | 55 ++++++- 4 files changed, 115 insertions(+), 134 deletions(-) diff --git a/packages/builder/src/components/settings/UserGroupPicker.svelte b/packages/builder/src/components/settings/UserGroupPicker.svelte index 67b970d1bf..3d49b8561d 100644 --- a/packages/builder/src/components/settings/UserGroupPicker.svelte +++ b/packages/builder/src/components/settings/UserGroupPicker.svelte @@ -16,7 +16,7 @@ return list.map(item => { return { ...item, - selected: selected.find(x => x._id === item._id) != null, + selected: selected.find(x => x === item._id) != null, } }) } diff --git a/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte b/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte index 45bd8ed1b4..8d7d9c927d 100644 --- a/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte +++ b/packages/builder/src/pages/builder/portal/manage/groups/[groupId].svelte @@ -48,51 +48,6 @@ } } - const adduserToGroup = async id => { - const user = await users.get(id) - if (!user?._id) { - return - } - - // Check we haven't already been added - if (group.users?.find(x => x._id === user._id)) { - return - } - - // Update group - await groups.actions.save({ - ...group, - users: [...group.users, { _id: user._id, email: user.email }], - }) - - // Update user - let userGroups = user.userGroups || [] - userGroups.push(groupId) - await users.save({ - ...user, - userGroups, - }) - } - - const removeUserFromGroup = async id => { - const user = await users.get(id) - if (!user?._id) { - return - } - - // Update group - await groups.actions.save({ - ...group, - users: group.users.filter(x => x._id !== id), - }) - - // Update user - await users.save({ - ...user, - userGroups: user.userGroups.filter(x => x !== groupId), - }) - } - async function fetchUsers(page, search) { if ($pageInfo.loading) { return @@ -191,10 +146,10 @@ user._id)} list={$users.data} - on:select={e => adduserToGroup(e.detail)} - on:deselect={e => removeUserFromGroup(e.detail)} + on:select={e => groups.actions.addUser(groupId, e.detail)} + on:deselect={e => groups.actions.removeUser(groupId, e.detail)} /> @@ -209,7 +164,7 @@ > { - removeUserFromGroup(user._id) + groups.actions.removeUser(groupId, user._id) e.stopPropagation() }} hoverable diff --git a/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte b/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte index 686223ff39..8ce83d93ad 100644 --- a/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte +++ b/packages/builder/src/pages/builder/portal/manage/users/[userId].svelte @@ -22,7 +22,6 @@ StatusLight, } from "@budibase/bbui" import { onMount } from "svelte" - import { fetchData } from "helpers" import { users, auth, groups, apps } from "stores/portal" import { roles } from "stores/backend" import { Constants } from "@budibase/frontend-core" @@ -38,28 +37,23 @@ let popoverAnchor let searchTerm = "" let popover - let selectedGroups = [] let allAppList = [] let user let loaded = false - $: fetchUser(userId) - $: fullName = $userFetch?.data?.firstName - ? $userFetch?.data?.firstName + " " + $userFetch?.data?.lastName - : "" - $: nameLabel = getNameLabel($userFetch) + $: fullName = user?.firstName ? user?.firstName + " " + user?.lastName : "" + $: nameLabel = getNameLabel(user) $: initials = getInitials(nameLabel) + $: filteredGroups = getFilteredGroups($groups, searchTerm) $: allAppList = $apps .filter(x => { - if ($userFetch.data?.roles) { - return Object.keys($userFetch.data.roles).find(y => { - return x.appId === apps.extractAppId(y) - }) - } + return Object.keys(user?.roles || {}).find(y => { + return x.appId === apps.extractAppId(y) + }) }) .map(app => { let roles = Object.fromEntries( - Object.entries($userFetch.data.roles).filter(([key]) => { + Object.entries(user?.roles).filter(([key]) => { return apps.extractAppId(key) === app.appId }) ) @@ -70,27 +64,27 @@ roles, } }) - // Used for searching through groups in the add group popover - $: filteredGroups = $groups.filter( - group => - selectedGroups && - group?.name?.toLowerCase().includes(searchTerm.toLowerCase()) - ) $: userGroups = $groups.filter(x => { return x.users?.find(y => { return y._id === userId }) }) - $: globalRole = $userFetch?.data?.admin?.global + $: globalRole = user?.admin?.global ? "admin" - : $userFetch?.data?.builder?.global + : user?.builder?.global ? "developer" : "appUser" - const userFetch = fetchData(`/api/global/users/${userId}`) + const getFilteredGroups = (groups, search) => { + if (!search) { + return groups + } + search = search.toLowerCase() + return groups.filter(group => group.name?.toLowerCase().includes(search)) + } - const getNameLabel = userFetch => { - const { firstName, lastName, email } = userFetch?.data || {} + const getNameLabel = user => { + const { firstName, lastName, email } = user || {} if (!firstName && !lastName) { return email || "" } @@ -136,24 +130,17 @@ } async function updateUserFirstName(evt) { try { - await users.save({ ...$userFetch?.data, firstName: evt.target.value }) - await userFetch.refresh() + await users.save({ ...user, firstName: evt.target.value }) + await fetchUser() } catch (error) { notifications.error("Error updating user") } } - async function removeGroup(id) { - let updatedGroup = $groups.find(x => x._id === id) - let newUsers = updatedGroup.users.filter(user => user._id !== userId) - updatedGroup.users = newUsers - groups.actions.save(updatedGroup) - } - async function updateUserLastName(evt) { try { - await users.save({ ...$userFetch?.data, lastName: evt.target.value }) - await userFetch.refresh() + await users.save({ ...user, lastName: evt.target.value }) + await fetchUser() } catch (error) { notifications.error("Error updating user") } @@ -169,25 +156,8 @@ } } - async function addGroup(groupId) { - let selectedGroup = selectedGroups.includes(groupId) - let group = $groups.find(group => group._id === groupId) - - if (selectedGroup) { - selectedGroups = selectedGroups.filter(id => id === selectedGroup) - let newUsers = group.users.filter(groupUser => user._id !== groupUser._id) - group.users = newUsers - } else { - selectedGroups = [...selectedGroups, groupId] - group.users.push(user) - } - - await groups.actions.save(group) - } - - async function fetchUser(userId) { - let userPromise = users.get(userId) - user = await userPromise + async function fetchUser() { + user = await users.get(userId) if (!user?._id) { $goto("./") } @@ -195,17 +165,31 @@ async function toggleFlags(detail) { try { - await users.save({ ...$userFetch?.data, ...detail }) - await userFetch.refresh() + await users.save({ ...user, ...detail }) + await fetchUser() } catch (error) { notifications.error("Error updating user") } } - function addAll() {} + const addGroup = async groupId => { + await groups.actions.addUser(groupId, userId) + await fetchUser() + } + + const removeGroup = async groupId => { + await groups.actions.removeUser(groupId, userId) + await fetchUser() + } + onMount(async () => { try { - await Promise.all([groups.actions.init(), apps.load(), roles.fetch()]) + await Promise.all([ + fetchUser(), + groups.actions.init(), + apps.load(), + roles.fetch(), + ]) loaded = true } catch (error) { notifications.error("Error getting user groups") @@ -228,8 +212,8 @@
{nameLabel} - {#if nameLabel !== $userFetch?.data?.email} - {$userFetch?.data?.email} + {#if nameLabel !== user?.email} + {user?.email} {/if}
@@ -256,21 +240,15 @@
- +
- +
- +
{#if userId !== $auth.user._id} @@ -304,13 +282,12 @@
addGroup(e.detail)} + on:deselect={e => removeGroup(e.detail)} /> @@ -325,7 +302,10 @@ on:click={() => $goto(`../groups/${group._id}`)} > { + removeGroup(group._id) + e.stopPropagation() + }} hoverable size="S" name="Close" @@ -370,13 +350,10 @@ {/if} - + - +