Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 336e5ac

Browse files
written saga-async action to fetch news once button is clicked
1 parent 7d3fddf commit 336e5ac

File tree

9 files changed

+79
-12
lines changed

9 files changed

+79
-12
lines changed

‎package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"axios": "^0.18.0",
67
"react": "^16.4.2",
78
"react-dom": "^16.4.2",
89
"react-redux": "^5.0.7",

‎src/App.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React, { Component } from 'react';
2-
import './App.css';
31

4-
class App extends Component {
5-
render() {
6-
return (
7-
<div className="App">
8-
<p>Divyanshu</p>
9-
</div>
10-
);
11-
}
12-
}
132

14-
export default App;
3+
import React from 'react';
4+
import Button from '../containers/Button';
5+
import NewsItem from '../containers/NewsItem'
6+
import Loading from '../containers/Loading'
7+
let App = () => (
8+
<div>
9+
<Button />
10+
<Loading />
11+
<NewsItem />
12+
</div>
13+
);
14+
export default App;

‎src/actions/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
export const getNews = () => ({
4+
type: 'GET_NEWS',
5+
});

‎src/containers/Button.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { connect } from 'react-redux';
3+
import { getNews } from '../actions';
4+
5+
6+
let Button= ({getNews}) =>(
7+
<button onClick={getNews}>Press to see news</button>
8+
)
9+
const mapDispatchToProps = {
10+
getNews: getNews,
11+
};
12+
13+
14+
// const mapDispatchToProps = (dispatch) => {
15+
// return bindActionCreators({api_data}, dispatch);
16+
// }
17+
18+
Button = connect(null,mapDispatchToProps)(Button);
19+
export default Button;

‎src/containers/Loading.js

Whitespace-only changes.

‎src/containers/NewsItem.js

Whitespace-only changes.

‎src/reducers/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
const reducer = (state = {}, action) => {
3+
switch (action.type) {
4+
case 'GET_NEWS':
5+
return { ...state, loading: true };
6+
case 'NEWS_RECEIVED':
7+
return { ...state, news: action.json[0], loading: false }
8+
default:
9+
return state;
10+
}
11+
};
12+
export default reducer;

‎src/sagas/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import axios from 'axios';
3+
import {put, takeLatest, all} from 'redux-saga/effects';
4+
5+
function *getNews(){
6+
const json = yield axios('https://newsapi.org/v1/articles?source= cnn&apiKey=c39a26d9c12f48dba2a5c00e35684ecc')
7+
.then(response => response.json())
8+
.catch(err => err);
9+
10+
yield put({ type: "NEWS_RECEIVED", json: json.articles, });
11+
}
12+
13+
function* actionWatcher() {
14+
yield takeLatest('GET_NEWS', fetchNews)
15+
}
16+
17+
export default function* rootSaga() {
18+
yield all([
19+
actionWatcher(),
20+
]);
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /