import React, { Component } from "react";
import Nitems from "./Nitems";
import Loading from "./Loading";
import './Font.css'
import PropTypes from 'prop-types'
export class News extends Component {
static defaultProps={
country: "in",
category: "general"
}
static propTypes={
country: PropTypes.string,
category: PropTypes.string
}
constructor() {
super();
console.log("Hii this is a constructor");
this.state = {
articles: [],
loading: false,
page: 1,
};
}
async componentDidMount() {
this.props.setProgress(10)
let url =
`https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&pagesize=12`;
this.setState({loading: true})
let data = await fetch(url);
this.props.setProgress(30)
let parsedata = await data.json();
this.props.setProgress(70)
this.setState({
articles: parsedata.articles,
totalarticles: parsedata.totalResults,
loading: false
});
this.props.setProgress(100)
}
previouspage = async () => {
this.props.setProgress(10)
let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}$apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
this.state.page - 1
}&pagesize=12`;
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.props.setProgress(100)
};
nextpage = async () => {
this.props.setProgress(10)
if (this.state.page 1 > Math.ceil(this.state.totalarticles / 12)) {
}
else {
let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
this.state.page 1
}&pagesize=12`;
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.props.setProgress(100)
}
};
render() {
return (
<div className="container ">
<h3 className="my-3" className="text-center" style={{margin: "18px 0px", fontFamily:" Permanent Marker", fontSize: "45px"}}>Top Headlines