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';
|
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-12-04 16:39:53 +00:00
|
|
|
import Touch from '../utils/touch';
|
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 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-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-03-30 19:50:27 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { createChannelRequest } from '../actions/createChannel';
|
2020-05-13 19:02:57 +00:00
|
|
|
import { goRoom } from '../utils/goRoom';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
separator: {
|
|
|
|
marginLeft: 60
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
button: {
|
2019-12-04 16:39:53 +00:00
|
|
|
height: 46,
|
2018-08-31 18:13:30 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonIcon: {
|
2019-06-10 16:22:35 +00:00
|
|
|
marginLeft: 18,
|
2020-03-30 19:50:27 +00:00
|
|
|
marginRight: 16
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 17,
|
|
|
|
...sharedStyles.textRegular
|
2020-04-01 12:28:54 +00:00
|
|
|
},
|
|
|
|
buttonContainer: {
|
|
|
|
paddingVertical: 25
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewMessageView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
headerLeft: () => <CloseModalButton navigation={navigation} testID='new-message-view-close' />,
|
2019-03-12 16:23:06 +00:00
|
|
|
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
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2020-04-01 12:28:54 +00:00
|
|
|
createChannel: PropTypes.func,
|
|
|
|
maxUsers: PropTypes.number,
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
isMasterDetail: PropTypes.bool
|
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;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', { nextAction: () => navigation.navigate('CreateChannelView') });
|
|
|
|
}
|
|
|
|
|
|
|
|
createGroupChat = () => {
|
|
|
|
const { createChannel, maxUsers, navigation } = this.props;
|
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
|
|
|
nextAction: () => createChannel({ group: true }),
|
|
|
|
buttonText: I18n.t('Create'),
|
|
|
|
maxUsers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom = (item) => {
|
|
|
|
const { isMasterDetail, navigation } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
renderButton = ({
|
|
|
|
onPress, testID, title, icon, first
|
|
|
|
}) => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<Touch
|
|
|
|
onPress={onPress}
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
|
|
|
testID={testID}
|
|
|
|
theme={theme}
|
|
|
|
>
|
|
|
|
<View style={[first ? sharedStyles.separatorVertical : sharedStyles.separatorBottom, styles.button, { borderColor: themes[theme].separatorColor }]}>
|
|
|
|
<CustomIcon style={[styles.buttonIcon, { color: themes[theme].tintColor }]} size={24} name={icon} />
|
|
|
|
<Text style={[styles.buttonText, { color: themes[theme].tintColor }]}>{title}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
createDiscussion = () => {
|
|
|
|
Navigation.navigate('CreateDiscussionView');
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { maxUsers, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
2020-04-01 12:28:54 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
|
|
|
{this.renderButton({
|
|
|
|
onPress: this.createChannel,
|
|
|
|
title: I18n.t('Create_Channel'),
|
2020-06-05 13:28:58 +00:00
|
|
|
icon: 'hash',
|
2020-04-01 12:28:54 +00:00
|
|
|
testID: 'new-message-view-create-channel',
|
|
|
|
first: true
|
|
|
|
})}
|
|
|
|
{maxUsers > 2 ? this.renderButton({
|
|
|
|
onPress: this.createGroupChat,
|
|
|
|
title: I18n.t('Create_Direct_Messages'),
|
|
|
|
icon: 'team',
|
|
|
|
testID: 'new-message-view-create-direct-message'
|
|
|
|
}) : null}
|
|
|
|
{this.renderButton({
|
|
|
|
onPress: this.createDiscussion,
|
|
|
|
title: I18n.t('Create_Discussion'),
|
|
|
|
icon: 'chat',
|
|
|
|
testID: 'new-message-view-create-discussion'
|
|
|
|
})}
|
|
|
|
</View>
|
2019-12-04 16:39:53 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <View style={[sharedStyles.separator, styles.separator, { backgroundColor: themes[theme].separatorColor }]} />;
|
|
|
|
}
|
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
|
|
|
|
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={item.search ? item.name : item.fname}
|
|
|
|
username={item.search ? item.username : item.name}
|
2020-06-15 14:00:46 +00:00
|
|
|
onPress={() => this.goRoom(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}
|
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;
|
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}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
render = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<SafeAreaView testID='new-message-view' theme={theme}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers: state.settings.DirectMesssage_maxUsers || 1,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
createChannel: params => dispatch(createChannelRequest(params))
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewMessageView));
|