it doesnt add posts to the profile import { Text, View, Image, Pressable, ScrollView }…
- User
it doesnt add posts to the profile
import { Text, View, Image, Pressable, ScrollView } from ‘react-native’; import { gStyle } from ‘…/styles/style’; import React, {useState, useEffect} from ‘react’; import Header from ‘…/components/Header’; import Footer from ‘…/components/Footer’; import { Ionicons, MaterialCommunityIcons } from ‘@expo/vector-icons’; import { useNavigation } from ‘@react-navigation/native’; import moment from ‘moment’; import {firebase} from ‘…/Firebase/firebase’; import ‘firebase/compat/auth’; import ‘firebase/compat/database’; import ‘firebase/compat/firestore’;
export default function SignUpForMK({route}) { const [userAuthenticated, setUserAuthenticated] = useState(false); useEffect(() => { const unsubscribe = firebase.auth().onAuthStateChanged(user => { if (user) { setUserAuthenticated(true); } else { setUserAuthenticated(false); } });
return unsubscribe; }, []); const {mkData}=route.params; const navigation = useNavigation(); const bookMK=()=>{ const currentUserID=firebase.auth().currentUser.uid; const bookedMKData={ name:mkData.name, time:mkData.time.toDate().toISOString(), image:mkData.image, price:mkData.price }; firebase .database() .ref(users/${currentUserID}/bookedMK) .push(bookedMKData) .then(()=>{ navigation.navigate(‘Profile’,{bookedMKData}); }) } return (
import { Text, View, Image, ScrollView, Pressable } from ‘react-native’; import Header from ‘…/components/Header’; import Footer from ‘…/components/Footer’; import { gStyle } from ‘…/styles/style’; import { firebase } from ‘…/Firebase/firebase’; import ‘firebase/compat/auth’; import ‘firebase/compat/database’; import ‘firebase/compat/firestore’; import React, {useState, useEffect} from ‘react’; import { useNavigation } from ‘@react-navigation/native’;
export default function Profile() { const navigation = useNavigation(); const options = { day: ‘numeric’, month: ‘long’, hour: ‘numeric’, minute: ‘numeric’ }; const [userId, setUserId]=useState(null); const [userData, setUserData] = useState(null); const [bookedMK, setBookedMK] = useState([]); useEffect(()=>{ const unsubscribe=firebase.auth().onAuthStateChanged((user)=>{ if(user){ setUserId(user.uid); }else{ setUserId(null); } }); return()=>unsubscribe(); },[]) useEffect(() => { if(userId){ const userRef=firebase.database().ref(users/{userId}/bookedMK); bookedMKRef.on(‘value’, snapshot => { const data = snapshot.val(); if (data) { const mk = Object.values(data); setBookedMK([]); setBookedMK(mk) } else { setBookedMK([]); } });
- Assistant
It seems that the issue is with the commented out code in the first block of code (SignUpForMK). The onPress function for the Pressable component seems to have been commented out. This means that when the user presses the "Забронировать" button, nothing happens since there is no function being called.
To fix this, you can uncomment the following code:
<Pressable style={gStyle.SupMoreDetails} onPress={bookMK} > <Text style={gStyle.SupBtn}>Забронировать</Text> </Pressable>This will call the
bookMKfunction when the user presses the button and add the post to the user's profile. - User
okay it works now, but how can i do that after bookMK it redirects to profile
- Assistant
To redirect the user to the Profile page after booking the MK, you can update the
bookMK()function in theSignUpForMKcomponent to navigate to the Profile page after successfully booking the MK. You can do this by calling thenavigation.navigate()function inside the.then()method of thefirebase.database().ref().push()promise.Here's the updated
bookMK()function:const bookMK = () => { const currentUserID = firebase.auth().currentUser.uid; const bookedMKData = { name: mkData.name, time: mkData.time.toDate().toISOString(), image: mkData.image, price: mkData.price, }; firebase .database() .ref(`users/${currentUserID}/bookedMK`) .push(bookedMKData) .then(() => { navigation.navigate('Profile'); }); };Here,
navigation.navigate('Profile')will navigate the user to the Profile page after booking the MK. - User
how to delete posts?
- Assistant
To delete a post, you will need to remove the corresponding data from the database.
Assuming you have a list of posts that you want to delete, you can create a function to delete the post from the database by using the Firebase Realtime Database
remove()method on the reference to the post data.Here's an example function that takes in a
postIDand deletes the post from the database:const deletePost = (postID) => { const currentUserID = firebase.auth().currentUser.uid; firebase .database() .ref(`users/${currentUserID}/posts/${postID}`) .remove() .then(() => { console.log('Post deleted successfully!'); }) .catch((error) => { console.error('Error deleting post:', error); }); };Here,
currentUserIDis the ID of the current authenticated user,postIDis the ID of the post you want to delete, andusers/${currentUserID}/posts/${postID}is the reference to the desired post data in the database. Callingremove()on this reference will delete the post data.You can call this function when the user performs a delete action on a post, such as pressing a delete button. You can pass in the as a parameter to this function to delete the correct post.
- User
- Assistant
To enable the user to delete a booked MK, you can add a delete button for each booked MK in the component.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.