-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
103 lines (90 loc) · 2.18 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, {useState} from 'react';
import {View, TouchableOpacity, Text, StyleSheet, Image} from 'react-native';
import ImagePicker from 'react-native-image-picker';
import Axios from 'axios';
export default function Upload() {
const [avatar, setAvatar] = useState();
const imagePickerOptions = {
title: 'Selecione uma imagem',
customButtons: [
{
name: 'fb',
title: 'Selecione uma imagem do facebook',
},
{
name: 'ig',
title: 'Selecione uma imagem do instagram',
},
],
};
function imagePickerCallback(data) {
if (data.didCancel) {
return;
}
if (data.error) {
return;
}
if (data.customButton) {
return;
}
if (!data.uri) {
return;
}
setAvatar(data);
}
async function uploadImage() {
const data = new FormData();
data.append('avatar', {
fileName: avatar.fileName,
uri: avatar.uri,
type: avatar.type,
});
await Axios.post('http://localhost:3333/files', data);
}
return (
<View style={styles.container}>
<Image
source={{
uri: avatar
? avatar.uri
: 'https://mltmpgeox6sf.i.optimole.com/w:761/h:720/q:auto/https://redbanksmilesnj.com/wp-content/uploads/2015/11/man-avatar-placeholder.png',
}}
style={styles.avatar}
/>
<TouchableOpacity
style={styles.button}
onPress={() =>
ImagePicker.showImagePicker(imagePickerOptions, imagePickerCallback)
}>
<Text style={styles.buttonText}>Escolher imagem</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={uploadImage}>
<Text style={styles.buttonText}>Enviar imagem</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
button: {
width: 150,
height: 50,
borderRadius: 3,
backgroundColor: '#7159c1',
justifyContent: 'center',
alignItems: 'center',
marginTop: 20,
},
buttonText: {
color: '#fff',
},
avatar: {
width: 100,
height: 100,
borderRadius: 50,
},
});