Skip to content

Instantly share code, notes, and snippets.

@kechol
Last active January 29, 2016 06:21
Show Gist options
  • Save kechol/7ba2feb96a0c0f1f8108 to your computer and use it in GitHub Desktop.
Save kechol/7ba2feb96a0c0f1f8108 to your computer and use it in GitHub Desktop.
AsyncActionCreator for Redux,
import _ from 'lodash'
import { createAction } from 'redux-actions'a
export const FETCH_API_REQUEST = 'FETCH_API_REQUEST'
export const FETCH_API_SUCCESS = 'FETCH_API_SUCCESS'
export const FETCH_API_FAILURE = 'FETCH_API_FAILURE'
export function createAsyncAction (type, payload, meta) {
const action = {type, payload, meta}
const subActionTypes = {
FETCH_API_REQUEST: FETCH_API_REQUEST,
FETCH_API_SUCCESS: FETCH_API_SUCCESS,
FETCH_API_FAILURE: FETCH_API_FAILURE
}
const asyncActions = _.mapValues(subActionTypes, (subtype) => {
const payload = (o) => o.payload
const meta = (o) => _.assign({}, o.meta)
return createAction(`${type}.${subtype}`, payload, meta)
})
return (dispatch) => {
dispatch(asyncActions[FETCH_API_REQUEST](action))
const fetchOptions = _.assign({},
config.FETCH_DEFAULTS,
_.pick(action.meta, ['method', 'headers', 'body'])
)
return fetch(action.meta.endpoint, fetchOptions)
.then( resp => {
action.meta.response = _.pick(resp,
['status', 'headers', 'ok', 'url', 'statusText']
)
return resp.json()
})
.then( json => {
action.payload = json
dispatch(asyncActions[FETCH_API_SUCCESS](action))
})
.catch( err => {
action.payload = err
dispatch(asyncActions[FETCH_API_FAILURE](action))
})
}
}
import _ from 'lodash'
import { createAsyncAction } from './asyncActionCreator'
export const GET_SOMETHING = 'GET_SOMETHING'
export function getSomething (payload) {
const fd = new FormData()
_.mapKeys(payload, (val, key) => fd.append(key, val))
return createAsyncAction(
GET_SOMETHING,
payload,
{
endpoint: 'http://example.com/something.json',
body: fd
}
)
}
{
"name": "redux-async-actions",
"version": "0.0.0",
"description": "AsyncActionCreator example",
"dependencies": {
"es6-promise": "^3.0.2",
"lodash": "^3.10.1",
"redux": "^3.0.4",
"redux-actions": "^0.9.0",
"redux-thunk": "^1.0.0",
"whatwg-fetch": "^0.11.0"
}
}
import { handleActions } from 'redux-actions'
import { GET_SOMETHING } from './action'
export default const reducer = handleActions({
'GET_SOMETHING.FETCH_API_REQUEST': (state, action) => {
console.log('I\'m fetching something new...')
return {...state}
},
'GET_SOMETHING.FETCH_API_SUCCESS': (state, action) => {
console.log('I got something new!!')
state = {
...state,
something: action.payload
}
return state
},
'GET_SOMETHING.FETCH_API_FAILURE': (state, action) => {
console.error('I couldn\'t fetch anything...')
return {...state}
}
}, {something: 'old'})
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
export default const store = compose(
applyMiddleware(thunk)
)(createStore)(reducer, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment