Last active
August 11, 2017 05:39
-
-
Save ayroblu/315438c8343e656bc761ff8b43c6daa6 to your computer and use it in GitHub Desktop.
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 {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