You are on page 1of 2

import React, { useState } from "react";

import { TextInput, Button } from "react-native-paper";


import { StyleSheet, View } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage"; // Importa
AsyncStorage
import ImagePicker from "../components/Camera/ImagePicker";
import MapPicker from "../components/Map/MapPicker";
import { Colors } from "../components/util/Colors";

const storeData = async (value) => {


try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem('my-key', jsonValue);
} catch (e) {
console.error("Error al guardar en AsyncStorage:", e);
}
};

const AddPlace = ({ navigation }) => {


const [place, setPlace] = useState("");
const [selectedImage, setSelectedImage] = useState();
const [pickedLocation, setPickedLocation] = useState();

function imageHandler(imageUri) {
setSelectedImage(imageUri);
}

function pickHandler(location) {
setPickedLocation(location);
}

async function savePlaceHandler() {


// Crea un nuevo objeto de lugar
const newPlace = {
place: place,
image: selectedImage,
location: pickedLocation,
};

try {
// Guarda el nuevo lugar en AsyncStorage
await storeData(newPlace);
console.log("Lugar guardado en AsyncStorage:", newPlace);
navigation.setParams({ place: newPlace });

} catch (error) {
console.error("Error al guardar el lugar en AsyncStorage:", error);
}

// Navega a la pantalla "AllPlaces" y pasa el nuevo lugar como parámetro de


navegación
navigation.navigate("AllPlaces", {
place: newPlace,
});
}

return (
<View style={styles.view}>
<TextInput
label="Place"
value={place}
mode="outlined"
onChangeText={(text) => setPlace(text)}
/>
<ImagePicker onImageChange={imageHandler} />
<MapPicker onPickChange={pickHandler} />
<Button
style={styles.btn}
buttonColor={Colors.primary700}
textColor={Colors.white}
mode="elevated"
onPress={savePlaceHandler}
>
Add Place
</Button>
</View>
);
};

const styles = StyleSheet.create({


view: {
marginTop: 16,
marginHorizontal: 16,
alignContent: "center",
},
btn: {
marginTop: 32,
},
});

export default AddPlace;

You might also like