Skip to content

Commit

Permalink
Multi select for permissions in user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed May 13, 2024
1 parent 40499c6 commit bee6481
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
7 changes: 7 additions & 0 deletions assets/js/lib/forms/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ export const REQUIRED_FIELD_TEXT = 'Required field';
export const errorMessage = (message) => (
<p className="text-red-500 mt-1">{capitalize(message)}</p>
);

export const mapAbilities = (abilities) =>
abilities.map(({ id, name, resource, label }) => ({
value: id,
label: `${name}:${resource}`,
tooltip: label,
}));
14 changes: 6 additions & 8 deletions assets/js/pages/Profile/ProfileForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Button from '@common/Button';
import Input from '@common/Input';
import Label from '@common/Label';
import Modal from '@common/Modal';
import MultiSelect from '@common/MultiSelect';
import ProfilePasswordChangeForm from '@pages/Profile/ProfilePasswordChangeForm';
import { REQUIRED_FIELD_TEXT, errorMessage } from '@lib/forms';
import { REQUIRED_FIELD_TEXT, errorMessage, mapAbilities } from '@lib/forms';

function ProfileForm({
fullName = '',
Expand Down Expand Up @@ -58,10 +59,6 @@ function ProfileForm({
setEmailAddressError(getError('email', errors));
}, [errors]);

const formattedAbilities = abilities
.map(({ name, resource }) => `${resource}:${name}`)
.join(' ');

return (
<div>
<div className="container max-w-7xl mx-auto p-4 sm:p-6 lg:p-8 bg-white dark:bg-gray-800 rounded-lg">
Expand Down Expand Up @@ -110,10 +107,11 @@ function ProfileForm({
</div>
<Label className="col-start-1 col-span-1">Permissions</Label>
<div className="col-start-2 col-span-3">
<Input
value={formattedAbilities}
<MultiSelect
aria-label="permissions"
disabled
values={mapAbilities(abilities)}
isDisabled
options={mapAbilities(abilities)}
/>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions assets/js/pages/Profile/ProfileForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('ProfileForm', () => {
expect(screen.getByText('Username')).toBeVisible();
expect(screen.getByLabelText('username').value).toBe(username);
expect(screen.getByText('Permissions')).toBeVisible();
expect(screen.getByLabelText('permissions').value).toBe(
abilities.map(({ name, resource }) => `${resource}:${name}`).join(' ')
);
abilities.forEach(({ resource, name }) => {
expect(screen.getByText(`${name}:${resource}`)).toBeVisible();
});
expect(screen.getByText('Change Password')).toBeVisible();
expect(screen.getByText('Save')).toBeVisible();
});
Expand Down
8 changes: 1 addition & 7 deletions assets/js/pages/Users/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PASSWORD_PLACEHOLDER,
REQUIRED_FIELD_TEXT,
errorMessage,
mapAbilities,
} from '@lib/forms';
import { getError } from '@lib/api/validationErrors';

Expand All @@ -23,13 +24,6 @@ const USER_ENABLED = 'Enabled';
const defaultAbilities = [];
const defaultErrors = [];

const mapAbilities = (abilities) =>
abilities.map(({ id, name, resource, label }) => ({
value: id,
label: `${name}:${resource}`,
tooltip: label,
}));

function UserForm({
fullName = '',
emailAddress = '',
Expand Down

0 comments on commit bee6481

Please sign in to comment.