verdnatura-chat/app/containers/MessageBox.js

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-08-09 13:12:00 +00:00
import React from 'react';
import PropTypes from 'prop-types';
2017-11-13 12:49:19 +00:00
import { View, TextInput, StyleSheet, SafeAreaView } from 'react-native';
2017-08-10 20:09:54 +00:00
import Icon from 'react-native-vector-icons/MaterialIcons';
import ImagePicker from 'react-native-image-picker';
2017-11-21 16:55:32 +00:00
import { connect } from 'react-redux';
2017-11-21 17:09:22 +00:00
import { userTyping } from '../actions/room';
2017-08-10 20:09:54 +00:00
import RocketChat from '../lib/rocketchat';
import { editRequest } from '../actions/messages';
2017-08-09 13:12:00 +00:00
const styles = StyleSheet.create({
textBox: {
paddingTop: 1,
2017-11-21 16:55:32 +00:00
paddingHorizontal: 15,
2017-08-10 20:09:54 +00:00
borderTopWidth: 1,
borderTopColor: '#ccc',
2017-11-13 12:49:19 +00:00
backgroundColor: '#fff'
},
safeAreaView: {
2017-08-10 20:09:54 +00:00
flexDirection: 'row',
alignItems: 'center'
2017-08-09 13:12:00 +00:00
},
textBoxInput: {
height: 40,
2017-08-11 18:18:09 +00:00
alignSelf: 'stretch',
2017-08-10 20:09:54 +00:00
flexGrow: 1
},
fileButton: {
color: '#aaa',
paddingTop: 10,
paddingBottom: 10,
fontSize: 20
},
editing: {
backgroundColor: '#fff5df'
2017-08-09 13:12:00 +00:00
}
});
@connect(state => ({
message: state.messages.message,
editing: state.messages.editing
}), dispatch => ({
editRequest: message => dispatch(editRequest(message)),
2017-11-21 17:09:22 +00:00
typing: status => dispatch(userTyping(status))
}))
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,
editRequest: PropTypes.func.isRequired,
message: PropTypes.object,
2017-11-21 17:09:22 +00:00
editing: PropTypes.bool,
typing: PropTypes.bool
}
componentWillReceiveProps(nextProps) {
if (this.props.message !== nextProps.message) {
this.component.setNativeProps({ text: nextProps.message.msg });
this.component.focus();
}
2017-08-09 13:12:00 +00:00
}
2017-08-11 18:18:09 +00:00
submit(message) {
const { editing } = this.props;
if (message.trim() === '') {
2017-08-09 13:12:00 +00:00
return;
}
// if is editing a message
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-08-22 01:24:41 +00:00
}
this.component.setNativeProps({ text: '' });
2017-08-11 18:18:09 +00:00
}
2017-08-09 13:12:00 +00:00
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-08-09 13:12:00 +00:00
render() {
return (
<View style={[styles.textBox, (this.props.editing ? styles.editing : null)]}>
2017-11-13 12:49:19 +00:00
<SafeAreaView style={styles.safeAreaView}>
<TextInput
ref={component => this.component = component}
style={styles.textBoxInput}
returnKeyType='send'
onSubmitEditing={event => this.submit(event.nativeEvent.text)}
blurOnSubmit={false}
placeholder='New message'
2017-11-21 16:55:32 +00:00
onChangeText={text => this.props.typing(text.length > 0)}
2017-11-13 12:49:19 +00:00
underlineColorAndroid='transparent'
defaultValue=''
/>
2017-11-21 16:55:32 +00:00
<Icon style={styles.fileButton} name='add-circle-outline' onPress={this.addFile} />
2017-11-13 12:49:19 +00:00
</SafeAreaView>
2017-08-09 13:12:00 +00:00
</View>
);
}
}