DanieleCalisti

Error Firestore

Nov 7th, 2021
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react';
  2. import { View, Text, Modal, StyleSheet, Pressable, TextInput, ScrollView } from 'react-native';
  3. import CustomizeButton from '../components/CustomizeButton';
  4. import TravelCard from '../components/TravelCard';
  5. import { getFirestore, setDoc, doc , collection} from 'firebase/firestore';
  6. import { initializeApp } from 'firebase/app';
  7. import md5 from "react-native-md5";
  8.  
  9. const firebaseConfig = {
  10.     apiKey: "AIzaSyBFu8QaULKyu3bX9o00qQmWXa69xrg5cWs",
  11.     authDomain: "esame-326913.firebaseapp.com",
  12.     projectId: "esame-326913",
  13.     storageBucket: "esame-326913.appspot.com",
  14.     messagingSenderId: "404096939057",
  15.     appId: "1:404096939057:web:cfe43c26c613818e041110",
  16.     measurementId: "G-DVGFHQ9J3V"
  17. };
  18.  
  19. // Initialize Firebase
  20. const app = initializeApp(firebaseConfig);
  21.  
  22. const db = getFirestore();
  23.  
  24. export default class Travels extends React.Component{
  25.  
  26.     state = {
  27.         isVisible: false,
  28.         titoloViaggio: "",
  29.         viaggi: []
  30.     }
  31.  
  32.     //Prendo tutti i viaggi dell'utente
  33.     componentDidMount(){
  34.         const cityRef = db.collection('utenti').doc('myValue');
  35.         const doc = cityRef.get();
  36.         if (!doc.exists) {
  37.         console.log('No such document!');
  38.         } else {
  39.         console.log('Document data:', doc.data());
  40.         }
  41.     }
  42.  
  43.     insertDocument = (id,titolo,img) => {
  44.         setDoc(doc(doc(db, "utenti", "calisti.daniele"),"viaggi",titolo), {
  45.             titolo,
  46.             img
  47.         });
  48.  
  49.     }
  50.  
  51.     changeVisibility = () => {
  52.         (this.state.isVisible ? this.setState({isVisible:false}) : this.setState({isVisible:true}))
  53.     }
  54.  
  55.     OnChangeTextHandler = (titoloViaggio) => {
  56.         this.setState({titoloViaggio})
  57.     }
  58.  
  59.     onAddViaggio = (viaggio) => {
  60.         if(viaggio.trim() === "")
  61.         {
  62.             alert("Dai un titolo al tuo viaggio!")
  63.             return
  64.         }
  65.  
  66.         const id = md5.hex_md5( Date.now() +"" );
  67.         const img = "https://source.unsplash.com/random";
  68.  
  69.         this.insertDocument(id,viaggio,img)
  70.         this.changeVisibility()
  71.         alert("Viaggio: " + viaggio + " inserito correttamente!");
  72.     }
  73.  
  74.     deleteViaggio = (id) =>
  75.     {
  76.       this.setState({
  77.         viaggi: this.state.viaggi.filter(viaggio => viaggio.id !== id)
  78.       })
  79.     }
  80.  
  81.     render(){
  82.         const cardViaggi = this.state.viaggi.map( (item, index) => {
  83.             return(
  84.                 <TravelCard title={item.viaggio}/>
  85.             )
  86.         })
  87.         return(
  88.             <View style={styles.mainContainer}>
  89.                 <Modal
  90.                     animationType="slide"
  91.                     transparent={true}
  92.                     visible={this.state.isVisible}
  93.                     onRequestClose={() => {
  94.                         this.changeVisibility
  95.                     }}
  96.                 >
  97.                     <View style={styles.centeredView}>
  98.                         <View style={styles.modalView}>
  99.                         <TextInput
  100.                             style={styles.input}
  101.                             onChangeText={this.OnChangeTextHandler}
  102.                             placeholder="Titolo del tuo viaggio"
  103.                         />
  104.                             <Pressable
  105.                                 style={[styles.button, styles.buttonClose]}
  106.                                 onPress={this.onAddViaggio.bind(this, this.state.titoloViaggio)}
  107.                                 >
  108.                                 <Text style={styles.textStyle}>Aggiungi viaggio</Text>
  109.                             </Pressable>
  110.                             <Pressable
  111.                                 style={[styles.button, styles.buttonClose]}
  112.                                 onPress={this.changeVisibility}
  113.                                 >
  114.                                 <Text style={styles.textStyle}>Hide Modal</Text>
  115.                             </Pressable>
  116.                         </View>
  117.                     </View>
  118.                 </Modal>
  119.  
  120.                 <CustomizeButton title={"Aggiungi un nuovo viaggio"} bgColor={"#5DADE2"} onPress={this.changeVisibility}/>
  121.                 <ScrollView>
  122.                     {cardViaggi}
  123.                 </ScrollView>
  124.             </View>
  125.         )
  126.     }
  127. }
  128.  
  129. const styles = StyleSheet.create({
  130.     input: {
  131.         height: 40,
  132.         margin: 12,
  133.         borderWidth: 1,
  134.         padding: 10,
  135.     },
  136.     mainContainer: {
  137.         flex: 1
  138.     },  
  139.     centeredView: {
  140.       flex: 1,
  141.       justifyContent: "center",
  142.       alignItems: "center",
  143.       marginTop: 22
  144.     },
  145.     modalView: {
  146.       margin: 20,
  147.       backgroundColor: "white",
  148.       borderRadius: 20,
  149.       padding: 35,
  150.       alignItems: "center",
  151.       shadowColor: "#000",
  152.       shadowOffset: {
  153.         width: 0,
  154.         height: 2
  155.       },
  156.       shadowOpacity: 0.25,
  157.       shadowRadius: 4,
  158.       elevation: 5
  159.     },
  160.     button: {
  161.       borderRadius: 20,
  162.       padding: 10,
  163.       elevation: 2
  164.     },
  165.     buttonOpen: {
  166.       backgroundColor: "#F194FF",
  167.     },
  168.     buttonClose: {
  169.       backgroundColor: "#2196F3",
  170.     },
  171.     textStyle: {
  172.       color: "white",
  173.       fontWeight: "bold",
  174.       textAlign: "center"
  175.     },
  176.     modalText: {
  177.       marginBottom: 15,
  178.       textAlign: "center"
  179.     }
  180.   });
  181.  
Advertisement
Add Comment
Please, Sign In to add comment