-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian
committed
Jul 19, 2017
1 parent
3b89afc
commit f935b1f
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# redux-auto | cookbook 🕮 | ||
|
||
## other middleware | ||
|
||
### using with redux-forms | ||
|
||
import { createStore, applyMiddleware, combineReducers } from 'redux'; | ||
import { reducer as forms } from 'redux-form'; | ||
|
||
import { auto, mergeReducers } from 'redux-auto'; | ||
|
||
const webpackModules = require.context("./store", true, /\.js$/); | ||
|
||
let middleware = applyMiddleware(auto(webpackModules, webpackModules.keys())); | ||
|
||
export default createStore( combineReducers(mergeReducers({forms})), middleware ); | ||
|
||
**bonus tip ✍** using with react-redux | ||
|
||
|
||
ActivePeriod = reduxForm({ form: 'Schedule/ActivePeriod'})(ActivePeriod) | ||
export default ActivePeriod | ||
const map_to_props = () => { | ||
|
||
return { }; | ||
}; | ||
|
||
export default connect(map_to_props)(ActivePeriod); | ||
|
||
### using with redux devtools | ||
// ... | ||
if(window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) | ||
middleware = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__(middleware); | ||
// ... | ||
|
||
## working with async and server calls | ||
|
||
### How to call multiple end-points at the same time? | ||
You hand all api call within your action function. If you have an array of urls, you can use [map] to transform each calls. | ||
|
||
export function action (payload){ | ||
var endpoints = ['//localhost:3000/api/post/1','//localhost:3000/api/post/2'] | ||
return Promise.all(endpoints.map(url => fetch(url) | ||
.then(resp => resp.json()))) | ||
.then(([firstPost,SecPost])=>({firstPost,SecPost})) | ||
} | ||
|
||
The above code is | ||
|
||
1. Map over endpoints string to create your Ajax calls | ||
2. Call json on the result of each fetch | ||
3. Then combine all responses using Promise.all | ||
4. Destructor the array into 2 variables | ||
5. Return a new Object with 2 properties | ||
|
||
**result** in you reduct function will now have `firstPost`, `SecPost` that can be used with | ||
|
||
export default function (user, payload, stage, result) { | ||
|
||
switch(stage){ | ||
case 'FULFILLED': | ||
return {...user, ...payload} // add firstPost, SecPost into users | ||
break; | ||
case 'REJECTED': | ||
console.error("problem loading user from server",result) | ||
break; | ||
case 'PENDING': | ||
default : | ||
break; | ||
} | ||
return user; | ||
} | ||
|
||
**bonus tip ✍** | ||
|
||
export function fulfilled (announcement, payload, {groups, lines_and_stations, devices}){ | ||
return announcement; | ||
} fulfilled.chain = actions.announcements.send_text_announcement.clear | ||
|
||
[map]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map |