From 5885e1185c4b274a65ae1f8439dfb54fe2c590f1 Mon Sep 17 00:00:00 2001 From: Prayag Bhatia Date: Sun, 16 Oct 2022 13:02:53 +0530 Subject: [PATCH] ADD : new news components categories --- src/App.js | 2 +- src/components/NavBar.js | 11 ++++++++--- src/components/News.js | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/App.js b/src/App.js index 3394764..ca924a0 100644 --- a/src/App.js +++ b/src/App.js @@ -9,7 +9,7 @@ export default class App extends Component { return (
- +
) } diff --git a/src/components/NavBar.js b/src/components/NavBar.js index f847ac6..107a310 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -16,9 +16,14 @@ export class NavBar extends Component {
  • Home
  • -
  • - About -
  • +
  • About
  • +
  • Business
  • +
  • Entertainment
  • +
  • General
  • +
  • Health
  • +
  • Science
  • +
  • Sports
  • +
  • Technology
  • diff --git a/src/components/News.js b/src/components/News.js index 2e23e87..90f22da 100644 --- a/src/components/News.js +++ b/src/components/News.js @@ -1,9 +1,24 @@ 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 : 'science' + } + + static propTypes = { + country : PropTypes.string, + pageSize : PropTypes.number, + category : PropTypes.string + + } + + constructor() { super(); this.state = { @@ -33,10 +48,12 @@ export class News extends Component { async fetchNewsData(page){ let todayDate = this.getCurrentDate() - let url = `https://newsapi.org/v2/everything?q=india&from=${todayDate}&sortBy=publishedAt&apiKey=7d248d9c8ea64fa6a4691432a30a74ac&pageSize=${this.props.pageSize}&page=` + page + let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&from=${todayDate}&sortBy=publishedAt&apiKey=7d248d9c8ea64fa6a4691432a30a74ac&pageSize=${this.props.pageSize}&page=` + page + console.log(url) this.setState({loading: true}) let data = await fetch(url) ; let parsedData = await data.json() + console.log(parsedData) this.setState({loading: false}) this.setState({articles: parsedData.articles, total_results: parsedData.totalResults}) } @@ -59,11 +76,11 @@ export class News extends Component { return (
    {this.state.loading && } -

    NewsMonkey -- Top Headlines

    +

    NewsMonkey -- Top Headlines

    {!this.state.loading && this.state.articles.map((element) => { return
    -
    })}