Skip to content

Commit

Permalink
multiple user assigned for a role
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman-Maheshwari committed Nov 3, 2021
1 parent ac84a41 commit a0b709a
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions client/views/admin/permissions/UsersInRolePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import React, { useState, useRef } from 'react';

import Page from '../../../components/Page';
import RoomAutoComplete from '../../../components/RoomAutoComplete';
import UserAutoComplete from '../../../components/UserAutoComplete';
import UserAutoCompleteMultiple from '../../../components/UserAutoCompleteMultiple';
import { useRoute } from '../../../contexts/RouterContext';
import { useEndpoint } from '../../../contexts/ServerContext';
import { useToastMessageDispatch } from '../../../contexts/ToastMessagesContext';
import { useTranslation } from '../../../contexts/TranslationContext';
import { useForm } from '../../../hooks/useForm';
import UsersInRoleTableContainer from './UsersInRoleTableContainer';

const UsersInRolePage = ({ data }) => {
Expand All @@ -17,10 +18,13 @@ const UsersInRolePage = ({ data }) => {

const reload = useRef();

const [user, setUser] = useState('');
const [rid, setRid] = useState();
const [userError, setUserError] = useState();

const { values, handlers } = useForm({ users: [] });
const { users } = values;
const { handleUsers } = handlers;

const { _id, name, description } = data;

const router = useRoute('admin-permissions');
Expand All @@ -35,26 +39,29 @@ const UsersInRolePage = ({ data }) => {
});

const handleAdd = useMutableCallback(async () => {
if (!user) {
if (users.length === 0) {
return setUserError(t('User_cant_be_empty'));
}

try {
await addUser({ roleName: _id, username: user, roomId: rid });
dispatchToastMessage({ type: 'success', message: t('User_added') });
setUser();
reload.current();
users.map(async (u) => {
await addUser({ roleName: _id, username: u, roomId: rid });
dispatchToastMessage({ type: 'success', message: t('User_added') });
reload.current();
});
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
});

const handleUserChange = useMutableCallback((user) => {
if (user !== '') {
setUserError();
const handleUserChange = useMutableCallback((value, action) => {
if (!action) {
if (users.includes(value)) {
return;
}
return handleUsers([...users, value]);
}

return setUser(user);
handleUsers(users.filter((current) => current !== value));
});

return (
Expand All @@ -78,14 +85,14 @@ const UsersInRolePage = ({ data }) => {
<Field>
<Field.Label>{t('Add_user')}</Field.Label>
<Field.Row>
<UserAutoComplete
value={user}
<UserAutoCompleteMultiple
value={users}
onChange={handleUserChange}
placeholder={t('User')}
/>

<ButtonGroup mis='x8' align='end'>
<Button primary onClick={handleAdd}>
<Button primary onClick={handleAdd} disabled={!users.length}>
{t('Add')}
</Button>
</ButtonGroup>
Expand Down

0 comments on commit a0b709a

Please sign in to comment.