From 5ecf405fde9cbfb0f7b37fac767de2d9d872b059 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedroso <> Date: Wed, 19 Jun 2019 17:08:53 -0400 Subject: [PATCH] Redux thunk and asynchronous API calls --- package-lock.json | 6 ++++++ package.json | 1 + src/js/actions/index.js | 10 ++++++++++ src/js/components/App.jsx | 5 +++++ src/js/components/Posts.jsx | 32 ++++++++++++++++++++++++++++++++ src/js/reducers/index.js | 9 ++++++++- src/js/store/index.js | 3 ++- 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/js/components/Posts.jsx diff --git a/package-lock.json b/package-lock.json index e894f5a..dc5f611 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9570,6 +9570,12 @@ "@redux-saga/core": "^1.0.3" } }, + "redux-thunk": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-2.3.0.tgz", + "integrity": "sha512-km6dclyFnmcvxhAcrQV2AkZmPQjzPDjgVlQtR0EQjxZPyJ0BnMf3in1ryuR8A2qU0HldVRfxYXbFSKlI3N7Slw==", + "dev": true + }, "regenerate": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz", diff --git a/package.json b/package.json index 21b274d..aa70de8 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "react-redux": "^7.1.0", "redux": "^4.0.1", "redux-saga": "^1.0.3", + "redux-thunk": "^2.3.0", "uuid": "^3.3.2" } } diff --git a/src/js/actions/index.js b/src/js/actions/index.js index 11999a9..ac839ba 100644 --- a/src/js/actions/index.js +++ b/src/js/actions/index.js @@ -3,3 +3,13 @@ import { ADD_ARTICLE } from "../constants/action-types" export function addArticle(payload) { return { type: ADD_ARTICLE, payload } } + +export function getData() { + return function(dispatch) { + return fetch("https://jsonplaceholder.typicode.com/posts") + .then(response => response.json()) + .then(json => { + dispatch({ type: "DATA_LOADED", payload: json }); + }) + } +} diff --git a/src/js/components/App.jsx b/src/js/components/App.jsx index a154a6f..ffd1aaf 100644 --- a/src/js/components/App.jsx +++ b/src/js/components/App.jsx @@ -1,6 +1,7 @@ import React from 'react' import List from './List' import Form from './Form' +import Post from './Posts' const App = () => (