Upload images for posts

This commit is contained in:
Adolfo Reyna
2022-11-22 12:40:15 -05:00
parent 5ff2c7971f
commit bae9d667fe
+33 -20
View File
@@ -1,13 +1,15 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { View, StyleSheet, Image } from 'react-native'; import { View, StyleSheet, Image, Text } from 'react-native';
import { TextInput, Button } from 'react-native-paper'; import { TextInput, Button } from 'react-native-paper';
import API from './../API.js'; import API from './../API.js';
import { useNavigation } from '@react-navigation/native'; import { useNavigation } from '@react-navigation/native';
import * as ImagePicker from 'expo-image-picker'; import * as ImagePicker from 'expo-image-picker';
import Media from './Media.js';
let NewPost = ({ profileid, newPostCB }) => { let NewPost = ({ profileid, newPostCB }) => {
let [postContent, setPostContent] = useState(''); let [postContent, setPostContent] = useState('');
let [extraContent, setExtraContent] = useState([]);
const navigation = useNavigation(); const navigation = useNavigation();
const [photo, setPhoto] = React.useState(null); const [photo, setPhoto] = React.useState(null);
@@ -23,7 +25,11 @@ let NewPost = ({ profileid, newPostCB }) => {
}); });
if (!result.cancelled) { if (!result.cancelled) {
setPhoto(result); setPhoto(result);
await handleUploadPhoto(result); let newPhotoURL = await handleUploadPhoto(result);
let newExtraContent = ["@image:" + newPhotoURL].concat(extraContent);
setExtraContent(newExtraContent);
console.log(newExtraContent.join(" "));
setPhoto(null);
} }
}; };
@@ -44,23 +50,33 @@ let NewPost = ({ profileid, newPostCB }) => {
type, type,
}); });
try { try {
fetch("https://social.emmint.com/upload.php", { let uploadedFile = await fetch("https://social.emmint.com/upload.php", {
method: "POST", method: "POST",
body: formData, body: formData,
headers: { "Content-Type": "multipart/form-data" } headers: { "Content-Type": "multipart/form-data" }
}) })
.then((res) => res.json()) .then((res) => res.json())
.then((data) => { .then((data) => {
console.log(data); console.log(data);
return data.fileName; return data.fileName;
}) })
.catch((err) => console.error(err)); .catch((err) => console.error(err));
return uploadedFile;
} catch (err) { } catch (err) {
console.log(err); console.log(err);
alert("Something went wrong"); alert("Something went wrong");
} }
}; };
const handleNewPostButton = async () => {
//setPostContent('');
API.newPost(postContent + " " + extraContent.join(" ")).then((newPost) => {
setPostContent('');
setExtraContent([]);
if (newPostCB) newPostCB(newPost);
});
}
return ( return (
@@ -77,25 +93,22 @@ let NewPost = ({ profileid, newPostCB }) => {
<> <>
<View style={{ flexDirection: "row", justifyContent: 'flex-end' }}> <View style={{ flexDirection: "row", justifyContent: 'flex-end' }}>
<Button icon="add-a-photo" mode="outlined" onPress={pickImage} ></Button> <Button icon="add-a-photo" mode="outlined" onPress={pickImage} ></Button>
<Button icon="send" mode="outlined" onPress={() => { <Button icon="send" mode="outlined" onPress={handleNewPostButton}>Post</Button>
setPostContent('');
API.newPost(postContent).then((newPost) => {
setPostContent('');
if (newPostCB) newPostCB(newPost);
});
}}>Post</Button>
</View> </View>
{photo && ( {photo && (
<View> <View>
<Text>Uploading...</Text>
<Image <Image
source={{ uri: photo.uri }} source={{ uri: photo.uri }}
style={{ width: 300, height: 300 }} style={{ width: 100, height: 100 }}
/> />
<Button title="Upload Photo" onPress={handleUploadPhoto} />
</View> </View>
)} )}
{
extraContent.length > 0 && (
<Media content={extraContent.join(" ")} />
)
}
</> </>
) : undefined ) : undefined
} }