import React from "react"; import { View, ImageBackground, ScrollView } from "react-native"; import { Text, TextInput, Button, Divider, Checkbox } from "react-native-paper"; import i18n from "../i18nMessages.js"; import Moment from 'moment'; import 'moment/min/locales'; import ProfileCardHorizontal from "../components/ProfileCardHorizontal.js"; Moment.locale(i18n.locale); import { useSnapshot } from 'valtio'; import GlobalState from '../contexts/GlobalState.js'; import API from "../API.js"; import * as ImagePicker from 'expo-image-picker'; let ProfileSettings = ()=>{ const gState = useSnapshot(GlobalState); const viewer = gState.me; const [photo, setPhoto] = React.useState(null); const [name, setName] = React.useState(viewer.profile.firstName); const [lastName, setLastName] = React.useState(viewer.profile.lastName); const [photoUrl, setphotoUrl] = React.useState(viewer.profile.photo); const [updateKey, setUpdateKey] = React.useState(0); const [description, setDescription] = React.useState(viewer.profile.description); 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: 0.7, //allowsMultipleSelection: true, }); if (!result.cancelled) { setPhoto(result); let newPhotoURL = await handleUploadPhoto(result); if(newPhotoURL !== ""){ setphotoUrl(newPhotoURL); GlobalState.me.profile.photo = newPhotoURL; setUpdateKey(updateKey+1); } 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, }); let uploadedFile = ''; try { 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) => { return data.fileName; }) .catch((err) => console.error(err)); } catch (err) { console.log(err); alert("Something went wrong"); } return uploadedFile; }; let updateProfile = () => { GlobalState.me.profile.firstName = name; GlobalState.me.profile.lastName = lastName; GlobalState.me.profile.description = description; API.updateMyProfile(viewer.profile, viewer.data); setUpdateKey(updateKey+1); } return ( Profile Settings setName(text)} /> setLastName(text)} /> setDescription(text)} /> Preview: Optional: setName(text)} /> setLastName(text)} /> setLastName(text)} /> setLastName(text)} /> setLastName(text)} /> setLastName(text)} /> ) } export default ProfileSettings;