demo_module_state.js.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: demo_module/state.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: demo_module/state.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/** @module */
  20. /**
  21. * state.js
  22. *
  23. * The application uses Redux to handle the state.
  24. * Redux has a **store** which allows access to the one big state of the
  25. * modules. It also has a dispatcher, which receives actions and calls
  26. * reducer functions to act on them.
  27. *
  28. * The store handles all state changes. For that, a module
  29. * defines **action creators**, which are helper functions generating action
  30. * objects. An action object contains a 'type' property, which gives the action
  31. * a name, and a payload, which will be processed.
  32. *
  33. * The processing unit is called a **reducer** and is exported as 'reducer' in
  34. * this file. It is a function which receives
  35. * the current state and an action and returns the processed state. It is good
  36. * practice, that the reducer doesn't change the state (which is passed by
  37. * reference), but creates a modified state and returns it. Also, the reducer
  38. * should return immediately. If an asynchronous operation has to be executed,
  39. * it is handled through redux-sagas, which is basically a concurrent process
  40. * triggered by a reducer, which generates a new action once it's completed.
  41. *
  42. * Action creators (defined as 'actions' in this file) are bound to the
  43. * Redux dispatcher in 'index.js'. When they are called, the dispatcher sends
  44. * the greated action to **all** reducers. This module, and other modules which
  45. * change the state of this module, use these action creators to modifiy the
  46. * state.
  47. *
  48. * This file exports everything which is necessary to set up and operate the
  49. * state of this module.
  50. * - actions
  51. * - reducers
  52. * - state
  53. * - sagas
  54. **/
  55. // Import NAME and DATA to be able to create action creators */
  56. import { NAME, DATA } from './constants'
  57. import { primary } from './initialData'
  58. import { delay } from 'redux-saga'
  59. import { call, put, takeEvery } from 'redux-saga/effects'
  60. /** state definition */
  61. /** It is generally easier to not have another object here. */
  62. export const state = []
  63. console.log('State state', state)
  64. /** actionTypes define what actions are handeled by the reducer. */
  65. const actionTypes = {}
  66. export const actionCreators = {}
  67. // Generate default actionsTypes CREATE, UPDATE, REMOVE
  68. DATA.forEach(dataItem => {
  69. ['create', 'update', 'remove'].forEach(action => {
  70. // The Redux convention is to name action types e.g. demo_module/UPDATE
  71. // For action creators, we define here the name e.g. removePrimary(id, data)
  72. // where id is the element id and data is the element itself.
  73. const actionType = `${action.toUpperCase()}_${dataItem.toUpperCase()}`
  74. const actionName = `${action}${dataItem[0].toUpperCase()}${dataItem.substring(1)}`
  75. actionCreators[actionName] = (id, data) => { return { type: `${NAME}/${actionType}`, id, data } }
  76. actionTypes[actionType] = `${NAME}/${actionType}`
  77. })
  78. })
  79. // Add specific action creators here:
  80. actionCreators['loadSamples'] = () => { return { type: `${NAME}/LOAD_SAMPLES` } }
  81. actionTypes['LOAD_SAMPLES'] = `${NAME}/LOAD_SAMPLES`
  82. console.log('State actionTypes, actionCreators', actionTypes, actionCreators)
  83. /** reducer is called by the redux dispatcher and handles all component actions */
  84. /** The module's root reducer tries to split up the state and let specific
  85. * specialized reducers handle the work. This is not necessary, but more
  86. * convenient and readable. */
  87. // In this example it is assumed, that the secondary items have a key 'primaryId',
  88. // which references the primary data element.
  89. export function reducer (state = [], action) {
  90. // find the primary data element with the matching id.
  91. const idx = state.findIndex(elem => {return (action.id === elem.id)})
  92. console.log(`Entering demo_module root reducer.`, state, action, idx)
  93. let nextState
  94. if (action.type.match(/SECONDARY/)) {
  95. // leave immediately, if no idx was found (operations on secondary not valid if there's no primary)
  96. if (idx &lt; 0) {
  97. return state
  98. }
  99. console.log(`Using secondary reducer.`)
  100. const subState = state.secondary
  101. const reducedState = secondaryReducer(subState, action)
  102. nextState = {
  103. ...state,
  104. secondary: reducedState
  105. }
  106. console.log('Leaving demo_module root reducer', subState, reducedState, nextState)
  107. return nextState
  108. }
  109. switch (action.type) {
  110. case actionTypes.CREATE_PRIMARY:
  111. nextState = [ ...state, action.data ]
  112. console.log('Creating primary', state, nextState)
  113. return nextState
  114. case actionTypes.UPDATE_PRIMARY:
  115. console.log(idx, state[idx], action.data, {...state[idx], ...action.data})
  116. nextState = [ ...state.slice(0, idx), {...state[idx], ...action.data}, ...state.slice(idx + 1) ]
  117. console.log('Updating primary', state, nextState)
  118. return nextState
  119. case actionTypes.REMOVE_PRIMARY:
  120. console.log('wtf')
  121. nextState = [ ...state.slice(0, idx), ...state.slice(idx+1) ]
  122. console.log('Removing primary', state, nextState)
  123. return nextState
  124. case actionTypes.LOAD_SAMPLES:
  125. nextState = primary
  126. console.log('Loading sample data', state, nextState)
  127. return nextState
  128. default:
  129. return state
  130. }
  131. }
  132. function secondaryReducer (state = [], action) {
  133. console.log(`Entering secondary reducer.`, state, action)
  134. const idx = state.findIndex(elem => {return (action.id === elem.id)})
  135. let nextState
  136. switch (action.type) {
  137. case actionTypes.CREATE_SECONDARY:
  138. nextState = [ ...state, action.data ]
  139. console.log(`Creating secondary.`, state, nextState)
  140. return nextState
  141. case actionTypes.UPDATE_SECONDARY:
  142. nextState = [ ...state.slice(0, idx), action.data, ...state.slice(idx + 1)]
  143. console.log(`Updating secondary.`, idx, state, nextState)
  144. return nextState
  145. case actionTypes.REMOVE_SECONDARY:
  146. nextState = [ ...state.slice(0, idx), ...state.slice(idx + 1) ]
  147. console.log(`Removing secondary.`, idx, state, nextState)
  148. return nextState
  149. default:
  150. return state
  151. }
  152. }
  153. console.log('State reducer', reducer)
  154. /** sagas are asynchronous workers (JS generators) to handle the state.
  155. // Worker
  156. export function * incrementAsync () {
  157. try {
  158. const data = yield call(Api.isIncrementOk)
  159. yield put({ type: 'INCREMENT_SUCCESS', data })
  160. } catch (error) {
  161. yield put({ type: 'INCREMENT_FAIL', error})
  162. }
  163. }
  164. // Watcher (intercepts INCREMENT_REQUEST, dispatches INCREMENT_SUCCESS or INCREMENT_FAIL in return.)
  165. export function * watchIncrementAsync () {
  166. yield takeEvery('INCREMENT_REQUEST', incrementAsync)
  167. }
  168. // export all sagas in parallel
  169. function * sagas () {
  170. yield takeEvery('INCREMENT_REQUEST')
  171. yield takeEvery('DECREMENT_REQUEST')
  172. } */
  173. export const sagas = null
  174. console.log('State sagas', sagas)
  175. </code></pre>
  176. </article>
  177. </section>
  178. </div>
  179. <nav>
  180. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-AutoMate.html">AutoMate</a></li><li><a href="module-demo_module.html">demo_module</a></li><li><a href="module-demo_module_components_index.html">demo_module/components/index</a></li><li><a href="module-demo_module_constants.html">demo_module/constants</a></li><li><a href="module-demo_module_initialData.html">demo_module/initialData</a></li><li><a href="module-demo_module_state.html">demo_module/state</a></li><li><a href="module-project.html">project</a></li><li><a href="module-project_components.html">project/components</a></li><li><a href="module-project_state.html">project/state</a></li><li><a href="module-registermap.html">registermap</a></li></ul>
  181. </nav>
  182. <br class="clear">
  183. <footer>
  184. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Mar 14 2017 18:34:23 GMT+0100 (W. Europe Standard Time)
  185. </footer>
  186. <script> prettyPrint(); </script>
  187. <script src="scripts/linenumber.js"> </script>
  188. </body>
  189. </html>