import React from 'react'; import { StyleSheet, View, Image, Alert, Keyboard, TouchableWithoutFeedback, KeyboardAvoidingView, Platform, } from 'react-native'; import {Button} from 'react-native-elements'; import {SearchBackground, TaggBigInput} from '../../components'; import {SCREEN_WIDTH, StatusBarHeight} from '../../utils'; import AsyncStorage from '@react-native-community/async-storage'; import {RouteProp} from '@react-navigation/native'; import {ProfileStackParams} from 'src/routes'; import {StackNavigationProp} from '@react-navigation/stack'; import {CaptionScreenHeader} from '../../components/'; import {MOMENTS_ENDPOINT} from '../../constants'; import {AuthContext} from '../../routes/authentication'; /** * Upload Screen to allow users to upload posts to Tagg */ type CaptionScreenRouteProp = RouteProp; type CaptionScreenNavigationProp = StackNavigationProp< ProfileStackParams, 'CaptionScreen' >; interface CaptionScreenProps { route: CaptionScreenRouteProp; navigation: CaptionScreenNavigationProp; } const CaptionScreen: React.FC = ({route, navigation}) => { const {title, image} = route.params; const { user: {userId}, updateMoments, } = React.useContext(AuthContext); const [caption, setCaption] = React.useState(''); const handleCaptionUpdate = (caption: string) => { setCaption(caption); }; const checkImageUploadStatus = (statusMap: object) => { for (let [key, value] of Object.entries(statusMap)) { if (value != 'Success') { return false; } } return true; }; const handleShare = async () => { try { const request = new FormData(); const uri = image.path; var fileName = image.filename; //Manipulating filename to end with .jpg instead of .heic if (fileName.endsWith('.heic') || fileName.endsWith('.HEIC')) { fileName = fileName.split('.')[0] + '.jpg'; } request.append('image', { uri: uri, name: fileName, type: 'image/jpg', }); request.append('moment', title); request.append('user_id', userId); request.append('captions', JSON.stringify({image: caption})); const token = await AsyncStorage.getItem('token'); let response = await fetch(MOMENTS_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'multipart/form-data', Authorization: 'Token ' + token, }, body: request, }); let statusCode = response.status; let data = await response.json(); if (statusCode === 200 && checkImageUploadStatus(data)) { Alert.alert('The picture was uploaded successfully!'); updateMoments(true); navigation.navigate('Profile', {isProfileView: false}); } else { Alert.alert('An error occured while uploading. Please try again!'); } } catch (err) { Alert.alert('An error occured during authenticaion. Please login again!'); } }; return (