Skip to content

Instantly share code, notes, and snippets.

@indigoviolet
Last active December 23, 2017 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indigoviolet/7c73e47786d925640fd0ee74b495298f to your computer and use it in GitHub Desktop.
Save indigoviolet/7c73e47786d925640fd0ee74b495298f to your computer and use it in GitHub Desktop.
fin blog embed
// 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