Skip to content

Commit

Permalink
Show email or group name in place of ID in AttachModal
Browse files Browse the repository at this point in the history
  • Loading branch information
eladlachmi committed Feb 12, 2024
1 parent 4760f11 commit 1ad4c36
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions webui/src/lib/components/auth/forms.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import {SearchIcon} from "@primer/octicons-react";
import {useAPI} from "../../hooks/api";
import {Checkbox, DataTable, DebouncedFormControl, AlertError, Loading} from "../controls";

const isEmptyString = (str) => (!str?.length);

const resolveEntityDisplayName = (ent) => {
// for users
if (ent?.email?.length) return ent.email;
// for groups
if (ent?.name?.length) return ent.name;
return ent.id;
}

export const AttachModal = ({ show, searchFn, onAttach, onHide, addText = "Add",
emptyState = 'No matches', modalTitle = 'Add', headers = ['Select', 'ID'],
Expand Down Expand Up @@ -41,19 +46,19 @@ export const AttachModal = ({ show, searchFn, onAttach, onHide, addText = "Add",
rowFn={ent => [
<Checkbox
defaultChecked={selected.indexOf(ent.id) >= 0}
onAdd={() => setSelected([...selected, ent.id])}
onRemove={() => setSelected(selected.filter(id => id !== ent.id))}
onAdd={() => setSelected([...selected, ent])}
onRemove={() => setSelected(selected.filter(selectedEnt => selectedEnt.id !== ent.id))}
name={'selected'}/>,
<strong>{!isEmptyString(ent.email) ? ent.email : ent.id}</strong>
<strong>{resolveEntityDisplayName(ent)}</strong>
]}/>

<div className="mt-3">
{(selected.length > 0) &&
<p>
<strong>Selected: </strong>
{(selected.map(item => (
<Badge key={item} pill variant="primary" className="me-1">
{item}
<Badge key={item.id} pill variant="primary" className="me-1">
{resolveEntityDisplayName(item)}
</Badge>
)))}
</p>
Expand Down
2 changes: 1 addition & 1 deletion webui/src/pages/auth/groups/group/members.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const GroupMemberList = ({ groupId, after, onPaginate }) => {
searchFn={prefix => auth.listUsers(prefix, "", 5).then(res => res.results)}
onHide={() => setShowAddModal(false)}
onAttach={(selected) => {
Promise.all(selected.map(userId => auth.addUserToGroup(userId, groupId)))
Promise.all(selected.map(user => auth.addUserToGroup(user.id, groupId)))
.then(() => { setRefresh(!refresh); setAttachError(null) })
.catch(error => { setAttachError(error) })
.finally(() => { setShowAddModal(false) });
Expand Down
2 changes: 1 addition & 1 deletion webui/src/pages/auth/groups/group/policies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const GroupPoliciesList = ({ groupId, after, onPaginate }) => {
searchFn={prefix => auth.listPolicies(prefix, "", 5).then(res => res.results)}
onHide={() => setShowAddModal(false)}
onAttach={(selected) => {
Promise.all(selected.map(policyId => auth.attachPolicyToGroup(groupId, policyId)))
Promise.all(selected.map(policy => auth.attachPolicyToGroup(groupId, policy.id)))
.then(() => { setRefresh(!refresh); setAttachError(null) })
.catch(error => { setAttachError(error) })
.finally(() => { setShowAddModal(false) })
Expand Down
2 changes: 1 addition & 1 deletion webui/src/pages/auth/users/user/groups.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const UserGroupsList = ({ userId, after, onPaginate }) => {
onHide={() => setShowAddModal(false)}
onAttach={(selected) => {
Promise.all(
selected.map((groupId) => auth.addUserToGroup(userId, groupId))
selected.map((group) => auth.addUserToGroup(userId, group.id))
)
.then(() => {
setRefresh(!refresh);
Expand Down
2 changes: 1 addition & 1 deletion webui/src/pages/auth/users/user/policies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const UserPoliciesList = ({ userId, after, onPaginate }) => {
searchFn={prefix => auth.listPolicies(prefix, "", 5).then(res => res.results)}
onHide={() => setShowAddModal(false)}
onAttach={(selected) => {
Promise.all(selected.map(policyId => auth.attachPolicyToUser(userId, policyId)))
Promise.all(selected.map(policy => auth.attachPolicyToUser(userId, policy.id)))
.then(() => { setRefresh(!refresh); setAttachError(null) })
.catch(error => { setAttachError(error) })
.finally(() => { setShowAddModal(false) });
Expand Down

0 comments on commit 1ad4c36

Please sign in to comment.