-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeews.js
139 lines (109 loc) · 4.67 KB
/
Neews.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React, { Component } from 'react'
import NewsItem from './NewsItem';
import Spinner from './spinner';
import PropTypes from 'prop-types'
import InfiniteScroll from "react-infinite-scroll-component";
export class News extends Component {
articles = []
static defaultProps = {
country: 'in',
pageSize: 8,
category: 'general',
}
static propTypes = {
country: PropTypes.string,
pageSize: PropTypes.number,
category: PropTypes.string,
}
capitalizeFirstLetter = (string) =>{
return string.charAt(0).toUpperCase() + string.slice(1);
}
constructor(props) {
super(props)
this.state = {
articles: this.articles,
loading: false,
page: 1,
totalResults: 0
};
document.title = `${this.capitalizeFirstLetter(this.props.category)} - NewsMonkey`;
};
async updateNews()
{
const url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=6483299ac99a411e80cb45c48a61e2a9&page=${this.state.page}&pageSize=${this.props.pageSize}`;
this.setState({loading: true});
let data = await fetch(url);
let parseData = await data.json()
this.setState({articles: parseData.articles, totalResults: parseData.totalResults, loading: false})
}
async componentDidMount(){
this.updateNews()
}
// handlePrevious = async ()=>{
// //console.log("Previous");
// //let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=6483299ac99a411e80cb45c48a61e2a9&page=${this.state.page - 1}&pageSize=${this.props.pageSize}`;
// //this.setState({loading: true});
// //let data = await fetch(url);
// //let parseData = await data.json()
// //console.log(parseData);
// //this.setState({
// //page: this.state.page - 1,
// //articles: parseData.articles,
// // loading: false
// //})
// this.setState({page: this.state.page - 1 });
// this.updateNews();
// }
// handleNext = async ()=> {
// //console.log("Next");
// //if(!(this.state.page+1 > Math.ceil(this.state.totalResults/this.props.pageSize)) ){
// //let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=6483299ac99a411e80cb45c48a61e2a9&page=${this.state.page + 1}&pageSize=${this.props.pageSize}`;
// //this.setState({loading: true});
// //let data = await fetch(url);
// //let parseData = await data.json()
// //this.setState({
// //page: this.state.page + 1,
// //articles: parseData.articles,
// //loading: false
// //})
// this.setState({page: this.state.page + 1 });
// this.updateNews();
// }
fetchMoreData = async() => {
this.setState({page: this.state.page +1});
const url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=6483299ac99a411e80cb45c48a61e2a9&page=${this.state.page}&pageSize=${this.props.pageSize}`;
this.setState({loading: true});
let data = await fetch(url);
let parseData = await data.json();
this.setState({articles: this.state.articles.concat(parseData.articles), totalResults: parseData.totalResults, loading: false})
};
render() {
return (
<>
<h1 className="text-center">NewsMonkey - Top {this.capitalizeFirstLetter(this.props.category)} Headlines </h1>
{/* {this.state.loading && <Spinner/>} */}
<InfiniteScroll
dataLength={this.state.articles.length}
next={this.fetchMoreData}
hasMore={this.state.articles.length !== this.state.totalResults}
loader={<Spinner/>}
>
<div className="container">
<div className="row">
{ this.state.articles.map((element) => {
return <div className="col-md-4" key={element.url}>
<NewsItem title={element.title?element.title.slice(0, 45):""} description={element.description} imageUrl={element.urlToImage} newsUrl={element.url} author={element.author} date={element.publishedAt} source={element.source.name} />
</div>
})}
</div>
</div>
</InfiniteScroll>
{/* <div className="container d-flex justify-content-between">
<button disabled={this.state.page<=1} type="button" className="btn btn-outline-dark" onClick={this.handlePrevious}>← Previous</button>
<button disabled={this.state.page+1 > Math.ceil(this.state.totalResults/this.props.pageSize)} type="button" className="btn btn-outline-dark" onClick={this.handleNext}>Next →</button>
</div> */}
</>
)
}
}
export default News