Skip to content

Commit

Permalink
ADD : new news components categories
Browse files Browse the repository at this point in the history
  • Loading branch information
prayag1740 committed Oct 16, 2022
1 parent 1f69ac3 commit 5885e11
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class App extends Component {
return (
<div>
<NavBar />
<News pageSize={5} />
<News pageSize={8} country={'in'} />
</div>
)
}
Expand Down
11 changes: 8 additions & 3 deletions src/components/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ export class NavBar extends Component {
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="/home">Home</a>
</li>
<li className="nav-item">
<a className="nav-link" href="/about">About</a>
</li>
<li className="nav-item"><a className="nav-link" href="/about">About</a></li>
<li className="nav-item"><a className="nav-link" href="/business">Business</a></li>
<li className="nav-item"><a className="nav-link" href="/entertainment">Entertainment</a></li>
<li className="nav-item"><a className="nav-link" href="/general">General</a></li>
<li className="nav-item"><a className="nav-link" href="/health">Health</a></li>
<li className="nav-item"><a className="nav-link" href="/science">Science</a></li>
<li className="nav-item"><a className="nav-link" href="/sports">Sports</a></li>
<li className="nav-item"><a className="nav-link" href="/technology">Technology</a></li>
</ul>
</div>
</div>
Expand Down
23 changes: 20 additions & 3 deletions src/components/News.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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})
}
Expand All @@ -59,11 +76,11 @@ export class News extends Component {
return (
<div className='container my-3'>
{this.state.loading && <Spinner/>}
<h1 className='text-center'> NewsMonkey -- Top Headlines</h1>
<h1 className='text-center' style={{margin: '35px 0px'}}> NewsMonkey -- Top Headlines</h1>
<div className='row'>
{!this.state.loading && this.state.articles.map((element) => {
return <div className='col md-4' key={element.url}>
<NewsItem title={element.title.slice(0,45)} description={element.description.slice(0,88)}
<NewsItem title={element.title.slice(0,45)} description={element.description}
imageUrl={element.urlToImage} newsUrl={element.url}/>
</div>
})}
Expand Down

0 comments on commit 5885e11

Please sign in to comment.