Skip to content

implemented cric news feature #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/javascript/packs/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import UserDetails from './UserDetails';
import Game from './Game';
import Diamond from './Diamond';
import DiamondSearch from './DiamondSearch';
import CricNews from './CricNews';
import CricNewsSearch from './CricNewsSearch';
import NotFound from './NotFound';

class App extends React.Component {
Expand All @@ -16,14 +18,15 @@ class App extends React.Component {
<Router>
<div>
<Header />

<Switch>
<Route exact path="/" component={Root} />
<Route exact path="/users" component={User} />
<Route exact path="/users/:id/:name" component={UserDetails} />
<Route exact path="/games" component={Game} />
<Route exact path="/diamonds" component={Diamond} />
<Route exact path="/diamonds/:shape/:color/:clarity/:lab/:polish" component={DiamondSearch} />
<Route exact path="/cric" component={CricNews} />
<Route exact path="/news_search/:tag" component={CricNewsSearch} />
<Route component={NotFound} />
</Switch>
</div>
Expand Down
56 changes: 56 additions & 0 deletions app/javascript/packs/components/CricNews.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';

class CricNews extends React.Component {
constructor(props) {
super(props);
this.state = { articles: [] };
}
componentDidMount() {
const api_key = 'cf3ecb1578544f908ae84946643000f4';
fetch(`https://newsapi.org/v2/top-headlines?sources=espn-cric-info&apiKey=${api_key}`, { method: 'GET' })
.then((Response) => Response.json())
.then((response) => {
this.setState({
articles: response.articles,
});
});
}
click_tag (event, search_tag){
window.location='/news_search/' + search_tag;
}
render() {
const rows = this.state.articles.map((article, i) => (
<tr key={i}>
<td>{i+1}</td>
<td><a href={article.url}>{article.title}</a></td>
<td>{article.description}</td>
</tr>
));
return (
<div>
<div className="col-sm-offset-10">
<h5>Favourite Tags</h5>
</div>
<button className="pull-right" onClick={(e) => this.click_tag(e, 'World Cup')}>World Cup</button>
<button className="pull-right" onClick={(e) => this.click_tag(e, 'Bangladesh')}>Bangladesh</button>
<button className="pull-right" onClick={(e) => this.click_tag(e, 'Premier League')}>Premier League</button>
<button className="pull-right" onClick={(e) => this.click_tag(e, 'IPL')}>IPL</button>
<h3>Top Headlines</h3>
<table className="table">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
</div>
);
}
}

export default CricNews;
64 changes: 64 additions & 0 deletions app/javascript/packs/components/CricNewsSearch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import { Link } from 'react-router-dom';
class CricNewsSearch extends React.Component {
constructor(props) {
super(props);
this.state = { articles: [] };
}
componentDidMount() {
const api_key = 'cf3ecb1578544f908ae84946643000f4';
fetch(`https://newsapi.org/v2/everything?sources=espn-cric-info&apiKey=${api_key}`, { method: 'GET' })
.then((Response) => Response.json())
.then((response) => {
this.setState({
articles: response.articles,
});
});
}
click_tag (event, search_tag){
window.location='/news_search/' + search_tag;
}
render() {
const search_word = `${this.props.match.params.tag}`;
// var btnStyle = {
// backgroundColor: 'skyblue'
// };
var count = 0;
const rows = this.state.articles.map((article, i) => {
if(article.title.includes(search_word) || article.description.includes(search_word)){
count++;
return (<tr key={i}>
<td>{count}</td>
<td><a href={article.url}>{article.title}</a></td>
<td>{article.description}</td>
</tr>)
}
});
return (
<div>
<div className="col-sm-offset-10">
<h5>Favourite Tags</h5>
</div>
<button className="pull-right" style={search_word ==='World Cup' ? {backgroundColor: 'skyblue'} : {} } onClick={(e) => this.click_tag(e, 'World Cup')}>World Cup</button>
<button className="pull-right" style={search_word ==='Bangladesh' ? {backgroundColor: 'skyblue'} : {} } onClick={(e) => this.click_tag(e, 'Bangladesh')}>Bangladesh</button>
<button className="pull-right" style={search_word ==='Premier League' ? {backgroundColor: 'skyblue'} : {} } onClick={(e) => this.click_tag(e, 'Premier League')}>Premier League</button>
<button className="pull-right" style={search_word ==='IPL' ? { backgroundColor: 'skyblue' } : {} } onClick={(e) => this.click_tag(e, 'IPL')}>IPL</button>
<h3>{this.props.match.params.tag} News</h3>
<table className="table">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
</div>
);
}
}

export default CricNewsSearch;
1 change: 1 addition & 0 deletions app/javascript/packs/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Header = () => (
<li><Link to="/users" href>Users</Link></li>
<li><Link to="/diamonds" href>Diamonds</Link></li>
<li><Link to="/games" href>Game</Link></li>
<li><Link to="/cric" href>Cric News</Link></li>
</ul>
</div>
</div>
Expand Down