verdnatura-chat/app/containers/MessageBox/index.js

187 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-08-09 13:12:00 +00:00
import React from 'react';
import PropTypes from 'prop-types';
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';
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';
@connect(state => ({
room: state.room,
message: state.messages.message,
editing: state.messages.editing
}), dispatch => ({
2017-11-24 20:44:52 +00:00
editCancel: () => dispatch(editCancel()),
editRequest: message => dispatch(editRequest(message)),
2017-11-24 20:44:52 +00:00
typing: status => dispatch(userTyping(status)),
clearInput: () => dispatch(clearInput())
}))
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,
rid: PropTypes.string.isRequired,
2017-11-24 20:44:52 +00:00
editCancel: PropTypes.func.isRequired,
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
}
constructor(props) {
super(props);
this.state = {
height: 20,
text: ''
};
}
componentWillReceiveProps(nextProps) {
2017-11-24 20:44:52 +00:00
if (this.props.message !== nextProps.message && nextProps.message) {
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-08-09 13:12:00 +00:00
}
onChangeText(text) {
this.setState({ text });
this.props.typing(text.length > 0);
}
get leftButtons() {
const { editing } = this.props;
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'
/>);
}
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'
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'
onPress={() => this.addFile()}
/>);
return icons;
2017-08-11 18:18:09 +00:00
}
2017-08-09 13:12:00 +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'
};
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: '' });
}
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() {
const { height } = this.state;
2017-08-09 13:12:00 +00:00
return (
<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}
style={[styles.textBoxInput, { height }]}
returnKeyType='default'
2017-11-13 12:49:19 +00:00
blurOnSubmit={false}
placeholder='New Message'
onChangeText={text => this.onChangeText(text)}
2017-11-13 12:49:19 +00:00
underlineColorAndroid='transparent'
defaultValue=''
multiline
placeholderTextColor='#9EA2A8'
onContentSizeChange={e => this.updateSize(e.nativeEvent.contentSize.height)}
/>
{this.rightButtons}
</View>
</SafeAreaView>
2017-08-09 13:12:00 +00:00
);
}
}