2017-01-15 02:55:42 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2017-01-15 02:55:42 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-01-15 02:55:42 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-01-15 02:55:42 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2017-01-15 02:55:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {
|
|
|
|
Alert,
|
|
|
|
CameraRoll,
|
|
|
|
Image,
|
|
|
|
ImageEditor,
|
|
|
|
Linking,
|
|
|
|
Platform,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TextInput,
|
|
|
|
TouchableHighlight,
|
|
|
|
View,
|
|
|
|
} = ReactNative;
|
|
|
|
|
2017-01-21 02:40:28 +00:00
|
|
|
const XHRExampleBinaryUpload = require('./XHRExampleBinaryUpload');
|
|
|
|
|
2017-01-15 02:55:42 +00:00
|
|
|
const PAGE_SIZE = 20;
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class XHRExampleFormData extends React.Component<Object, Object> {
|
2017-01-15 02:55:42 +00:00
|
|
|
state: Object = {
|
|
|
|
isUploading: false,
|
|
|
|
uploadProgress: null,
|
|
|
|
randomPhoto: null,
|
|
|
|
textParams: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
_isMounted: boolean = true;
|
|
|
|
|
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
this._fetchRandomPhoto();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fetchRandomPhoto = () => {
|
2018-05-11 20:32:37 +00:00
|
|
|
CameraRoll.getPhotos({first: PAGE_SIZE}).then(data => {
|
|
|
|
if (!this._isMounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var edges = data.edges;
|
|
|
|
var edge = edges[Math.floor(Math.random() * edges.length)];
|
|
|
|
var randomPhoto = edge && edge.node && edge.node.image;
|
|
|
|
if (randomPhoto) {
|
|
|
|
let {width, height} = randomPhoto;
|
|
|
|
width *= 0.25;
|
|
|
|
height *= 0.25;
|
|
|
|
ImageEditor.cropImage(
|
|
|
|
randomPhoto.uri,
|
|
|
|
{offset: {x: 0, y: 0}, size: {width, height}},
|
|
|
|
uri => this.setState({randomPhoto: {uri}}),
|
|
|
|
error => undefined,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, error => undefined);
|
2017-01-15 02:55:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_addTextParam = () => {
|
|
|
|
var textParams = this.state.textParams;
|
|
|
|
textParams.push({name: '', value: ''});
|
|
|
|
this.setState({textParams});
|
|
|
|
};
|
|
|
|
|
|
|
|
_onTextParamNameChange(index, text) {
|
|
|
|
var textParams = this.state.textParams;
|
|
|
|
textParams[index].name = text;
|
|
|
|
this.setState({textParams});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTextParamValueChange(index, text) {
|
|
|
|
var textParams = this.state.textParams;
|
|
|
|
textParams[index].value = text;
|
|
|
|
this.setState({textParams});
|
|
|
|
}
|
|
|
|
|
|
|
|
_upload = () => {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', 'http://posttestserver.com/post.php');
|
|
|
|
xhr.onload = () => {
|
|
|
|
this.setState({isUploading: false});
|
2017-01-21 02:40:28 +00:00
|
|
|
XHRExampleBinaryUpload.handlePostTestServerUpload(xhr);
|
2017-01-15 02:55:42 +00:00
|
|
|
};
|
|
|
|
var formdata = new FormData();
|
|
|
|
if (this.state.randomPhoto) {
|
|
|
|
formdata.append('image', {
|
|
|
|
...this.state.randomPhoto,
|
|
|
|
type: 'image/jpg',
|
|
|
|
name: 'image.jpg',
|
|
|
|
});
|
|
|
|
}
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.textParams.forEach(param =>
|
|
|
|
formdata.append(param.name, param.value),
|
2017-01-15 02:55:42 +00:00
|
|
|
);
|
2018-05-11 20:32:37 +00:00
|
|
|
xhr.upload.onprogress = event => {
|
2017-01-15 02:55:42 +00:00
|
|
|
if (event.lengthComputable) {
|
|
|
|
this.setState({uploadProgress: event.loaded / event.total});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formdata);
|
|
|
|
this.setState({isUploading: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
var image = null;
|
|
|
|
if (this.state.randomPhoto) {
|
|
|
|
image = (
|
2018-05-11 20:32:37 +00:00
|
|
|
<Image source={this.state.randomPhoto} style={styles.randomPhoto} />
|
2017-01-15 02:55:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
var textItems = this.state.textParams.map((item, index) => (
|
|
|
|
<View style={styles.paramRow}>
|
|
|
|
<TextInput
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoCorrect={false}
|
|
|
|
onChangeText={this._onTextParamNameChange.bind(this, index)}
|
|
|
|
placeholder="name..."
|
|
|
|
style={styles.textInput}
|
|
|
|
/>
|
|
|
|
<Text style={styles.equalSign}>=</Text>
|
|
|
|
<TextInput
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoCorrect={false}
|
|
|
|
onChangeText={this._onTextParamValueChange.bind(this, index)}
|
|
|
|
placeholder="value..."
|
|
|
|
style={styles.textInput}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
));
|
|
|
|
var uploadButtonLabel = this.state.isUploading ? 'Uploading...' : 'Upload';
|
|
|
|
var uploadProgress = this.state.uploadProgress;
|
|
|
|
if (uploadProgress !== null) {
|
|
|
|
uploadButtonLabel += ' ' + Math.round(uploadProgress * 100) + '%';
|
|
|
|
}
|
|
|
|
var uploadButton = (
|
|
|
|
<View style={styles.uploadButtonBox}>
|
|
|
|
<Text style={styles.uploadButtonLabel}>{uploadButtonLabel}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
if (!this.state.isUploading) {
|
|
|
|
uploadButton = (
|
|
|
|
<TouchableHighlight onPress={this._upload}>
|
|
|
|
{uploadButton}
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<View style={styles.paramRow}>
|
|
|
|
<Text style={styles.photoLabel}>
|
2018-05-11 20:32:37 +00:00
|
|
|
Random photo from your library (<Text
|
|
|
|
style={styles.textButton}
|
|
|
|
onPress={this._fetchRandomPhoto}>
|
2017-01-15 02:55:42 +00:00
|
|
|
update
|
|
|
|
</Text>)
|
|
|
|
</Text>
|
|
|
|
{image}
|
|
|
|
</View>
|
|
|
|
{textItems}
|
|
|
|
<View>
|
|
|
|
<Text
|
|
|
|
style={[styles.textButton, styles.addTextParamButton]}
|
|
|
|
onPress={this._addTextParam}>
|
|
|
|
Add a text param
|
|
|
|
</Text>
|
|
|
|
</View>
|
2018-05-11 20:32:37 +00:00
|
|
|
<View style={styles.uploadButton}>{uploadButton}</View>
|
2017-01-15 02:55:42 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
paramRow: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
paddingVertical: 8,
|
|
|
|
alignItems: 'center',
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderBottomColor: 'grey',
|
|
|
|
},
|
|
|
|
photoLabel: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
randomPhoto: {
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
},
|
|
|
|
textButton: {
|
|
|
|
color: 'blue',
|
|
|
|
},
|
|
|
|
addTextParamButton: {
|
|
|
|
marginTop: 8,
|
|
|
|
},
|
|
|
|
textInput: {
|
|
|
|
flex: 1,
|
|
|
|
borderRadius: 3,
|
|
|
|
borderColor: 'grey',
|
|
|
|
borderWidth: 1,
|
|
|
|
height: Platform.OS === 'android' ? 50 : 30,
|
|
|
|
paddingLeft: 8,
|
|
|
|
},
|
|
|
|
equalSign: {
|
|
|
|
paddingHorizontal: 4,
|
|
|
|
},
|
|
|
|
uploadButton: {
|
|
|
|
marginTop: 16,
|
|
|
|
},
|
|
|
|
uploadButtonBox: {
|
|
|
|
flex: 1,
|
|
|
|
paddingVertical: 12,
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: 'blue',
|
|
|
|
borderRadius: 4,
|
|
|
|
},
|
|
|
|
uploadButtonLabel: {
|
|
|
|
color: 'white',
|
|
|
|
fontSize: 16,
|
|
|
|
fontWeight: '500',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = XHRExampleFormData;
|