Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import { createStore, applyMiddleware, compose } from 'redux'
  2.  
  3. import { browserHistory } from 'react-router'
  4. import { routerMiddleware } from 'react-router-redux'
  5.  
  6. import { makeRootReducer } from '../reducer'
  7.  
  8. const makeStore = (initialState = {}) => {
  9. const middleware = [
  10. routerMiddleware(browserHistory)
  11. // other middlewares
  12. ]
  13.  
  14. const store = createStore(
  15. makeRootReducer(),
  16. initialState,
  17. compose(
  18. applyMiddleware(...middleware)
  19. )
  20. )
  21. store.asyncReducers = {}
  22.  
  23. return store
  24. }
  25.  
  26. module.exports = makeStore
  27.  
  28. import { combineReducers } from 'redux'
  29. import { routerReducer } from 'react-router-redux'
  30. import { combineForms } from 'react-redux-form'
  31.  
  32. import { initialLoginState } from './components/forms/login'
  33.  
  34. const makeRootReducer = (asyncReducers) => {
  35. const customReducers = {
  36. // other of my own imported reducers
  37. }
  38.  
  39. const forms = {
  40. login: initialLoginState
  41. }
  42.  
  43. const allReducers = {
  44. ...customReducers,
  45. forms: combineForms(forms, 'forms'),
  46. routing: routerReducer,
  47. ...asyncReducers
  48. }
  49.  
  50. return combineReducers(allReducers)
  51. }
  52.  
  53. // mutate the store just this once.
  54. const injectReducer = (store, { key, reducer }) => {
  55. store.asyncReducers[key] = reducer // eslint-disable-line no-param-reassign
  56. store.replaceReducer(makeRootReducer(store.asyncReducers))
  57. }
  58.  
  59. module.exports = {
  60. makeRootReducer,
  61. injectReducer
  62. }
  63.  
  64. <Form model='forms.login' onSubmit={login => console.debug(login)}>
  65. <label>Email</label>
  66. <Control.text model='.username' />
  67. <label>Password</label>
  68. <Control type='password' model='.password' />
  69. <button type="submit">Login</button>
  70. </Form>
  71.  
  72. state:
  73. forms:
  74. login:
  75. username: ''
  76. password: ''
  77. forms:
  78. $form:
  79. focus: false
  80. # ... etc
  81. login:
  82. $form:
  83. focus: false
  84. # ... etc
  85. username: ''
  86. password: ''
  87.  
  88. const allReducers = {
  89. ...customReducers,
  90. forms: combineForms(forms),
  91. routing: routerReducer,
  92. ...asyncReducers
  93. }
  94.  
  95. state:
  96. forms:
  97. login:
  98. username: ''
  99. password: ''
  100. forms:
  101. $form:
  102. focus: false
  103. # ... etc
  104. login:
  105. $form:
  106. focus: false
  107. # ... etc
  108. username: ''
  109. password: ''
  110.  
  111. state:
  112. forms:
  113. login:
  114. $form:
  115. focus: false
  116. # ... etc
  117. username: ''
  118. password: ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement