P
P
Pogran2016-09-27 16:17:02
React
Pogran, 2016-09-27 16:17:02

How can this version of state be rewritten?

I have this option to return one of the cases in the reducer

const el = state.structure[action.id].childIds[0];

      if(el) {
        if(action.blockId) {
          return {
            ...state,
            structure: {
              ...state.structure,
              [el] : {
                ...state.structure[el],
                visibility: true
              },
              [action.id] : {
                ...state.structure[action.id],
                render : action.render
              },
              [action.blockId] : {
                ...state.structure[action.blockId],
                block: action.block,
              }
            }
          };
        } else {
          return {
            ...state,
            structure: {
              ...state.structure,
              [el] : {
                ...state.structure[el],
                visibility: true
              },
              [action.id] : {
                ...state.structure[action.id],
                render : action.render
              },
            }
          };
        }
      } else {
        if(action.blockId) {
          return {
            ...state,
            structure: {
              ...state.structure,
              [action.id] : {
                ...state.structure[action.id],
                render : action.render
              },
              [action.blockId] : {
                ...state.structure[action.blockId],
                block: action.block,
              }
            }
          };
        } else {
          return {
            ...state,
            structure: {
              ...state.structure,
              [action.id] : {
                ...state.structure[action.id],
                render : action.render
              }
            }
          };
        }

      }

How can you rewrite it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2016-09-27
@Pogran

It looks like you have one action followed by several. Try to break them up - you can use one common action creator:

function removeFromStructure(id) {
  return { type: REMOVE_FROM_STRUCTURE, paylodad: id }
}
function doSomething(id) {
  return { type: DO_SOMETHING, paylodad: id }
}
function commonAction(structureObject) {
  return structureObject.blockId ? removeFromStructure(id) : doSomething(id)
}

The code I provided is just an example. Actually - break it into several actions and process each "small" action in the reducer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question