-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostListing.js
77 lines (66 loc) · 2.58 KB
/
PostListing.js
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
67
68
69
70
71
72
73
74
75
76
77
import { useSelector } from "react-redux";
import { useSearchParams } from "react-router-dom";
import { selectAllPosts } from "../postContent/postContentSlice";
import PostListingItem from "../postContent/PostListingItem";
import { selectListingsLoadedStatus } from "./postListingsSlice";
import { selectListing } from "./postListingsSlice";
import utilStyles from "../../App/utils.module.css";
import styles from "./PostListing.module.css";
export default function PostListings({ name, search, gridArea }) {
const listingsLoaded = useSelector(selectListingsLoadedStatus);
const listing = useSelector(state => selectListing(state, name));
const mainListing = useSelector(state => selectListing(state, "All"));
const postsContent = useSelector(selectAllPosts);
const [queryParams] = useSearchParams();
function getCategoryPostIds() {
const allPostIds = mainListing.postIds;
const listingSubs = listing.includedSubs;
const categoryPostIds = allPostIds.filter(postId => {
// Post's subreddit is within listing's included subreddits
const postSub = postsContent[postId].subreddit;
return listingSubs.includes(postSub);
});
return categoryPostIds;
}
function getSearchPostIds() {
const allPostIds = mainListing.postIds;
const query = queryParams.get("q");
if (!query) {
return allPostIds;
}
const lowerCaseQuery = queryParams.get("q").toLowerCase();
const searchPostIds = allPostIds.filter(postId => {
// Post's title or subreddit name includes the search term (case-insensitive)
const lowerCaseTitle = postsContent[postId].title.toLowerCase();
const lowerCaseSub = postsContent[postId].subreddit.toLowerCase();
return (lowerCaseTitle.includes(lowerCaseQuery) ||
lowerCaseSub.includes(lowerCaseQuery));
});
return searchPostIds;
}
function generatePosts() {
let postIds;
if (name === "All") {
postIds = mainListing.postIds;
} else if (search) {
postIds = getSearchPostIds();
} else {
postIds = getCategoryPostIds();
}
if (postIds.length === 0) {
return <p>Sorry, no posts found.</p>
}
const listingIds = postIds.slice(0, 24);
const posts = listingIds.map(id => <PostListingItem key={id} id={id} />);
return <div>{posts}</div>;
}
return (
<div style={{gridArea}} className={styles.postListing}>
<div className={styles.listingHeader}>
<h2 className={styles.h2}>{name}</h2>
<div className={utilStyles.hLine}></div>
</div>
{listingsLoaded ? generatePosts() : <p>Loading posts...</p>}
</div>
);
}