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,
|
2017-08-11 18:18:09 +00:00
|
|
|
alignSelf: 'stretch',
|
2017-08-09 13:12:00 +00:00
|
|
|
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: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
submit(message) {
|
|
|
|
// console.log(this.state);
|
|
|
|
const text = message;
|
|
|
|
this.setState({ text: '' });
|
|
|
|
if (text.trim() === '') {
|
2017-08-09 13:12:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-11 18:18:09 +00:00
|
|
|
this.props.onSubmit(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
|
2017-08-11 18:18:09 +00:00
|
|
|
ref={component => this.component = component}
|
2017-08-09 13:12:00 +00:00
|
|
|
style={styles.textBoxInput}
|
|
|
|
value={this.state.text}
|
|
|
|
onChangeText={text => this.setState({ text })}
|
|
|
|
returnKeyType='send'
|
2017-08-11 18:18:09 +00:00
|
|
|
onSubmitEditing={event => this.submit(event.nativeEvent.text)}
|
2017-08-09 13:12:00 +00:00
|
|
|
blurOnSubmit={false}
|
|
|
|
placeholder='New message'
|
2017-08-11 18:18:09 +00:00
|
|
|
underlineColorAndroid='transparent'
|
2017-08-09 13:12:00 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|