-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews.js
61 lines (52 loc) · 1.57 KB
/
News.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
import React, { useState } from 'react';
import NewsArticle from 'NewsArticle';
import useFetchNews from 'useFetchNews';
import Searchbar from 'Searchbar';
import Pagination from 'Pagination';
import Loading from 'Loading';
import Error from 'Error';
function News() {
const [search, setSearch] = useState('');
const [page, setPage] = useState(1);
const pageSize = 20;
const { data, loading, error } = useFetchNews(search, page, pageSize);
return (
<div className='container'>
<h1 id='title'>News24 📰</h1>
<Searchbar setSearch={setSearch} search={search} setPage={setPage} />
{!loading && !error && data.totalResults > 0 && (
<Pagination
page={page}
setPage={setPage}
totalResults={data.totalResults}
pageSize={pageSize}
/>
)}
{!loading && !error && data.totalResults === 0 && (
<div id='not__found'>
<h1>Sorry no results found</h1>
<p>
<strong> Try searching with different keywords </strong>
</p>
</div>
)}
{loading && <Loading />}
{error && <Error error={error} />}
<div className='all__news'>
{data.articles &&
data.articles.map((article) => (
<NewsArticle key={article.url} article={article} />
))}
</div>
{!loading && !error && data.totalResults > 0 && (
<Pagination
page={page}
setPage={setPage}
totalResults={data.totalResults}
pageSize={pageSize}
/>
)}
</div>
);
}
export default News;