2017-09-25 13:15:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-31 18:13:30 +00:00
|
|
|
import { View, StyleSheet, SafeAreaView, FlatList, LayoutAnimation, Platform } from 'react-native';
|
2017-09-25 13:15:28 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
|
|
|
import { addUser, removeUser, reset, setLoading } from '../actions/selectedUsers';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database 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-04-26 17:33:43 +00:00
|
|
|
import LoggedView from './View';
|
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';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
|
|
|
import sharedStyles from './Styles';
|
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,
|
2018-08-31 18:13:30 +00:00
|
|
|
backgroundColor: Platform.OS === 'ios' ? '#F7F8FA' : '#E1E5E8'
|
2017-09-25 13:15:28 +00:00
|
|
|
},
|
2018-08-31 18:13:30 +00:00
|
|
|
header: {
|
|
|
|
backgroundColor: '#fff'
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
@connect(state => ({
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
2018-07-10 13:40:32 +00:00
|
|
|
users: state.selectedUsers.users,
|
|
|
|
loading: state.selectedUsers.loading
|
|
|
|
}), dispatch => ({
|
|
|
|
addUser: user => dispatch(addUser(user)),
|
|
|
|
removeUser: user => dispatch(removeUser(user)),
|
|
|
|
reset: () => dispatch(reset()),
|
|
|
|
setLoadingInvite: loading => dispatch(setLoading(loading))
|
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
2018-04-26 17:33:43 +00:00
|
|
|
export default class SelectedUsersView extends LoggedView {
|
2017-09-25 13:15:28 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
|
|
|
rid: PropTypes.string,
|
|
|
|
nextAction: PropTypes.string.isRequired,
|
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,
|
|
|
|
setLoadingInvite: PropTypes.func
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-26 17:33:43 +00:00
|
|
|
super('SelectedUsersView', 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
|
|
|
};
|
|
|
|
this.data.addListener(this.updateState);
|
2018-07-10 13:40:32 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.navigator.setDrawerEnabled({
|
|
|
|
side: 'left',
|
|
|
|
enabled: false
|
|
|
|
});
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
async componentDidUpdate(prevProps) {
|
|
|
|
const isVisible = await this.props.navigator.screenIsCurrentlyVisible();
|
|
|
|
if (!isVisible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (prevProps.users.length !== this.props.users.length) {
|
|
|
|
const { length } = this.props.users;
|
2018-07-10 13:40:32 +00:00
|
|
|
const rightButtons = [];
|
|
|
|
if (length > 0) {
|
|
|
|
rightButtons.push({
|
|
|
|
id: 'create',
|
2018-08-31 18:13:30 +00:00
|
|
|
title: I18n.t('Next'),
|
|
|
|
testID: 'selected-users-view-submit'
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.props.navigator.setButtons({ rightButtons });
|
2018-04-10 13:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
componentWillUnmount() {
|
2018-04-24 19:34:03 +00:00
|
|
|
this.updateState.stop();
|
|
|
|
this.data.removeAllListeners();
|
|
|
|
this.props.reset();
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
async onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
|
|
|
if (event.id === 'create') {
|
|
|
|
const { nextAction, setLoadingInvite, navigator } = this.props;
|
|
|
|
if (nextAction === 'CREATE_CHANNEL') {
|
|
|
|
this.props.navigator.push({
|
|
|
|
screen: 'CreateChannelView',
|
2018-08-31 18:13:30 +00:00
|
|
|
title: I18n.t('Create_Channel'),
|
|
|
|
backButtonTitle: ''
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
setLoadingInvite(true);
|
|
|
|
await RocketChat.addUsersToRoom(this.props.rid);
|
|
|
|
navigator.pop();
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomActions Add User', e);
|
|
|
|
} finally {
|
|
|
|
setLoadingInvite(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
|
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-08-31 18:13:30 +00:00
|
|
|
isChecked = username => this.props.users.findIndex(el => el.name === username) !== -1;
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
toggleUser = (user) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
2018-08-31 18:13:30 +00:00
|
|
|
if (!this.isChecked(user.name)) {
|
2017-09-25 13:15:28 +00:00
|
|
|
this.props.addUser(user);
|
|
|
|
} else {
|
|
|
|
this.props.removeUser(user);
|
|
|
|
}
|
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 = () => {
|
|
|
|
if (this.props.users.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
|
|
|
data={this.props.users}
|
|
|
|
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-04-24 19:34:03 +00:00
|
|
|
renderSelectedItem = ({ item }) => (
|
2018-08-31 18:13:30 +00:00
|
|
|
<UserItem
|
|
|
|
name={item.fname}
|
|
|
|
username={item.name}
|
2017-09-25 13:15:28 +00:00
|
|
|
onPress={() => this._onPressSelectedItem(item)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`selected-user-${ item.name }`}
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl={this.props.baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={{ paddingRight: 15 }}
|
2017-09-25 13:15:28 +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 }) => {
|
|
|
|
const name = item.search ? item.name : item.fname;
|
|
|
|
const username = item.search ? item.username : item.name;
|
|
|
|
let style = {};
|
|
|
|
if (index === 0) {
|
|
|
|
style = { ...sharedStyles.separatorTop };
|
|
|
|
}
|
|
|
|
if (this.state.search.length > 0 && index === this.state.search.length - 1) {
|
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
if (this.state.search.length === 0 && index === this.data.length - 1) {
|
|
|
|
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-11 16:32:52 +00:00
|
|
|
baseUrl={this.props.baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
style={style}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderList = () => (
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
|
|
|
data={this.state.search.length > 0 ? this.state.search : this.data}
|
|
|
|
extraData={this.props}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
renderItem={this.renderItem}
|
2018-08-31 16:46:33 +00:00
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
2018-08-31 18:13:30 +00:00
|
|
|
ListHeaderComponent={this.renderHeader}
|
2017-09-25 13:15:28 +00:00
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
2018-08-31 18:13:30 +00:00
|
|
|
)
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
render = () => (
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView} testID='select-users-view'>
|
|
|
|
{this.renderList()}
|
|
|
|
<Loading visible={this.props.loading} />
|
|
|
|
</SafeAreaView>
|
2018-08-31 18:13:30 +00:00
|
|
|
)
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|