Parcourir la source

added primary CUD actions

Tomi Cvetic il y a 8 ans
Parent
commit
b736ad2422
2 fichiers modifiés avec 23 ajouts et 22 suppressions
  1. 0 10
      src/demo_module/initialData.js
  2. 23 12
      src/demo_module/state.js

+ 0 - 10
src/demo_module/initialData.js

@@ -13,10 +13,6 @@ const primary = [{
   id: 'primary-1',
   name: 'String1',
   active: true,
-  items: [
-    { id: 'item1-1' },
-    { id: 'item1-2' }
-  ],
   secondar: [{
     id: 'id2',
     name: 'secondary-1'
@@ -25,12 +21,6 @@ const primary = [{
   id: 'primary-2',
   name: 'String2',
   active: false,
-  items: [
-    { id: 'item2-1' },
-    { id: 'item2-2' },
-    { id: 'item2-3' },
-    { id: 'item2-4' }
-  ],
   secondary: [{
     id: 'id1',
     name: 'secondary-2'

+ 23 - 12
src/demo_module/state.js

@@ -48,7 +48,7 @@ export const actionCreators = {}
 
 // Generate default actionsTypes CREATE, UPDATE, REMOVE
 DATA.forEach(dataItem => {
-  ['create', 'update', 'delete'].forEach(action => {
+  ['create', 'update', 'remove'].forEach(action => {
     // The Redux convention is to name action types e.g. demo_module/UPDATE
     // For action creators, we define here the name e.g. removePrimary(id, data)
     // where id is the element id and data is the element itself.
@@ -72,12 +72,12 @@ removeSecondary2 (id, data) {
 
 // Add specific action creators here:
 actionCreators['loadSamples'] = () => { return { type: `${NAME}/LOAD_SAMPLES` } }
+actionTypes['LOAD_SAMPLES'] = `${NAME}/LOAD_SAMPLES`
 console.log('State actionTypes, actionCreators', actionTypes, actionCreators)
 
 
 /** state definition */
-/** It is generally easier to not have another object here. If you can combine everything into
-  * the primary data, do it. */
+/** It is generally easier to not have another object here. */
 export const state = []
 console.log('State state', state)
 
@@ -89,17 +89,19 @@ console.log('State state', state)
   // In this example it is assumed, that the secondary items have a key 'primaryId',
   // which references the primary data element.
 export function reducer (state = [], action) {
-  console.log(`Entering demo_module root reducer.`, state, action)
+  // find the primary data element with the matching id.
   const idx = state.findIndex(elem => {return (action.id === elem.id)})
+  console.log(`Entering demo_module root reducer.`, state, action, idx)
+  let nextState
   if (action.type.match(/SECONDARY/)) {
-    // leave immediately, if no idx was found (operations on secondary not valid )
+    // leave immediately, if no idx was found (operations on secondary not valid if there's no primary)
     if (idx < 0) {
       return state
     }
     console.log(`Using secondary reducer.`)
     const subState = state.secondary
     const reducedState = secondaryReducer(subState, action)
-    const nextState = {
+    nextState = {
       ...state,
       secondary: reducedState
     }
@@ -108,13 +110,22 @@ export function reducer (state = [], action) {
   }
   switch (action.type) {
     case actionTypes.CREATE_PRIMARY:
-      return state
+      nextState = [ ...state, action.data ]
+      console.log('Creating primary', state, nextState)
+      return nextState
     case actionTypes.UPDATE_PRIMARY:
-      return state
+      nextState = [ ...state.slice(0, idx), action.data, ...state.slice(idx + 1) ]
+      console.log('Updating primary', state, nextState)
+      return nextState
     case actionTypes.REMOVE_PRIMARY:
-      return state
+      console.log('wtf')
+      nextState = [ ...state.slice(0, idx), ...state.slice(idx+1) ]
+      console.log('Removing primary', state, nextState)
+      return nextState
     case actionTypes.LOAD_SAMPLES:
-      return state
+      nextState = primary
+      console.log('Loading sample data', state, nextState)
+      return nextState
     default:
       return state
   }
@@ -133,9 +144,9 @@ function secondaryReducer (state = [], action) {
       nextState = [ ...state.slice(0, idx), action.data, ...state.slice(idx + 1)]
       console.log(`Updating secondary.`, idx, state, nextState)
       return nextState
-    case actionTypes.DELETE_SECONDARY:
+    case actionTypes.REMOVE_SECONDARY:
       nextState = [ ...state.slice(0, idx), ...state.slice(idx + 1) ]
-      console.log(`Deleting secondary.`, idx, state, nextState)
+      console.log(`Removing secondary.`, idx, state, nextState)
       return nextState
     default:
       return state