Rocket.Chat.ReactNative/app/components/MessageBox.js

121 lines
2.8 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 } 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';
import RNFetchBlob from 'react-native-fetch-blob';
import RocketChat from '../lib/rocketchat';
2017-08-09 13:12:00 +00:00
const styles = StyleSheet.create({
textBox: {
paddingTop: 1,
2017-08-10 20:09:54 +00:00
borderTopWidth: 1,
borderTopColor: '#ccc',
backgroundColor: '#fff',
flexDirection: 'row',
alignItems: 'center'
2017-08-09 13:12:00 +00:00
},
textBoxInput: {
height: 40,
backgroundColor: '#fff',
2017-08-10 20:09:54 +00:00
flexGrow: 1
},
fileButton: {
color: '#aaa',
paddingLeft: 23,
paddingRight: 20,
paddingTop: 10,
paddingBottom: 10,
fontSize: 20
2017-08-09 13:12:00 +00:00
}
});
export default class MessageBox extends React.PureComponent {
static propTypes = {
2017-08-10 20:09:54 +00:00
onSubmit: PropTypes.func.isRequired,
rid: PropTypes.string.isRequired
2017-08-09 13:12:00 +00:00
}
constructor(props) {
super(props);
this.state = {
text: ''
};
}
submit = () => {
if (this.state.text.trim() === '') {
return;
}
2017-08-09 18:15:44 +00:00
this.props.onSubmit(this.state.text);
this.setState({ text: '' });
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) => {
console.log('Response = ', 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',
rid: this.props.rid,
// description: '',
store: 'Uploads'
};
RocketChat.ufsCreate(fileInfo).then((result) => {
RNFetchBlob.fetch('POST', result.url, {
'Content-Type': 'application/octet-stream'
}, response.data).then(() => {
RocketChat.ufsComplete(result.fileId, fileInfo.store, result.token)
.then((completeRresult) => {
RocketChat.sendFileMessage(completeRresult.rid, {
_id: completeRresult._id,
type: completeRresult.type,
size: completeRresult.size,
name: completeRresult.name,
url: completeRresult.path
});
});
}).catch((err) => {
console.log({ err });
});
});
}
});
}
2017-08-09 13:12:00 +00:00
render() {
return (
<View style={styles.textBox}>
2017-08-10 20:09:54 +00:00
<Icon style={styles.fileButton} name='add-circle-outline' onPress={this.addFile} />
2017-08-09 13:12:00 +00:00
<TextInput
style={styles.textBoxInput}
value={this.state.text}
onChangeText={text => this.setState({ text })}
returnKeyType='send'
onSubmitEditing={this.submit}
blurOnSubmit={false}
placeholder='New message'
/>
</View>
);
}
}