2017-08-09 13:12:00 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-12-08 19:36:03 +00:00
|
|
|
import { View, TextInput, SafeAreaView, Platform } from 'react-native';
|
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2017-08-10 20:09:54 +00:00
|
|
|
import ImagePicker from 'react-native-image-picker';
|
2017-11-21 16:55:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-12-08 19:36:03 +00:00
|
|
|
import { userTyping } from '../../actions/room';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import { editRequest, editCancel, clearInput } from '../../actions/messages';
|
|
|
|
import styles from './style';
|
|
|
|
import MyIcon from '../icons';
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
2017-12-08 19:36:03 +00:00
|
|
|
room: state.room,
|
2017-11-21 14:55:50 +00:00
|
|
|
message: state.messages.message,
|
|
|
|
editing: state.messages.editing
|
|
|
|
}), dispatch => ({
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: () => dispatch(editCancel()),
|
2017-11-21 17:05:14 +00:00
|
|
|
editRequest: message => dispatch(editRequest(message)),
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: status => dispatch(userTyping(status)),
|
|
|
|
clearInput: () => dispatch(clearInput())
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
|
|
|
export default class MessageBox extends React.Component {
|
2017-08-09 13:12:00 +00:00
|
|
|
static propTypes = {
|
2017-08-10 20:09:54 +00:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
rid: PropTypes.string.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: PropTypes.func.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
editRequest: PropTypes.func.isRequired,
|
|
|
|
message: PropTypes.object,
|
2017-11-21 17:09:22 +00:00
|
|
|
editing: PropTypes.bool,
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: PropTypes.func,
|
|
|
|
clearInput: PropTypes.func
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:27:06 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-12-08 19:36:03 +00:00
|
|
|
height: 20,
|
|
|
|
text: ''
|
2017-11-28 16:27:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-11-24 20:44:52 +00:00
|
|
|
if (this.props.message !== nextProps.message && nextProps.message) {
|
2017-11-21 14:55:50 +00:00
|
|
|
this.component.setNativeProps({ text: nextProps.message.msg });
|
|
|
|
this.component.focus();
|
2017-11-24 20:44:52 +00:00
|
|
|
} else if (!nextProps.message) {
|
|
|
|
this.component.setNativeProps({ text: '' });
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
}
|
2017-12-08 19:36:03 +00:00
|
|
|
onChangeText(text) {
|
|
|
|
this.setState({ text });
|
|
|
|
this.props.typing(text.length > 0);
|
|
|
|
}
|
|
|
|
get leftButtons() {
|
2017-11-24 17:21:21 +00:00
|
|
|
const { editing } = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
if (editing) {
|
2017-12-11 20:37:33 +00:00
|
|
|
return (<Icon
|
|
|
|
style={styles.actionButtons}
|
|
|
|
name='ios-close'
|
|
|
|
accessibilityLabel='Cancel editing'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
onPress={() => this.editCancel()}
|
|
|
|
/>);
|
2017-08-22 01:24:41 +00:00
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
return !this.state.emoji ? (<Icon
|
|
|
|
style={styles.actionButtons}
|
|
|
|
onPress={() => this.openEmoji()}
|
|
|
|
accessibilityLabel='Open emoji selector'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
name='md-happy'
|
|
|
|
/>) : (<Icon
|
|
|
|
onPress={() => this.openEmoji()}
|
|
|
|
style={styles.actionButtons}
|
|
|
|
accessibilityLabel='Close emoji selector'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
name='md-sad'
|
|
|
|
/>);
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
|
|
|
get rightButtons() {
|
|
|
|
const icons = [];
|
|
|
|
|
|
|
|
if (this.state.text.length) {
|
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#1D74F5' }]}
|
|
|
|
name='send'
|
|
|
|
key='sendIcon'
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Send message'
|
|
|
|
accessibilityTraits='button'
|
2017-12-08 19:36:03 +00:00
|
|
|
onPress={() => this.submit(this.component._lastNativeText)}
|
|
|
|
/>);
|
|
|
|
}
|
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#2F343D', fontSize: 16 }]}
|
|
|
|
name='plus'
|
|
|
|
key='fileIcon'
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Message actions'
|
|
|
|
accessibilityTraits='button'
|
2017-12-08 19:36:03 +00:00
|
|
|
onPress={() => this.addFile()}
|
|
|
|
/>);
|
|
|
|
return icons;
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
// get placeholder() {
|
|
|
|
// return `New Message`.substring(0, 35);
|
|
|
|
// }
|
|
|
|
updateSize = (height) => {
|
|
|
|
this.setState({ height: height + (Platform.OS === 'ios' ? 0 : 0) });
|
|
|
|
}
|
2017-08-10 20:09:54 +00:00
|
|
|
addFile = () => {
|
|
|
|
const options = {
|
|
|
|
customButtons: [{
|
|
|
|
name: 'import', title: 'Import File From'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
ImagePicker.showImagePicker(options, (response) => {
|
|
|
|
if (response.didCancel) {
|
|
|
|
console.log('User cancelled image picker');
|
|
|
|
} else if (response.error) {
|
|
|
|
console.log('ImagePicker Error: ', response.error);
|
|
|
|
} else if (response.customButton) {
|
|
|
|
console.log('User tapped custom button: ', response.customButton);
|
|
|
|
} else {
|
|
|
|
const fileInfo = {
|
|
|
|
name: response.fileName,
|
|
|
|
size: response.fileSize,
|
|
|
|
type: response.type || 'image/jpeg',
|
|
|
|
// description: '',
|
|
|
|
store: 'Uploads'
|
|
|
|
};
|
2017-08-15 19:28:46 +00:00
|
|
|
RocketChat.sendFileMessage(this.props.rid, fileInfo, response.data);
|
2017-08-10 20:09:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel() {
|
|
|
|
this.props.editCancel();
|
|
|
|
this.component.setNativeProps({ text: '' });
|
|
|
|
}
|
2017-12-08 19:36:03 +00:00
|
|
|
openEmoji() {
|
|
|
|
this.setState({ emoji: !this.state.emoji });
|
|
|
|
}
|
|
|
|
submit(message) {
|
|
|
|
this.component.setNativeProps({ text: '' });
|
|
|
|
this.props.clearInput();
|
|
|
|
this.setState({ text: '' });
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.props.typing(false);
|
|
|
|
if (message.trim() === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if is editing a message
|
|
|
|
const { editing } = this.props;
|
|
|
|
if (editing) {
|
|
|
|
const { _id, rid } = this.props.message;
|
|
|
|
this.props.editRequest({ _id, msg: message, rid });
|
|
|
|
} else {
|
|
|
|
// if is submiting a new message
|
|
|
|
this.props.onSubmit(message);
|
|
|
|
}
|
|
|
|
});
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 13:12:00 +00:00
|
|
|
render() {
|
2017-11-28 16:27:06 +00:00
|
|
|
const { height } = this.state;
|
2017-08-09 13:12:00 +00:00
|
|
|
return (
|
2017-12-08 19:36:03 +00:00
|
|
|
<SafeAreaView style={[styles.textBox, (this.props.editing ? styles.editing : null)]}>
|
|
|
|
<View style={styles.textArea}>
|
|
|
|
{this.leftButtons}
|
2017-11-13 12:49:19 +00:00
|
|
|
<TextInput
|
|
|
|
ref={component => this.component = component}
|
2017-11-28 16:27:06 +00:00
|
|
|
style={[styles.textBoxInput, { height }]}
|
|
|
|
returnKeyType='default'
|
2017-11-13 12:49:19 +00:00
|
|
|
blurOnSubmit={false}
|
2017-12-08 19:36:03 +00:00
|
|
|
placeholder='New Message'
|
|
|
|
onChangeText={text => this.onChangeText(text)}
|
2017-11-13 12:49:19 +00:00
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
defaultValue=''
|
2017-11-28 16:27:06 +00:00
|
|
|
multiline
|
2017-12-08 19:36:03 +00:00
|
|
|
placeholderTextColor='#9EA2A8'
|
2017-11-28 16:27:06 +00:00
|
|
|
onContentSizeChange={e => this.updateSize(e.nativeEvent.contentSize.height)}
|
|
|
|
/>
|
2017-12-08 19:36:03 +00:00
|
|
|
{this.rightButtons}
|
|
|
|
</View>
|
|
|
|
</SafeAreaView>
|
2017-08-09 13:12:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|