SimCoderYoutube / InstagramClone

Instagram Clone React Native Tutorial 2021 👨‍💻 I'll show you how you can do this in the simplest way and terms possible. By the end of this series you'll have learned how the big companies do it and will be able to do the same, you not only will be able to do this app, but you'll be able to put what you learn into your very own projects! In this series, we use React Native with Expo to quickly deploy the project. We use firebase for all our microservice needs like the auth system, database, storage, amongst others. firebase, redux, react native, javascript, expo. In this series, we'll use all of them and you'll learn them by doing an iconic app. Welcome to this Simcoder project and make an Instagram Clone!
Apache License 2.0
745 stars 294 forks source link

const found = getState().usersState.users.some(el => el.uid === uid) #54

Open vinodseni opened 10 months ago

vinodseni commented 10 months ago

import { doc, getDoc, onSnapshot,collection ,getDocs, orderBy} from "@firebase/firestore" import { USERS_DATA_POSTS_STATE_CHANGE, USERS_DATA_STATE_CHANGE, USER_FOLLOWING_STATE_CHANGE, USER_POSTS_STATE_CHANGE, USER_STATE_CHANGE } from "../constants" import { db,auth } from "../../config/firebase"

export function fetchUser(){

return((dispatch)=>{

    getDoc(doc(db, "users",auth.currentUser.uid))

   .then((snapshot)=>{

    if(snapshot.exists){

dispatch({type:USER_STATE_CHANGE,currentUser:snapshot.data()}) } else{ console.log('donst not exit data!!!') } }) })

}

export function fetchUserPosts(){

return((dispatch)=>{

    getDocs(collection(db, "Posts", auth.currentUser.uid, "userPost"))
   .then((snapshot)=>{
   const posts = snapshot.docs.map(doc=>{
    const data = doc.data();
    const id = doc.id;
    return {id ,...data}
   })
//    console.log('posts',posts) 

dispatch({type:USER_POSTS_STATE_CHANGE,posts})

   })
})

}

export function fetchUserFollwowing(){

return((dispatch)=>{

    onSnapshot(collection(db, "following", auth.currentUser.uid, "userFollowing"),

   (snapshot)=>{

   const following = snapshot.docs.map(doc=>{
       const id = doc.id;
       return id 
    })
    console.log(following)

dispatch({type:USER_FOLLOWING_STATE_CHANGE,following}); for (let i = 0; i < following.length; i++) {

dispatch(fetchUsersData(following[i]));

}

   })
})

}

export function fetchUsersData (uid){

return ((dispatch,getState)=>{ console.log('uid',uid) const found = getState().usersState?.users.some(el => el.uid === uid);

if(!found){

getDoc(doc(db, "users",uid))

.then((snapshot)=>{

    if(snapshot.exists){

    let  user = snapshot.data();
    user.uid = snapshot.id;

dispatch({type:USERS_DATA_STATE_CHANGE,user}); dispatch(fetchUsersFollowingPosts(user.id)); } else{ console.log('donst not exit data!!!') } }) }

})

}

export function fetchUsersFollowingPosts(uid){

return((dispatch,getState)=>{

    getDocs(collection(db, "Posts",uid, "userPost"))
    // .orderBy("timestamp","asc")
   .then((snapshot)=>{

    const  uid = snapshot.query.EP.path.segments[1];

console.log('segments',uid) const user = getState().usersState.users.find(el => el.uid === uid);

   const posts = snapshot.docs.map(doc=>{
    const data = doc.data();
    const id = doc.id;
    return {id ,...data,user}
   })
   console.log('posts',posts) 

dispatch({type:USERS_DATA_POSTS_STATE_CHANGE,posts,uid}) console.log('posts',getState())

   })

Screenshot (17)

})

}

vinodseni commented 10 months ago