Skip to content

Instantly share code, notes, and snippets.

@ayroblu
Last active August 11, 2017 05:39
Show Gist options
  • Save ayroblu/315438c8343e656bc761ff8b43c6daa6 to your computer and use it in GitHub Desktop.
Save ayroblu/315438c8343e656bc761ff8b43c6daa6 to your computer and use it in GitHub Desktop.
import {MainApi} from '../api'
// import * as postActions from '../actions/post'
// Similar to redux thunk
// type ReduxAction = {
// dispatch: function
// , getState: function
// }
type StateFuncs = {
getState: function
, setState: function
}
// It means you can just put this.props in and get a good outcome
async function getPosts(...params, {setState, getState}: StateFuncs){
// first, we check posts
if (getState().posts.isPostsLoading) {
return
}
if (getState().posts.posts.length) {
return // caching?
}
setState({isPostsLoading: true})
const {token} = getState().user
const api = new MainApi({token})
try {
const posts = await api.getPosts()
setState({isPostsLoading: false, posts})
} catch (err) {
console.error('err', err.message, err)
setState({isPostsLoading: false, errorText: 'Sorry, we failed to reach the server'})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment