2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import { avatarURL } from '../../utils/avatar';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-10-30 13:12:02 +00:00
|
|
|
import database from '../../lib/database';
|
2020-03-30 19:50:27 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { MultiSelect } from '../../containers/UIKit/MultiSelect';
|
|
|
|
|
|
|
|
import styles from './styles';
|
2020-04-01 12:07:03 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
const SelectUsers = ({
|
2020-10-30 15:54:02 +00:00
|
|
|
server, token, userId, selected, onUserSelect, blockUnauthenticatedAccess, theme
|
2020-03-30 19:50:27 +00:00
|
|
|
}) => {
|
|
|
|
const [users, setUsers] = useState([]);
|
|
|
|
|
|
|
|
const getUsers = debounce(async(keyword = '') => {
|
|
|
|
try {
|
2020-10-30 13:12:02 +00:00
|
|
|
const db = database.active;
|
|
|
|
const usersCollection = db.collections.get('users');
|
2020-03-30 19:50:27 +00:00
|
|
|
const res = await RocketChat.search({ text: keyword, filterRooms: false });
|
2020-10-30 13:12:02 +00:00
|
|
|
let items = [...users.filter(u => selected.includes(u.name)), ...res.filter(r => !users.find(u => u.name === r.name))];
|
|
|
|
const records = await usersCollection.query(Q.where('username', Q.oneOf(items.map(u => u.name)))).fetch();
|
|
|
|
items = items.map((item) => {
|
|
|
|
const index = records.findIndex(r => r.username === item.name);
|
|
|
|
if (index > -1) {
|
|
|
|
const record = records[index];
|
|
|
|
return {
|
|
|
|
uids: item.uids,
|
|
|
|
usernames: item.usernames,
|
|
|
|
prid: item.prid,
|
|
|
|
fname: item.fname,
|
|
|
|
name: item.name,
|
|
|
|
avatarETag: record.avatarETag
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
setUsers(items);
|
2020-03-30 19:50:27 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
const getAvatar = item => avatarURL({
|
2020-10-30 15:54:02 +00:00
|
|
|
text: RocketChat.getRoomAvatar(item),
|
|
|
|
type: 'd',
|
|
|
|
user: { id: userId, token },
|
|
|
|
server,
|
|
|
|
avatarETag: item.avatarETag,
|
|
|
|
blockUnauthenticatedAccess
|
2020-03-30 19:50:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-04-01 12:07:03 +00:00
|
|
|
<Text style={[styles.label, { color: themes[theme].titleText }]}>{I18n.t('Invite_users')}</Text>
|
2020-03-30 19:50:27 +00:00
|
|
|
<MultiSelect
|
|
|
|
theme={theme}
|
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
onSearch={getUsers}
|
|
|
|
onChange={onUserSelect}
|
|
|
|
options={users.map(user => ({
|
|
|
|
value: user.name,
|
|
|
|
text: { text: RocketChat.getRoomTitle(user) },
|
2020-10-30 13:12:02 +00:00
|
|
|
imageUrl: getAvatar(user)
|
2020-03-30 19:50:27 +00:00
|
|
|
}))}
|
|
|
|
onClose={() => setUsers(users.filter(u => selected.includes(u.name)))}
|
|
|
|
placeholder={{ text: `${ I18n.t('Select_Users') }...` }}
|
|
|
|
context={BLOCK_CONTEXT.FORM}
|
|
|
|
multiselect
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
SelectUsers.propTypes = {
|
|
|
|
server: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
|
|
|
userId: PropTypes.string,
|
|
|
|
selected: PropTypes.array,
|
|
|
|
onUserSelect: PropTypes.func,
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess: PropTypes.bool,
|
2020-03-30 19:50:27 +00:00
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectUsers;
|