state.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /** @module */
  2. /**
  3. * state.js
  4. *
  5. * The application uses Redux to handle the state.
  6. * Redux has a **store** which allows access to the one big state of the
  7. * modules. It also has a dispatcher, which receives actions and calls
  8. * reducer functions to act on them.
  9. *
  10. * The store handles all state changes. For that, a module
  11. * defines **action creators**, which are helper functions generating action
  12. * objects. An action object contains a 'type' property, which gives the action
  13. * a name, and a payload, which will be processed.
  14. *
  15. * The processing unit is called a **reducer** and is exported as 'reducer' in
  16. * this file. It is a function which receives
  17. * the current state and an action and returns the processed state. It is good
  18. * practice, that the reducer doesn't change the state (which is passed by
  19. * reference), but creates a modified state and returns it. Also, the reducer
  20. * should return immediately. If an asynchronous operation has to be executed,
  21. * it is handled through redux-sagas, which is basically a concurrent process
  22. * triggered by a reducer, which generates a new action once it's completed.
  23. *
  24. * Action creators (defined as 'actions' in this file) are bound to the
  25. * Redux dispatcher in 'index.js'. When they are called, the dispatcher sends
  26. * the greated action to **all** reducers. This module, and other modules which
  27. * change the state of this module, use these action creators to modifiy the
  28. * state.
  29. *
  30. * This file exports everything which is necessary to set up and operate the
  31. * state of this module.
  32. * - actions
  33. * - reducers
  34. * - state
  35. * - sagas
  36. **/
  37. // Import NAME and DATA to be able to create action creators */
  38. import { NAME, DATA } from './constants'
  39. import { primary } from './initialData'
  40. import { delay } from 'redux-saga'
  41. import { call, put, takeEvery } from 'redux-saga/effects'
  42. /** actionTypes define what actions are handeled by the reducer. */
  43. const actionTypes = {}
  44. export const actionCreators = {}
  45. // Generate default actionsTypes CREATE, UPDATE, REMOVE
  46. DATA.forEach((dataItem, idx) => {
  47. ['create', 'update', 'remove'].forEach(action => {
  48. // The Redux convention is to name action types e.g. demo_module/UPDATE
  49. // For action creators, we define here the name e.g. removePrimary(id, data)
  50. // where id is the element id and data is the element itself.
  51. const actionType = `${action.toUpperCase()}_${dataItem.toUpperCase()}`
  52. const actionName = `${action}${dataItem[0].toUpperCase()}${dataItem.substring(1)}`
  53. if (idx === 0) {
  54. actionCreators[actionName] = (id, data) => { return { type: `${NAME}/${actionType}`, id, data } }
  55. } else {
  56. actionCreators[actionName] = (primaryId, id, data) => { return { type: `${NAME}/${actionType}`, primaryId, id, data } }
  57. }
  58. actionTypes[actionType] = `${NAME}/${actionType}`
  59. })
  60. })
  61. // Add specific action creators here:
  62. actionCreators['loadSamples'] = () => { return { type: `${NAME}/LOAD_SAMPLES` } }
  63. actionTypes['LOAD_SAMPLES'] = `${NAME}/LOAD_SAMPLES`
  64. console.log('State actionTypes, actionCreators', actionTypes, actionCreators)
  65. /** state definition */
  66. /** It is generally easier to not have another object here. */
  67. export const state = []
  68. console.log('State state', state)
  69. /** reducer is called by the redux dispatcher and handles all component actions */
  70. /** The module's root reducer tries to split up the state and let specific
  71. * specialized reducers handle the work. This is not necessary, but more
  72. * convenient and readable. */
  73. // In this example it is assumed, that the secondary items have a key 'primaryId',
  74. // which references the primary data element.
  75. export function reducer (state = [], action) {
  76. // Return immediately, if action type doesn't match
  77. if (typeof action.type === 'undefined' || !action.type.match(RegExp(NAME))) {
  78. return state
  79. }
  80. // find the primary data element with the matching id.
  81. let idx
  82. let nextState
  83. console.log(`Entering demo_module root reducer.`, state, action, idx)
  84. if (action.type.match(/SECONDARY/)) {
  85. idx = state.findIndex(elem => { return (action.primaryId === elem.id) })
  86. // leave immediately, if no idx was found (operations on secondary not valid if there's no primary)
  87. if (idx < 0) {
  88. return state
  89. }
  90. console.log(`Using secondary reducer.`)
  91. const subState = state[idx].secondary
  92. const reducedState = secondaryReducer(subState, action)
  93. nextState = [ ...state.slice(0, idx), {...state[idx], secondary: reducedState}, ...state.slice(idx + 1) ]
  94. console.log('Leaving demo_module root reducer', subState, reducedState, nextState)
  95. return nextState
  96. }
  97. switch (action.type) {
  98. case actionTypes.CREATE_PRIMARY:
  99. nextState = [ ...state, action.data ]
  100. console.log('Creating primary', state, nextState)
  101. return nextState
  102. case actionTypes.UPDATE_PRIMARY:
  103. nextState = [ ...state.slice(0, idx), action.data, ...state.slice(idx + 1) ]
  104. console.log('Updating primary', state, nextState)
  105. return nextState
  106. case actionTypes.REMOVE_PRIMARY:
  107. console.log('wtf')
  108. nextState = [ ...state.slice(0, idx), ...state.slice(idx + 1) ]
  109. console.log('Removing primary', state, nextState)
  110. return nextState
  111. case actionTypes.LOAD_SAMPLES:
  112. nextState = primary
  113. console.log('Loading sample data', state, nextState)
  114. return nextState
  115. default:
  116. return state
  117. }
  118. }
  119. function secondaryReducer (state = [], action) {
  120. console.log(`Entering secondary reducer.`, state, action)
  121. const idx = state.findIndex(elem => {
  122. console.log('searching index', action, elem)
  123. return (action.id === elem.id)
  124. })
  125. let nextState
  126. switch (action.type) {
  127. case actionTypes.CREATE_SECONDARY:
  128. nextState = [ ...state, action.data ]
  129. console.log(`Creating secondary.`, state, nextState)
  130. return nextState
  131. case actionTypes.UPDATE_SECONDARY:
  132. nextState = [ ...state.slice(0, idx), action.data, ...state.slice(idx + 1) ]
  133. console.log(`Updating secondary.`, idx, state, nextState)
  134. return nextState
  135. case actionTypes.REMOVE_SECONDARY:
  136. nextState = [ ...state.slice(0, idx), ...state.slice(idx + 1) ]
  137. console.log(`Removing secondary.`, idx, state, nextState)
  138. return nextState
  139. default:
  140. return state
  141. }
  142. }
  143. console.log('State reducer', reducer)
  144. /** sagas are asynchronous workers (JS generators) to handle the state.
  145. // Worker
  146. export function * incrementAsync () {
  147. try {
  148. const data = yield call(Api.isIncrementOk)
  149. yield put({ type: 'INCREMENT_SUCCESS', data })
  150. } catch (error) {
  151. yield put({ type: 'INCREMENT_FAIL', error})
  152. }
  153. }
  154. // Watcher (intercepts INCREMENT_REQUEST, dispatches INCREMENT_SUCCESS or INCREMENT_FAIL in return.)
  155. export function * watchIncrementAsync () {
  156. yield takeEvery('INCREMENT_REQUEST', incrementAsync)
  157. }
  158. // export all sagas in parallel
  159. function * sagas () {
  160. yield takeEvery('INCREMENT_REQUEST')
  161. yield takeEvery('DECREMENT_REQUEST')
  162. } */
  163. export const sagas = null
  164. console.log('State sagas', sagas)