0

The output on the other pageThe way my current code works, is that the user is prompted to input a song, artist, and as of now a photo. These three inputs are put onto a card, and sent to a different screen to be viewed. Underneath these, I also have the code that allows a user to access their camera roll and upload a picture. I was wondering, is there any way I can combine both of these, and replace the text input of photo with the uploaded photo?

I tried changing the let postPhoto to uri but that doesn't work, and I don't think string works either. Whenever I assign postPhoto to image I get an error as well

import { StatusBar } from 'expo-status-bar';
import { Platform, StyleSheet, TextInput, SafeAreaView, Pressable, TouchableOpacity, Image, Button } from 'react-native';
import { useState } from 'react';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { useNavigation } from '@react-navigation/native';
import { CompositeScreenProps } from '@react-navigation/native';
import { RootStackScreenProps } from '../types';
import { getCurrentUser} from '../components/makeAccount';
import * as ImagePicker from 'expo-image-picker';

let postSong:string
let postArtist:string
let postPhoto: string
let userPost: JSX.Element
let saved :boolean  = false
export function getSaved(){
  return saved
}
export function getUserPost(){
  return userPost
}
export default function PostScreen({ navigation }: RootStackScreenProps<'Post'>) {
    const [song, setSong] = useState('song');
    const [image, setImage] = useState(null);

    const pickImage = async () => {
      let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
        allowsEditing: true,
        aspect: [4, 3],
        quality: 1,
      });
      console.log(result);
  
      if (!result.canceled) {
        setImage(result.assets[0].uri);
      }
    };

    let test = new Post('postSong', "postArtist", "postPhoto")
      return (
        <View style={styles.container}>
          <TextInput 
          style={styles.input}
          placeholder='Enter a Song' 
          placeholderTextColor="rgb(41, 41, 95 )"
          onChangeText={(val) => {
            setSong(val)
            postSong = val
          }
          }/>
          <TextInput 
          style={styles.input}
          placeholder='Enter the Artist' 
          placeholderTextColor="rgb(41, 41, 95 )"
          onChangeText={(val) => {
            setSong(val)
            postArtist = val
          }
          }/>
          <TextInput 
          style={styles.input}
          placeholder='Enter a Photo' 
          placeholderTextColor="rgb(41, 41, 95 )"
          onChangeText={(val) => {
            postPhoto = val
          }
          }/>
          <Button title="Pick an image to upload with your song" 
          onPress={pickImage} />
          {image && <><Image source={{ uri: image }} style={{ width: 200, height: 200 }} /></>}
          
          

          <Text style={styles.newText}> The song you will post is: {song}</Text>

          <TouchableOpacity style = {styles.button}

              onPress={() =>{
                
                test = new Post(postSong, postArtist, postPhoto)
                saved = true
                userPost = test.card()
                navigation.replace('Root')
              }} >
                <Text style = {styles.buttonText}>Post Song</Text>
                </TouchableOpacity>

        </View>
      )
    }
;

      
class Post{
  songName:string
  artist:string
  picture:string
  constructor(songName:string, artist: string,  picture: string){
    this.songName = songName
    this.artist = artist
    this.picture = picture
    
  }

  card(){
    console.log("Song Name: ", this.songName)
    console.log("Song Artist: ", this.artist)
    console.log("Song Photo: ", this.picture)
    
    return(
      <View style = {styles.songBase}>
        <View style = {styles.songData}>
          <Text style = {styles.songPoster}> Posted by : {getCurrentUser()}</Text>
          <Text style = {styles.songInfo}>Song : {this.songName}</Text>
          <Text style = {styles.songInfo}>Artist: {this.artist}</Text>
        </View>
        <View style = {styles.songPhoto}>
          <Image source={{uri: this.picture}} style = {styles.songPhoto}/>
        </View>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'rgb(204, 197, 244)',
  },
  input: {
    height: 40,
    width: "80%",
    alignSelf:"center",
    borderColor: 'rgb(41, 41, 95 )',
    borderWidth: 2,
    borderRadius: 10,
    marginVertical: 5,
    textAlign: 'center',
    color:"rgb(41, 41, 95 )",
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
  },
  separator: {
    marginVertical: 30,
    height: 1,
    width: '80%',
  },
  newText: {
    fontSize: 12,
    color:'black',
  },
  songBase:{
    width: '80%',
    height: 200,
    borderColor : "rgb(41, 41, 95 )",
    borderWidth: 10,
    borderRadius: 45,
    flexDirection: 'column',
    backgroundColor: "#111"
  },
  songData:{
    flex: 1,
    borderColor : 'white',
    borderRadius: 45,
    width: "95%",
    alignSelf: 'center',
    backgroundColor:"#111",
  },
  songPhoto:{
    flex:2,
    aspectRatio: 1,
    borderColor : 'rgb(204, 197, 244 )',
    borderWidth: 5,
    alignSelf: 'center',

  },
  songInfo:{
    fontWeight: 'bold',
    fontSize: 15,
    alignSelf: 'auto',
    color: 'rgb(204, 197, 244 )',
  },
  songPoster:{
    fontSize: 10,
    alignSelf: 'flex-end',
    color: 'rgb(204, 197, 244 )',
    marginRight: 20
  },
  button: {
    height: 40,
    width: 200,
    backgroundColor: "rgb(41, 41, 95 )",
    borderColor: "rgb(204, 197, 244 )",
    borderWidth: 2,
    borderRadius: 10,
    justifyContent: "center",
    alignItems: "center",
    margin: 5,
    alignSelf: "center"
  },
  buttonText: {
    color: "rgb(204, 197, 244 )",
    fontWeight: "bold",
    margin: 5,
  },
});
Sunny
  • 708
  • 1
  • 4
  • 21
Joe M
  • 1
  • 1
  • If I understand correctly, you want to keep both the string input and image picker but regardless of what happens, you want to send the uploaded photo url to the next screen? Where are you uploading the photo and what is the error? I'm not sure I completely understand what you're trying to do. – zackrypaul May 04 '23 at 18:23
  • @zackrypaul I just added a picture that shows the output on the other page. card() takes the song name, artist name, and whats supposed to be the picture onto one card and moves it onto a different page. I want, if possible, the box in the middle to be replaced with the picture that is uploaded by the user. The other page simply prompts the user for a song, artist, (text input for picture atm), and then a button that can be clicked to upload a picture. – Joe M May 04 '23 at 18:47

0 Answers0