verdnatura-chat/app/containers/MessageBox.js

171 lines
4.4 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, StyleSheet, SafeAreaView, Platform } 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';
2017-11-24 20:44:52 +00:00
import { editRequest, editCancel, clearInput } 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,
minHeight: 40,
maxHeight: 120,
flexGrow: 1,
paddingHorizontal: 10,
paddingTop: 12
2017-08-10 20:09:54 +00:00
},
2017-11-24 20:44:52 +00:00
actionButtons: {
2017-08-10 20:09:54 +00:00
color: '#aaa',
paddingTop: 10,
paddingBottom: 10,
paddingHorizontal: 8,
fontSize: 20,
alignSelf: 'flex-end'
},
editing: {
backgroundColor: '#fff5df'
2017-08-09 13:12:00 +00:00
}
});
@connect(state => ({
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: 40
};
}
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
}
2017-08-11 18:18:09 +00:00
submit(message) {
this.component.setNativeProps({ text: '' });
this.props.typing(false);
if (message.trim() === '') {
2017-08-09 13:12:00 +00:00
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-08-22 01:24:41 +00:00
}
2017-11-24 20:44:52 +00:00
this.props.clearInput();
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
}
});
}
updateSize = (height) => {
this.setState({ height: height + (Platform.OS === 'ios' ? 20 : 0) });
}
2017-11-24 20:44:52 +00:00
editCancel() {
this.props.editCancel();
this.component.setNativeProps({ text: '' });
}
renderLeftButton() {
const { editing } = this.props;
if (editing) {
return <Icon style={styles.actionButtons} name='close' onPress={() => this.editCancel()} />;
}
return <Icon style={styles.actionButtons} name='add-circle-outline' onPress={this.addFile} />;
}
2017-08-09 13:12:00 +00:00
render() {
const { height } = this.state;
2017-08-09 13:12:00 +00:00
return (
<View style={[styles.textBox, (this.props.editing ? styles.editing : null)]}>
2017-11-13 12:49:19 +00:00
<SafeAreaView style={styles.safeAreaView}>
2017-11-24 20:44:52 +00:00
{this.renderLeftButton()}
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'
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=''
multiline
onContentSizeChange={e => this.updateSize(e.nativeEvent.contentSize.height)}
/>
<Icon
style={styles.actionButtons}
name='send'
onPress={() => this.submit(this.component._lastNativeText)}
2017-11-13 12:49:19 +00:00
/>
</SafeAreaView>
2017-08-09 13:12:00 +00:00
</View>
);
}
}