-
-
Save indigoviolet/f5307575bd00eb8d436dbce8ce456108 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
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