-
-
Save indigoviolet/7c73e47786d925640fd0ee74b495298f to your computer and use it in GitHub Desktop.
fin blog embed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adapted from http://redux.js.org/docs/advanced/AsyncActions.html#actionsjs | |
import fetch from 'isomorphic-fetch' | |
export const REQUEST_POSTS = 'REQUEST_POSTS' | |
function requestPosts(subreddit) { | |
return { | |
type: REQUEST_POSTS, | |
subreddit | |
} | |
} | |
export const RECEIVE_POSTS = 'RECEIVE_POSTS' | |
function receivePosts(subreddit, json) { | |
return { | |
type: RECEIVE_POSTS, | |
subreddit, | |
posts: json.data.children.map(child => child.data), | |
receivedAt: Date.now() | |
} | |
} | |
export function fetchPosts(subreddit) { | |
return function (dispatch) { | |
dispatch(requestPosts(subreddit)) | |
return fetch(`https://www.reddit.com/r/${subreddit}.json`) | |
.then( | |
response => response.json(), | |
error => console.log('An error occured.', error) | |
) | |
.then(json => | |
dispatch(receivePosts(subreddit, json)) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment