File size: 1,515 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import React, { useEffect, useState } from "react";
import Post from "./Post";
import { onSnapshot, collection, query, orderBy } from "firebase/firestore";
import { firestore } from "../firebase/firebase";
import PostSkeleton from "./Skeleton/Skeleton";
type PostsProps = {};
const Posts: React.FC<PostsProps> = () => {
const [posts, setPosts] = useState<any[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const fetchPost = () => {
try {
setLoading(false);
const fetchQuery = onSnapshot(
query(collection(firestore, "posts"), orderBy("timestamp", "desc")),
(snapshot) => {
setPosts(snapshot.docs);
}
);
fetchQuery;
setTimeout(() => {
setLoading(true);
}, 1000);
} catch (error: any) {
console.log(error.message);
}
};
useEffect(() => {
fetchPost();
}, [firestore]);
return (
<div>
{loading ? (
<>
{posts.map((post) => (
<Post
key={post.id}
id={post.id}
userId={post.data().userId}
username={post.data().username}
userImage={post.data().profileImage}
img={post.data().image}
caption={post.data().caption}
/>
))}
</>
) : (
<>
{[...Array(5)].map((_, i) => (
<PostSkeleton key={i} loading={true} />
))}
</>
)}
</div>
);
};
export default Posts;
|