Skip to content

Instantly share code, notes, and snippets.

@indigoviolet
Created October 4, 2017 08:46
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/f5307575bd00eb8d436dbce8ce456108 to your computer and use it in GitHub Desktop.
Save indigoviolet/f5307575bd00eb8d436dbce8ce456108 to your computer and use it in GitHub Desktop.
fin blog embed
import { createAsyncAction } from 'helpers/async';
import fetch from 'isomorphic-fetch'
// (pretend for a moment that such an action exists)
import { navigate, SIGN_IN } from 'actions/navigation';
// takes args: { subreddit }
function postsWithLikesRequest({ subreddit }) {
// returns a 'thunk promise' that uses dispatch and getState
return function(dispatch, getState) {
const userId = getState().userId;
if(!userId) {
// we have the dispatch function in our async action!
dispatch(navigate(SIGN_IN))
} else {
return fetch(`https://www.reddit.com/r/${subreddit}.json?likes=true&userId=${userId}`)
.then(
response => response.json(),
error => console.log('An error occured.', error)
)
}
}
}
const FETCH_POSTS_WITH_LIKES = 'Actions/Posts/FetchWithLikes';
export const fetchPostsWithLikes = createAsyncAction(FETCH_POSTS_WITH_LIKES, postsWithLikesRequest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment