|
@@ -0,0 +1,140 @@
|
|
|
+/**
|
|
|
+ * state.js
|
|
|
+ *
|
|
|
+ * Collection of everything which has to do with state changes.
|
|
|
+ * - actionTypes
|
|
|
+ * - actions
|
|
|
+ * - reducers
|
|
|
+ **/
|
|
|
+
|
|
|
+import { NAME, DATA } from './constants'
|
|
|
+import { primary, secondary2 } from './initialData'
|
|
|
+// import { call, put, takeEvery } from 'redux-saga/effects'
|
|
|
+
|
|
|
+/** actionTypes define what actions are handeled by the reducer. */
|
|
|
+export const actionTypes = {}
|
|
|
+export const actions = {}
|
|
|
+
|
|
|
+// Generate default actionsTypes CREATE, UPDATE, DELETE for every data type
|
|
|
+DATA.forEach(data => {
|
|
|
+ ['create', 'update', 'delete'].forEach(action => {
|
|
|
+ const actionType = `${action.toUpperCase()}_${data.toUpperCase()}`
|
|
|
+ const actionName = `${action}${data[0].toUpperCase()}${data.substring(1)}`
|
|
|
+ actionTypes[actionType] = `${NAME}/${actionType}`
|
|
|
+ actions[actionName] = (id, data) => { return { type: actionType, id, data } }
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// Add specific actionTypes/actions
|
|
|
+actionTypes['LOAD_SAMPLES'] = `${NAME}/LOAD_SAMPLES`
|
|
|
+actions['loadSamples'] = () => { return { type: `${NAME}/LOAD_SAMPLES` } }
|
|
|
+console.log('State actionTypes, actions', actionTypes, actions)
|
|
|
+
|
|
|
+/** state definition */
|
|
|
+/** It is generally easier to not have another object here. If you can combine everything into
|
|
|
+ * the primary data, do it. */
|
|
|
+export const state = {
|
|
|
+ primary: [],
|
|
|
+ secondary2: []
|
|
|
+}
|
|
|
+console.log('State state', state)
|
|
|
+
|
|
|
+/** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
+function secondaryReducer (state = {}, action) {
|
|
|
+ console.log(`Entering registermap setting reducer.`, state, action)
|
|
|
+ switch (action.type) {
|
|
|
+ case actionTypes.CREATE_SETTING:
|
|
|
+ console.log(`Creating setting.`, state, action)
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ [action.project.projectId]: action.project
|
|
|
+ }
|
|
|
+ case actionTypes.UPDATE_SETTING:
|
|
|
+ console.log(`Update setting.`, state, action)
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ [action.projectId]: action.project
|
|
|
+ }
|
|
|
+ case actionTypes.DELETE_SETTING:
|
|
|
+ console.log(`Delete setting.`, state, action)
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ [action.projectId]: null
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ return state
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function primaryReducer (state = {}, action) {
|
|
|
+ console.log(`Entering registermap reducer.`, state, action)
|
|
|
+ switch (action.type) {
|
|
|
+ case actionTypes.CREATE_REGISTERMAP:
|
|
|
+ console.log(`Creating registermap.`, state, action)
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ [action.project.projectId]: action.project
|
|
|
+ }
|
|
|
+ case actionTypes.UPDATE_REGISTERMAP:
|
|
|
+ console.log(`Update registermap.`, state, action)
|
|
|
+ const idx = state.findIndex(elem => { return (elem.id === action.registermapId) })
|
|
|
+ console.log(`Update registermap.`, idx)
|
|
|
+ const next = [
|
|
|
+ ...state.slice(0, idx),
|
|
|
+ action.registermap,
|
|
|
+ ...state.slice(idx + 1)
|
|
|
+ ]
|
|
|
+ console.log(`Update registermap.`, next)
|
|
|
+ return next
|
|
|
+ case actionTypes.DELETE_REGISTERMAP:
|
|
|
+ console.log(`Delete registermap.`, state, action)
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ [action.projectId]: null
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ return state
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function reducer (state = {}, action) {
|
|
|
+ console.log(`Entering registermap root reducer.`, state, action)
|
|
|
+ if (typeof action.settingId !== 'undefined') {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ registermap: secondaryReducer(state.registermap, action)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (typeof action.registermapId !== 'undefined') {
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ registermap: primaryReducer(state.registermap, action)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return state
|
|
|
+}
|
|
|
+console.log('State reducer', reducer)
|
|
|
+
|
|
|
+/** sagas are asynchronous workers (JS generators) to handle the state.
|
|
|
+// Worker
|
|
|
+export function * incrementAsync () {
|
|
|
+ try {
|
|
|
+ const data = yield call(Api.isIncrementOk)
|
|
|
+ yield put({ type: 'INCREMENT_SUCCESS', data })
|
|
|
+ } catch (error) {
|
|
|
+ yield put({ type: 'INCREMENT_FAIL', error})
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Watcher (intercepts INCREMENT_REQUEST, dispatches INCREMENT_SUCCESS or INCREMENT_FAIL in return.)
|
|
|
+export function * watchIncrementAsync () {
|
|
|
+ yield takeEvery('INCREMENT_REQUEST', incrementAsync)
|
|
|
+}
|
|
|
+
|
|
|
+// export all sagas in parallel
|
|
|
+function * sagas () {
|
|
|
+ yield takeEvery('INCREMENT_REQUEST')
|
|
|
+ yield takeEvery('DECREMENT_REQUEST')
|
|
|
+} */
|
|
|
+export const sagas = null
|
|
|
+console.log('State sagas', sagas)
|