-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOldNews.js
116 lines (93 loc) · 3.64 KB
/
OldNews.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
import React, { Component } from 'react'
import NewsItem from './NewsItem'
import Spinner from './Spinner';
import PropTypes from 'prop-types'
export class News extends Component {
static defaultProps = {
country:"in",
pageSize: 8,
category:"general"
}
static propTypes = {
// name:PropTypes.bool.isRequired,
country:PropTypes.string,
pageSize: PropTypes.number,
category:PropTypes.string
}
capatalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
constructor (props) {
super(props);
this.state = {
articles : [],
loading : false,
page:1
}
document.title = `${this.capatalizeFirstLetter(this.props.category)} - Mine News`;
}
async updateNews() {
let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=d11e174e88154920ae94359e22d4a467&page=${this.state.page}&&pageSize=${this.props.pageSize}`;
this.setState({loading:true})
let data = await fetch(url);
let parsedData = await data.json()
console.log(parsedData);
this.setState({
articles:parsedData.articles,
totalResults:parsedData.totalResults,
loading:false
})
}
async componentDidMount(){
// console.log("cdm");
// let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=d11e174e88154920ae94359e22d4a467&page=${this.state.page}&&pageSize=${this.props.pageSize}`;
// this.setState({loading:true})
// let data = await fetch(url);
// let parsedData = await data.json()
// console.log(parsedData);
// this.setState({
// articles:parsedData.articles,
// totalResults:parsedData.totalResults,
// loading:false
// })
this.updateNews()
}
handlePrevClick = async () => {
console.log("Previous");
this.setState({page:this.state.page - 1})
this.updateNews()
}
handleNextClick = async () => {
console.log("Next");
this.setState({page:this.state.page + 1})
this.updateNews()
}
render() {
console.log("render");
return (
<div className="container my-3">
<h1 className="text-center " style={{margin:'35px 0px;'}} >MyNews - Top {this.capatalizeFirstLetter(this.props.category)} Headlines </h1>
{/* if loading is true then we have to show loading component */}
{this.state.loading && <Spinner/>}
<div className="row" >
{/* if loading is not true then we have to show contant */}
{!this.state.loading && this.state.articles.map((element) => {
return(
<div className="col-md-4" key={element.url}>
<NewsItem title={element.title} description={element.description} imageUrl = {element.urlToImage} newsUrl= {element.url} author={element.author} source={element.source.name} date={(new Date(element.publishedAt)).toGMTString()} />
</div>)
})}
</div>
<div className="container d-flex justify-content-between">
<button disabled={this.state.page<=1} type="button" className="btn btn-dark" onClick={this.handlePrevClick}> ← Previous</button>
<button disabled = { this.state.page +1 > Math.ceil(this.state.totalResults/this.props.pageSize)} type="button" className="btn btn-dark" onClick={this.handleNextClick}>Next → </button>
</div>
{/* <div className="container">
<button type="button" class="btn btn-dark">Previous</button>
<button type="button" class="btn btn-dark">Next</button>
</div> */}
</div>
)
}
}
export default News