-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticleList.jsx
66 lines (62 loc) · 1.8 KB
/
ArticleList.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
60
61
62
63
64
65
66
import { useState, useEffect } from "react";
import { getArticles } from "./apiFunctions";
import ArticleItem from "./ArticleItem";
import LoadingSpinner from "./LoadingSpinner";
import { useSearchParams } from "react-router-dom";
import { capitaliseFirstLetter } from "./utils";
const ArticleList = ({ setNumItems, pageNumber }) => {
const [isLoading, setIsLoading] = useState(true);
const [articles, setArticles] = useState([]);
const [noArticlesFound, setNoArticlesFound] = useState(null);
const [searchParams, setSearchParams] = useSearchParams();
useEffect(() => {
setIsLoading(true);
setNumItems(null);
const sortBy = searchParams.get("sort_by");
const order = searchParams.get("order");
const authorFilter = searchParams.get("author");
const topicFilter = searchParams.get("topic");
getArticles(pageNumber, topicFilter, authorFilter, sortBy, order)
.then((articles) => {
setNumItems(articles.total_count);
setArticles(articles.articles);
setNoArticlesFound(null);
setIsLoading(false);
})
.catch((error) => {
setNumItems(0);
setNoArticlesFound(error.response.data.msg);
setArticles([]);
setIsLoading(false);
});
}, [pageNumber, searchParams]);
return (
<div id="articleListBox">
{isLoading ? (
<div>
<p id="articleLoadingText">Loading Articles</p>
<LoadingSpinner />
</div>
) : (
articles.map((article, index) => (
<ArticleItem
key={article.article_id}
article={article}
className={
index % 2
? "articleListDarkBackground"
: "articleListLightBackground"
}
/>
))
)}
{!isLoading && noArticlesFound && (
<p id="articleLoadingText">
{capitaliseFirstLetter(noArticlesFound)}
</p>
)}
<div id="bottomSpace"></div>
</div>
);
};
export default ArticleList;