2017-09-25 13:15:28 +00:00
|
|
|
import ActionButton from 'react-native-action-button';
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { View, StyleSheet, TextInput, Text, TouchableOpacity, SafeAreaView, FlatList, LayoutAnimation, Platform } from 'react-native';
|
2017-09-25 13:15:28 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { addUser, removeUser, reset } 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';
|
|
|
|
import RoomItem from '../presentation/RoomItem';
|
|
|
|
import Avatar from '../containers/Avatar';
|
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';
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'stretch',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
2017-11-13 12:49:19 +00:00
|
|
|
safeAreaView: {
|
2017-11-13 16:04:13 +00:00
|
|
|
flex: 1,
|
2017-11-13 12:49:19 +00:00
|
|
|
backgroundColor: '#FFFFFF'
|
|
|
|
},
|
2017-09-25 13:15:28 +00:00
|
|
|
list: {
|
|
|
|
width: '100%',
|
|
|
|
backgroundColor: '#FFFFFF'
|
|
|
|
},
|
|
|
|
actionButtonIcon: {
|
|
|
|
fontSize: 20,
|
|
|
|
height: 22,
|
|
|
|
color: 'white'
|
|
|
|
},
|
|
|
|
searchBoxView: {
|
|
|
|
backgroundColor: '#eee'
|
|
|
|
},
|
|
|
|
searchBox: {
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
margin: 5,
|
|
|
|
borderRadius: 5,
|
|
|
|
padding: 5,
|
|
|
|
paddingLeft: 10,
|
|
|
|
color: '#aaa'
|
|
|
|
},
|
|
|
|
selectItemView: {
|
|
|
|
width: 80,
|
|
|
|
height: 80,
|
|
|
|
padding: 8,
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
|
|
|
status: {
|
|
|
|
bottom: -2,
|
|
|
|
right: -2,
|
|
|
|
borderWidth: 2,
|
|
|
|
borderRadius: 12,
|
|
|
|
width: 12,
|
|
|
|
height: 12
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
@connect(
|
|
|
|
state => ({
|
2018-04-10 13:03:54 +00:00
|
|
|
user: state.login.user,
|
2017-09-25 13:15:28 +00:00
|
|
|
Site_Url: state.settings.Site_Url,
|
2018-04-24 19:34:03 +00:00
|
|
|
users: state.selectedUsers.users,
|
|
|
|
loading: state.selectedUsers.loading
|
2017-09-25 13:15:28 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
2018-04-24 19:34:03 +00:00
|
|
|
addUser: user => dispatch(addUser(user)),
|
|
|
|
removeUser: user => dispatch(removeUser(user)),
|
|
|
|
reset: () => dispatch(reset())
|
2017-09-25 13:15:28 +00:00
|
|
|
})
|
|
|
|
)
|
2018-04-26 17:33:43 +00:00
|
|
|
export default class SelectedUsersView extends LoggedView {
|
2017-09-25 13:15:28 +00:00
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object.isRequired,
|
2018-04-24 19:34:03 +00:00
|
|
|
user: PropTypes.object,
|
2017-09-25 13:15:28 +00:00
|
|
|
Site_Url: PropTypes.string,
|
|
|
|
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-04-24 19:34:03 +00:00
|
|
|
loading: PropTypes.bool
|
2018-04-10 13:03:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const params = navigation.state.params || {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
headerRight: (
|
|
|
|
params.showCreateiOS && Platform.OS === 'ios' ?
|
|
|
|
<TouchableOpacity
|
|
|
|
style={{
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
height: 44,
|
|
|
|
width: 44,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
2018-04-24 19:34:03 +00:00
|
|
|
onPress={() => params.nextAction()}
|
2018-05-18 17:55:08 +00:00
|
|
|
accessibilityLabel='Submit'
|
2018-04-10 13:03:54 +00:00
|
|
|
accessibilityTraits='button'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='selected-users-view-submit'
|
2018-04-10 13:03:54 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name='ios-add'
|
|
|
|
color='#292E35'
|
|
|
|
size={24}
|
|
|
|
backgroundColor='transparent'
|
|
|
|
/>
|
|
|
|
</TouchableOpacity> : null
|
|
|
|
)
|
|
|
|
};
|
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-04-10 13:03:54 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.users.length !== this.props.users.length) {
|
|
|
|
this.props.navigation.setParams({
|
|
|
|
showCreateiOS: nextProps.users.length > 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
async search(text) {
|
2017-09-25 13:15:28 +00:00
|
|
|
const searchText = text.trim();
|
|
|
|
if (searchText === '') {
|
2018-04-24 19:34:03 +00:00
|
|
|
delete this.oldPromise;
|
2017-09-25 13:15:28 +00:00
|
|
|
return this.setState({
|
2018-04-24 19:34:03 +00:00
|
|
|
search: []
|
2017-09-25 13:15:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
let data = this.data.filtered('name CONTAINS[c] $0 AND t = $1', searchText, 'd').slice(0, 7);
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const usernames = data.map(sub => sub.map);
|
|
|
|
try {
|
|
|
|
if (data.length < 7) {
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise('cancel');
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const { users } = await Promise.race([
|
|
|
|
RocketChat.spotlight(searchText, usernames, { users: true, rooms: false }),
|
|
|
|
new Promise((resolve, reject) => this.oldPromise = reject)
|
|
|
|
]);
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
data = users.map(user => ({
|
|
|
|
...user,
|
|
|
|
rid: user.username,
|
|
|
|
name: user.username,
|
|
|
|
t: 'd',
|
|
|
|
search: true
|
|
|
|
}));
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
delete this.oldPromise;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
search: data
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// alert(JSON.stringify(e));
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
|
|
|
toggleUser = (user) => {
|
2018-04-24 19:34:03 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
2017-09-25 13:15:28 +00:00
|
|
|
const index = this.props.users.findIndex(el => el.name === user.name);
|
|
|
|
if (index === -1) {
|
|
|
|
this.props.addUser(user);
|
|
|
|
} else {
|
|
|
|
this.props.removeUser(user);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_onPressItem = (id, item = {}) => {
|
|
|
|
if (item.search) {
|
|
|
|
this.toggleUser({ _id: item._id, name: item.username });
|
|
|
|
} else {
|
|
|
|
this.toggleUser({ _id: item._id, name: item.name });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_onPressSelectedItem = item => this.toggleUser(item);
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
nextAction = () => {
|
|
|
|
const params = this.props.navigation.state.params || {};
|
|
|
|
params.nextAction();
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
renderHeader = () => (
|
|
|
|
<View style={styles.container}>
|
|
|
|
{this.renderSearchBar()}
|
|
|
|
{this.renderSelected()}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderSearchBar = () => (
|
|
|
|
<View style={styles.searchBoxView}>
|
|
|
|
<TextInput
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
style={styles.searchBox}
|
2018-04-24 19:34:03 +00:00
|
|
|
onChangeText={text => this.onSearchChangeText(text)}
|
2017-09-25 13:15:28 +00:00
|
|
|
returnKeyType='search'
|
|
|
|
placeholder='Search'
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
blurOnSubmit
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='select-users-view-search'
|
2018-05-18 17:55:08 +00:00
|
|
|
autoCorrect={false}
|
|
|
|
autoCapitalize='none'
|
2017-09-25 13:15:28 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
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}
|
2017-09-25 13:15:28 +00:00
|
|
|
style={styles.list}
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem={this.renderSelectedItem}
|
2017-09-25 13:15:28 +00:00
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
horizontal
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
renderSelectedItem = ({ item }) => (
|
2017-09-25 13:15:28 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
key={item._id}
|
|
|
|
style={styles.selectItemView}
|
|
|
|
onPress={() => this._onPressSelectedItem(item)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`selected-user-${ item.name }`}
|
2017-09-25 13:15:28 +00:00
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<Avatar text={item.name} size={40} />
|
2017-09-25 13:15:28 +00:00
|
|
|
<Text ellipsizeMode='tail' numberOfLines={1} style={{ fontSize: 10 }}>
|
|
|
|
{item.name}
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem = ({ item }) => (
|
2017-09-25 13:15:28 +00:00
|
|
|
<RoomItem
|
|
|
|
key={item._id}
|
|
|
|
name={item.name}
|
|
|
|
type={item.t}
|
|
|
|
baseUrl={this.props.Site_Url}
|
|
|
|
onPress={() => this._onPressItem(item._id, item)}
|
2018-04-10 13:03:54 +00:00
|
|
|
id={item.rid.replace(this.props.user.id, '').trim()}
|
2018-04-24 19:34:03 +00:00
|
|
|
showLastMessage={false}
|
|
|
|
avatarSize={30}
|
|
|
|
statusStyle={styles.status}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`select-users-view-item-${ item.name }`}
|
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}
|
2017-09-25 13:15:28 +00:00
|
|
|
style={styles.list}
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
2017-09-25 13:15:28 +00:00
|
|
|
enableEmptySections
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
renderCreateButton = () => {
|
2018-04-10 13:03:54 +00:00
|
|
|
if (this.props.users.length === 0 || Platform.OS === 'ios') {
|
2017-09-25 13:15:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<ActionButton
|
|
|
|
buttonColor='rgba(67, 165, 71, 1)'
|
2018-04-24 19:34:03 +00:00
|
|
|
onPress={() => this.nextAction()}
|
|
|
|
renderIcon={() => <Icon name='md-arrow-forward' style={styles.actionButtonIcon} />}
|
2017-09-25 13:15:28 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
render = () => (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View style={styles.container} testID='select-users-view'>
|
2017-11-13 12:49:19 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView}>
|
|
|
|
{this.renderList()}
|
|
|
|
{this.renderCreateButton()}
|
2018-04-24 19:34:03 +00:00
|
|
|
<Loading visible={this.props.loading} />
|
2017-11-13 12:49:19 +00:00
|
|
|
</SafeAreaView>
|
2017-09-25 13:15:28 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|