This is the firestore data structure where I was able to store the ids of users who liked it but not able to count the total number of likes.
I am trying to count and display the total number of likes for a particular post, whereas I was able to store the interactions on the post but I want the total like count as of Insta or FB posts.
const onLikePress = (userId, postId) => {
firebase.firestore()
.collection("posts")
.doc(userId)
.collection("userPosts")
.doc(postId)
.collection("likes")
.doc(firebase.auth().currentUser.uid)
.set({})
}
const onDislikePress = (userId, postId) => {
firebase.firestore()
.collection("posts")
.doc(userId)
.collection("userPosts")
.doc(postId)
.collection("likes")
.doc(firebase.auth().currentUser.uid)
.delete()
}
<View style={{flexDirection:"row" , justifyContent:"space-around" , marginTop:4}}>
{ item.currentUserLike ?
(
<TouchableNativeFeedback
onPress={() => onDislikePress(item.user.uid, item.id)}
>
<MaterialCommunityIcons name="heart" size={24} color="blue" />
</TouchableNativeFeedback>
)
:
(
<TouchableNativeFeedback
onPress={() => onLikePress(item.user.uid, item.id)}
>
<MaterialCommunityIcons name="heart-outline" size={24} color="black" />
</TouchableNativeFeedback>
)
}
<TouchableOpacity onPress={() => props.navigation.navigate('Comment', { postId: item.id, uid: item.user.uid })}>
<MaterialCommunityIcons name="comment-text-outline" size={24} color="black" />
</TouchableOpacity>
<TouchableOpacity onPress={ () => onShare (item.downloadURL)}>
<MaterialCommunityIcons name="share-outline" size={24} color="black" />
</TouchableOpacity>
</View>
<Text>{item.likesCount}</Text>
</View>
