2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-03-01 16:49:11 +00:00
|
|
|
View, StyleSheet, FlatList, Text
|
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';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { orderBy } from 'lodash';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2018-08-31 18:13:30 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import UserItem from '../presentation/UserItem';
|
|
|
|
import sharedStyles from './Styles';
|
|
|
|
import I18n from '../i18n';
|
2019-09-16 20:26:32 +00:00
|
|
|
import log from '../utils/log';
|
2018-08-31 18:13:30 +00:00
|
|
|
import Touch from '../utils/touch';
|
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';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
|
|
|
import { CloseModalButton } from '../containers/HeaderButton';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-03-29 19:36:07 +00:00
|
|
|
import { COLOR_PRIMARY, COLOR_WHITE } from '../constants/colors';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
safeAreaView: {
|
|
|
|
flex: 1,
|
2019-01-29 19:52:56 +00:00
|
|
|
backgroundColor: isIOS ? '#F7F8FA' : '#E1E5E8'
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
|
|
|
separator: {
|
|
|
|
marginLeft: 60
|
|
|
|
},
|
|
|
|
createChannelButton: {
|
|
|
|
marginVertical: 25
|
|
|
|
},
|
|
|
|
createChannelContainer: {
|
|
|
|
height: 47,
|
2019-03-29 19:36:07 +00:00
|
|
|
backgroundColor: COLOR_WHITE,
|
2018-08-31 18:13:30 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
createChannelIcon: {
|
2019-03-29 19:36:07 +00:00
|
|
|
color: COLOR_PRIMARY,
|
2019-06-10 16:22:35 +00:00
|
|
|
marginLeft: 18,
|
|
|
|
marginRight: 15
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
|
|
|
createChannelText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
color: COLOR_PRIMARY,
|
|
|
|
fontSize: 17,
|
|
|
|
...sharedStyles.textRegular
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewMessageView extends React.Component {
|
2019-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
headerLeft: <CloseModalButton navigation={navigation} testID='new-message-view-close' />,
|
|
|
|
title: I18n.t('New_Message')
|
|
|
|
})
|
2018-08-31 18:13:30 +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,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
})
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2018-08-31 18:13:30 +00:00
|
|
|
this.state = {
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
|
|
|
chats: []
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2018-12-21 10:55:35 +00:00
|
|
|
if (!equal(nextState.search, search)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!equal(nextState.chats, chats)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
componentWillUnmount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.querySubscription && this.querySubscription.unsubscribe) {
|
|
|
|
this.querySubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
onPressItem = (item) => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
const onPressItem = navigation.getParam('onPressItem', () => {});
|
2018-12-21 10:55:35 +00:00
|
|
|
onPressItem(item);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
dismiss = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
return navigation.pop();
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
search = async(text) => {
|
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createChannel = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', { nextActionID: 'CREATE_CHANNEL', title: I18n.t('Select_Users') });
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderHeader = () => (
|
|
|
|
<View>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
|
|
|
<Touch onPress={this.createChannel} style={styles.createChannelButton} testID='new-message-view-create-channel'>
|
|
|
|
<View style={[sharedStyles.separatorVertical, styles.createChannelContainer]}>
|
2019-03-01 16:49:11 +00:00
|
|
|
<CustomIcon style={styles.createChannelIcon} size={24} name='plus' />
|
2018-08-31 18:13:30 +00:00
|
|
|
<Text style={styles.createChannelText}>{I18n.t('Create_Channel')}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderSeparator = () => <View style={[sharedStyles.separator, styles.separator]} />;
|
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = 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
|
|
|
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 };
|
|
|
|
}
|
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={item.search ? item.name : item.fname}
|
|
|
|
username={item.search ? item.username : item.name}
|
|
|
|
onPress={() => this.onPressItem(item)}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
testID={`new-message-view-item-${ item.name }`}
|
|
|
|
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 = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2019-09-16 20:26:32 +00:00
|
|
|
data={search.length > 0 ? search : chats}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
render = () => (
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={styles.safeAreaView} testID='new-message-view' forceInset={{ vertical: 'never' }}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
2018-08-31 18:13:30 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(NewMessageView);
|