From bf0104c970804ada8a2fcc422cb2dc88340d05b9 Mon Sep 17 00:00:00 2001 From: Prayag Bhatia Date: Thu, 20 Oct 2022 16:44:13 +0530 Subject: [PATCH] ADD : search functionality --- src/App.js | 9 +++++++-- src/components/NavBar.js | 11 +++++++++++ src/components/News.js | 18 +++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/App.js b/src/App.js index b33bde0..1b1a378 100644 --- a/src/App.js +++ b/src/App.js @@ -18,17 +18,21 @@ export default class App extends Component { apiKey = process.env.REACT_APP_NEWS_API_KEY - state = {progress: 0} + state = {progress: 0, searchText: ''} setProgress = (progress) => { this.setState({progress: progress}) } + setSearchText = (searchText) => { + this.setState({searchText: searchText}) + } + render() { return (
- + }> }> }> + }>
diff --git a/src/components/NavBar.js b/src/components/NavBar.js index 99a3cf4..e65c2d8 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -3,6 +3,11 @@ import {Link} from 'react-router-dom' export class NavBar extends Component { + constructor(props) { + super(props) ; + this.textInput = React.createRef() ; + } + render() { return (
@@ -24,6 +29,12 @@ export class NavBar extends Component {
  • Sports
  • Technology
  • +
    + + + + +
    diff --git a/src/components/News.js b/src/components/News.js index f0a1c86..7732acd 100644 --- a/src/components/News.js +++ b/src/components/News.js @@ -50,12 +50,20 @@ export class News extends Component { this.fetchNewsData(this.state.page - 1); }; - async fetchNewsData(page) { - this.props.setProgress(10) ; + getNewsAPIUrl(page) { let todayDate = this.getCurrentDate(); - const url = - `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&from=${todayDate}&sortBy=publishedAt&apiKey=${this.props.apiKey}&pageSize=${this.props.pageSize}&page=` + - page; + console.log(this.props.searchText) + let url = + `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&from=${todayDate}&sortBy=publishedAt&apiKey=${this.props.apiKey}&pageSize=${this.props.pageSize}&page=` + page; + if (this.props.searchText) { + url = url + `&q=${this.props.searchText}` + } + return url; + } + + async fetchNewsData() { + this.props.setProgress(10) ; + let url = this.getNewsAPIUrl(this.state.page); console.log(url); this.setState({ loading: true }); let data = await fetch(url);