2017-09-25 13:15:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-01-29 19:52:56 +00:00
|
|
|
View, StyleSheet, FlatList, LayoutAnimation
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
addUser as addUserAction, removeUser as removeUserAction, reset as resetAction, setLoading as setLoadingAction
|
|
|
|
} from '../actions/selectedUsers';
|
2019-04-04 18:08:40 +00:00
|
|
|
import database, { safeAddListener } from '../lib/realm';
|
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';
|
|
|
|
import debounce from '../utils/debounce';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-07-10 13:40:32 +00:00
|
|
|
import log from '../utils/log';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
|
|
|
import sharedStyles from './Styles';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { Item, CustomHeaderButtons } from '../containers/HeaderButton';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-03-29 19:36:07 +00:00
|
|
|
import { COLOR_WHITE } from '../constants/colors';
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2017-11-13 12:49:19 +00:00
|
|
|
safeAreaView: {
|
2017-11-13 16:04:13 +00:00
|
|
|
flex: 1,
|
2019-01-29 19:52:56 +00:00
|
|
|
backgroundColor: isIOS ? '#F7F8FA' : '#E1E5E8'
|
2017-09-25 13:15:28 +00:00
|
|
|
},
|
2018-08-31 18:13:30 +00:00
|
|
|
header: {
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_WHITE
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
2018-08-31 16:46:33 +00:00
|
|
|
separator: {
|
|
|
|
marginLeft: 60
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class SelectedUsersView extends React.Component {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const title = navigation.getParam('title');
|
|
|
|
const nextAction = navigation.getParam('nextAction', () => {});
|
2019-02-08 16:34:01 +00:00
|
|
|
return {
|
2019-03-12 16:23:06 +00:00
|
|
|
title,
|
|
|
|
headerRight: (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item title={I18n.t('Next')} onPress={nextAction} testID='selected-users-view-submit' />
|
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
2019-02-08 16:34:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
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
|
|
|
setLoadingInvite: PropTypes.func,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
})
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.data = database.objects('subscriptions').filtered('t = $0', 'd').sorted('roomUpdatedAt', true);
|
2017-09-25 13:15:28 +00:00
|
|
|
this.state = {
|
2018-04-24 19:34:03 +00:00
|
|
|
search: []
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2019-04-04 18:08:40 +00:00
|
|
|
safeAddListener(this.data, this.updateState);
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.setParams({ nextAction: this.nextAction });
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { search } = this.state;
|
|
|
|
const { users, loading } = this.props;
|
|
|
|
if (nextProps.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextProps.users, users)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.search, search)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
componentWillUnmount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { reset } = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
this.updateState.stop();
|
|
|
|
this.data.removeAllListeners();
|
2018-09-25 19:28:42 +00:00
|
|
|
reset();
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
nextAction = async() => {
|
|
|
|
const { navigation, setLoadingInvite } = this.props;
|
|
|
|
const nextActionID = navigation.getParam('nextActionID');
|
|
|
|
if (nextActionID === 'CREATE_CHANNEL') {
|
|
|
|
navigation.navigate('CreateChannelView');
|
|
|
|
} else {
|
|
|
|
const rid = navigation.getParam('rid');
|
|
|
|
try {
|
|
|
|
setLoadingInvite(true);
|
|
|
|
await RocketChat.addUsersToRoom(rid);
|
|
|
|
navigation.pop();
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_add_user', e);
|
2019-03-12 16:23:06 +00:00
|
|
|
} finally {
|
|
|
|
setLoadingInvite(false);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2018-04-24 19:34:03 +00:00
|
|
|
updateState = debounce(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
}, 1000);
|
|
|
|
|
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
|
|
|
|
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) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { addUser, removeUser } = this.props;
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
2018-08-31 18:13:30 +00:00
|
|
|
if (!this.isChecked(user.name)) {
|
2018-09-25 19:28:42 +00:00
|
|
|
addUser(user);
|
2017-09-25 13:15:28 +00:00
|
|
|
} else {
|
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);
|
|
|
|
|
|
|
|
renderHeader = () => (
|
2018-08-31 18:13:30 +00:00
|
|
|
<View style={styles.header}>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='select-users-view-search' />
|
2017-09-25 13:15:28 +00:00
|
|
|
{this.renderSelected()}
|
|
|
|
</View>
|
2018-08-31 18:13:30 +00:00
|
|
|
)
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSelected = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { users } = this.props;
|
|
|
|
|
|
|
|
if (users.length === 0) {
|
2017-09-25 13:15:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
2018-09-25 19:28:42 +00:00
|
|
|
data={users}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyExtractor={item => item._id}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={[styles.list, sharedStyles.separatorTop]}
|
|
|
|
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-02-07 19:58:20 +00:00
|
|
|
const { baseUrl, user } = 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}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
renderSeparator = () => <View style={[sharedStyles.separator, styles.separator]} />
|
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { search } = this.state;
|
2019-02-07 19:58:20 +00:00
|
|
|
const { baseUrl, user } = 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;
|
|
|
|
let style = {};
|
|
|
|
if (index === 0) {
|
|
|
|
style = { ...sharedStyles.separatorTop };
|
|
|
|
}
|
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 };
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length === 0 && index === this.data.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}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
|
|
|
const { search } = this.state;
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={search.length > 0 ? search : this.data}
|
|
|
|
extraData={this.props}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
render = () => {
|
|
|
|
const { loading } = this.props;
|
|
|
|
return (
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView} testID='select-users-view' forceInset={{ vertical: 'never' }}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<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 => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
users: state.selectedUsers.users,
|
|
|
|
loading: state.selectedUsers.loading,
|
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
addUser: user => dispatch(addUserAction(user)),
|
|
|
|
removeUser: user => dispatch(removeUserAction(user)),
|
|
|
|
reset: () => dispatch(resetAction()),
|
|
|
|
setLoadingInvite: loading => dispatch(setLoadingAction(loading))
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(SelectedUsersView);
|