-
NewsMonkey
+
NewsMonkey
diff --git a/src/components/News.js b/src/components/News.js
index 6255d9c..7e40a3e 100644
--- a/src/components/News.js
+++ b/src/components/News.js
@@ -1,22 +1,46 @@
import React, { Component } from 'react'
import NewsItem from './NewsItem'
+const pageSize = 10
export class News extends Component {
constructor() {
super();
this.state = {
articles: [],
- loading: false
+ loading: false,
+ page: 1,
+ total_results: 0
}
}
//Lifecycle method run after render method is called ; used for loading of data
- async componentDidMount() {
- let url = "https://newsapi.org/v2/everything?q=india&from=2022-09-14&sortBy=publishedAt&apiKey=7d248d9c8ea64fa6a4691432a30a74ac"
+ componentDidMount() {
+ this.fetchNewsData(1) //intially fetch data for 1st page
+ }
+
+ handleNextClick = () => {
+ this.setState({page: this.state.page + 1})
+ console.log(this.state.page)
+ this.fetchNewsData(this.state.page+1)
+ }
+
+ handlePrevClick = () => {
+ this.setState({page: this.state.page - 1})
+ console.log(this.state.page)
+ this.fetchNewsData(this.state.page-1)
+ }
+
+ async fetchNewsData(page){
+ let url = `https://newsapi.org/v2/everything?q=india&from=2022-09-14&sortBy=publishedAt&apiKey=7d248d9c8ea64fa6a4691432a30a74ac&pageSize=${pageSize}&page=` + page
let data = await fetch(url) ;
+ console.log(data)
let parsedData = await data.json()
- this.setState({articles: parsedData.articles})
+ this.setState({articles: parsedData.articles, total_results: parsedData.totalResults})
+ }
+
+ showNextButton() {
+ return this.state.page + 1 > Math.ceil(this.state.total_results/pageSize)
}
render() {
@@ -31,6 +55,10 @@ export class News extends Component {
})}