Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vinodseni opened this issue Oct 2, 2023 · 1 comment
Open

Comments

@vinodseni
Copy link

vinodseni commented Oct 2, 2023

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 vinodseni changed the title const found = getState().usersState?.users.some(el => el.uid === uid) const found = getState().usersState.users.some(el => el.uid === uid) Oct 2, 2023
@vinodseni
Copy link
Author

@vinodseni vinodseni reopened this Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant