2017-09-25 13:15:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-02-26 16:27:04 +00:00
|
|
|
import { View, FlatList } from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-02-01 17:18:55 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2021-02-26 16:27:04 +00:00
|
|
|
import * as List from '../containers/List';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2017-09-25 13:15:28 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-08-31 16:46:33 +00:00
|
|
|
import UserItem from '../presentation/UserItem';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Loading from '../containers/Loading';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2020-07-30 13:26:17 +00:00
|
|
|
import log, { logEvent, events } from '../utils/log';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
|
|
|
import sharedStyles from './Styles';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { withTheme } from '../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2020-04-01 12:28:54 +00:00
|
|
|
import {
|
|
|
|
reset as resetAction,
|
|
|
|
addUser as addUserAction,
|
|
|
|
removeUser as removeUserAction
|
|
|
|
} from '../actions/selectedUsers';
|
|
|
|
import { showErrorAlert } from '../utils/info';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-05-12 19:01:29 +00:00
|
|
|
const ITEM_WIDTH = 250;
|
|
|
|
const getItemLayout = (_, index) => ({ length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index });
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class SelectedUsersView extends React.Component {
|
2017-09-25 13:15:28 +00:00
|
|
|
static propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2017-09-25 13:15:28 +00:00
|
|
|
addUser: PropTypes.func.isRequired,
|
|
|
|
removeUser: PropTypes.func.isRequired,
|
2018-04-24 19:34:03 +00:00
|
|
|
reset: PropTypes.func.isRequired,
|
2018-04-10 13:03:54 +00:00
|
|
|
users: PropTypes.array,
|
2018-07-10 13:40:32 +00:00
|
|
|
loading: PropTypes.bool,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
2020-04-01 12:28:54 +00:00
|
|
|
token: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
name: PropTypes.string
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2021-05-12 19:01:29 +00:00
|
|
|
this.flatlist = React.createRef();
|
2020-06-15 14:00:46 +00:00
|
|
|
const maxUsers = props.route.params?.maxUsers;
|
2017-09-25 13:15:28 +00:00
|
|
|
this.state = {
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers,
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
|
|
|
chats: []
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
if (this.isGroupChat()) {
|
|
|
|
props.addUser({ _id: user.id, name: user.username, fname: user.name });
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader(props.route.params?.showButton);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.isGroupChat()) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { users } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
if (prevProps.users.length !== users.length) {
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader(users.length > 0);
|
2020-04-01 12:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
componentWillUnmount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { reset } = this.props;
|
|
|
|
reset();
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.querySubscription && this.querySubscription.unsubscribe) {
|
|
|
|
this.querySubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
// showButton can be sent as route params or updated by the component
|
|
|
|
setHeader = (showButton) => {
|
|
|
|
const { navigation, route } = this.props;
|
|
|
|
const title = route.params?.title ?? I18n.t('Select_Users');
|
|
|
|
const buttonText = route.params?.buttonText ?? I18n.t('Next');
|
|
|
|
const maxUsers = route.params?.maxUsers;
|
|
|
|
const nextAction = route.params?.nextAction ?? (() => {});
|
|
|
|
const options = {
|
|
|
|
title,
|
|
|
|
headerRight: () => (
|
|
|
|
(!maxUsers || showButton) && (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item title={buttonText} onPress={nextAction} testID='selected-users-view-submit' />
|
|
|
|
</HeaderButton.Container>
|
2020-06-15 14:00:46 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
navigation.setOptions(options);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
init = async() => {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const observable = await db.collections
|
|
|
|
.get('subscriptions')
|
|
|
|
.query(Q.where('t', 'd'))
|
|
|
|
.observeWithColumns(['room_updated_at']);
|
|
|
|
|
|
|
|
this.querySubscription = observable.subscribe((data) => {
|
|
|
|
const chats = orderBy(data, ['roomUpdatedAt'], ['desc']);
|
|
|
|
this.setState({ chats });
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
search = async(text) => {
|
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
isGroupChat = () => {
|
|
|
|
const { maxUsers } = this.state;
|
|
|
|
return maxUsers > 2;
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
isChecked = (username) => {
|
|
|
|
const { users } = this.props;
|
|
|
|
return users.findIndex(el => el.name === username) !== -1;
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
toggleUser = (user) => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { maxUsers } = this.state;
|
|
|
|
const {
|
|
|
|
addUser, removeUser, users, user: { username }
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
// Disallow removing self user from the direct message group
|
|
|
|
if (this.isGroupChat() && username === user.name) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
if (!this.isChecked(user.name)) {
|
2020-04-01 12:28:54 +00:00
|
|
|
if (this.isGroupChat() && users.length === maxUsers) {
|
|
|
|
return showErrorAlert(I18n.t('Max_number_of_users_allowed_is_number', { maxUsers }), I18n.t('Oops'));
|
|
|
|
}
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.SELECTED_USERS_ADD_USER);
|
2018-09-25 19:28:42 +00:00
|
|
|
addUser(user);
|
2017-09-25 13:15:28 +00:00
|
|
|
} else {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.SELECTED_USERS_REMOVE_USER);
|
2018-09-25 19:28:42 +00:00
|
|
|
removeUser(user);
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
_onPressItem = (id, item = {}) => {
|
|
|
|
if (item.search) {
|
2018-08-31 18:13:30 +00:00
|
|
|
this.toggleUser({ _id: item._id, name: item.username, fname: item.name });
|
2017-09-25 13:15:28 +00:00
|
|
|
} else {
|
2018-08-31 18:13:30 +00:00
|
|
|
this.toggleUser({ _id: item._id, name: item.name, fname: item.fname });
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
_onPressSelectedItem = item => this.toggleUser(item);
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].backgroundColor }}>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='select-users-view-search' />
|
|
|
|
{this.renderSelected()}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-05-12 19:01:29 +00:00
|
|
|
setFlatListRef = ref => this.flatlist = ref;
|
|
|
|
|
|
|
|
onContentSizeChange = () => this.flatlist.scrollToEnd({ animated: true });
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSelected = () => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { users, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
if (users.length === 0) {
|
2017-09-25 13:15:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-12 19:01:29 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
2018-09-25 19:28:42 +00:00
|
|
|
data={users}
|
2021-05-12 19:01:29 +00:00
|
|
|
ref={this.setFlatListRef}
|
|
|
|
onContentSizeChange={this.onContentSizeChange}
|
|
|
|
getItemLayout={getItemLayout}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyExtractor={item => item._id}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[sharedStyles.separatorTop, { borderColor: themes[theme].separatorColor }]}
|
2018-08-31 18:13:30 +00:00
|
|
|
contentContainerStyle={{ marginVertical: 5 }}
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem={this.renderSelectedItem}
|
2017-09-25 13:15:28 +00:00
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
horizontal
|
|
|
|
/>
|
|
|
|
);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderSelectedItem = ({ item }) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.fname}
|
|
|
|
username={item.name}
|
|
|
|
onPress={() => this._onPressSelectedItem(item)}
|
|
|
|
testID={`selected-user-${ item.name }`}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
style={{ paddingRight: 15 }}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
renderItem = ({ item, index }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const name = item.search ? item.name : item.fname;
|
|
|
|
const username = item.search ? item.username : item.name;
|
2019-12-04 16:39:53 +00:00
|
|
|
let style = { borderColor: themes[theme].separatorColor };
|
2018-08-31 18:13:30 +00:00
|
|
|
if (index === 0) {
|
2019-12-04 16:39:53 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorTop };
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (search.length === 0 && index === chats.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={name}
|
|
|
|
username={username}
|
|
|
|
onPress={() => this._onPressItem(item._id, item)}
|
|
|
|
testID={`select-users-view-item-${ item.name }`}
|
|
|
|
icon={this.isChecked(username) ? 'check' : null}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={style}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
|
|
|
|
const data = (search.length > 0 ? search : chats)
|
|
|
|
// filter DM between multiple users
|
|
|
|
.filter(sub => !RocketChat.isGroupChat(sub));
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2020-04-01 12:28:54 +00:00
|
|
|
data={data}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.props}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
renderItem={this.renderItem}
|
2021-02-26 16:27:04 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2018-09-25 19:28:42 +00:00
|
|
|
ListHeaderComponent={this.renderHeader}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
render = () => {
|
2020-10-30 13:59:44 +00:00
|
|
|
const { loading } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='select-users-view'>
|
|
|
|
<StatusBar />
|
2018-09-25 19:28:42 +00:00
|
|
|
{this.renderList()}
|
|
|
|
<Loading visible={loading} />
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2019-08-07 13:51:34 +00:00
|
|
|
users: state.selectedUsers.users,
|
|
|
|
loading: state.selectedUsers.loading,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
addUser: user => dispatch(addUserAction(user)),
|
|
|
|
removeUser: user => dispatch(removeUserAction(user)),
|
2020-04-01 12:28:54 +00:00
|
|
|
reset: () => dispatch(resetAction())
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(SelectedUsersView));
|