import React, { useState } from 'react'; import { View, TextInput, Image } from "react-native"; import { Text, Button, Divider } from "react-native-paper"; import { SafeAreaView } from "react-native-safe-area-context"; import API from './../API.js'; import { useNavigation } from '@react-navigation/native'; import * as ImagePicker from 'expo-image-picker'; import i18n from "../i18nMessages"; import Media from '../components/Media'; let NewPostView = ({writeTo})=>{ let [postContent, setPostContent] = useState(''); let [extraContent, setExtraContent] = useState([]); const [photo, setPhoto] = React.useState(null); const navigation = useNavigation(); const pickImage = async () => { // No permissions request is necessary for launching the image library let result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, //allowsEditing: true, //aspect: [4, 3], //quality: 1, allowsMultipleSelection: true, }); if (!result.cancelled) { setPhoto(result); let newPhotoURL = await handleUploadPhoto(result); let newExtraContent = ["@image:" + newPhotoURL].concat(extraContent); setExtraContent(newExtraContent); console.log(newExtraContent.join(" ")); setPhoto(null); } }; const handleUploadPhoto = async (photo) => { if (!photo) return; const uri = Platform.OS === "android" ? photo.uri : photo.uri.replace("file://", ""); const filename = photo.uri.split("/").pop(); const match = /\.(\w+)$/.exec(filename); const ext = match?.[1]; const type = match ? `image/${match[1]}` : `image`; const formData = new FormData(); formData.append("banner", { uri, name: `image.${ext}`, type, }); try { let uploadedFile = await fetch("https://social.emmint.com/upload.php", { method: "POST", body: formData, headers: { "Content-Type": "multipart/form-data" } }) .then((res) => res.json()) .then((data) => { console.log(data); return data.fileName; }) .catch((err) => console.error(err)); return uploadedFile; } catch (err) { console.log(err); alert("Something went wrong"); } }; const handleNewPostButton = async () => { //setPostContent(''); API.newPost(postContent + " " + extraContent.join(" ")).then((newPost) => { setPostContent(''); setExtraContent([]); navigation.goBack(); //if (newPostCB) newPostCB(newPost); }); } return ( {i18n.t("message.statusUpdate")}: {photo && ( Uploading... )} { extraContent.length > 0 && ( ) } ) } export default NewPostView;