-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPastPosts.jsx
59 lines (54 loc) · 1.78 KB
/
PastPosts.jsx
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
import styles from '../css/pastPosts.module.css';
import { collection, query, where, getDocs } from 'firebase/firestore';
import { db } from '../firebase/config';
import { useEffect, useState } from 'react';
import ProfilePagePostCard from './ProfilePagePostCard';
import { useRouter } from 'next/router';
export default function PastPosts({ userName, userNameFromParams }) {
const [pastPosts, setPastPosts] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const router = useRouter();
useEffect(() => {
setIsLoading(true);
if (!router.isReady || !userNameFromParams) return;
const getUserPosts = async () => {
const postsCol = collection(db, 'posts');
const postQuery = query(
postsCol,
where('user', '==', userNameFromParams)
);
const postsSnapshot = await getDocs(postQuery);
const postList = postsSnapshot.docs.map((doc) => doc.data());
postList.sort((a, b) => {
return b.postTime - a.postTime;
});
return postList;
};
getUserPosts()
.then((response) => {
setPastPosts(response);
setIsLoading(false);
})
.catch((error) => {});
}, [router.isReady, userNameFromParams, router]);
if (isLoading) return <div>Loading...</div>;
return (
<div style={{ display: 'flex', justifyContent: 'center' }}>
<div className={styles.pastPostContainer}>
<div className={styles.subHeader}>Past Posts</div>
{pastPosts.length === 0 ? (
<p>You have not created any posts yet.</p>
) : null}
{pastPosts?.map((post) => {
return (
<ProfilePagePostCard
key={post.postId}
props={post}
userName={userName}
/>
);
})}
</div>
</div>
);
}