A
A
Artem Maximum2016-04-21 13:25:34
React
Artem Maximum, 2016-04-21 13:25:34

Having received a json response with nested data, how is it possible to send data to two Reducers?

I call from the view Action in which I eventually get a response from the ajax request.
The received data has the following structure:

{
  "departments": [
    {
      "id": 1,
      "name": "General Department",
      "operators": [
        {
          "id": 1,
          "name": "Artem Maximum"
        },
        {
          "id": 2,
          "name": "Chehonte"
        }
      ]
    },
    {
      "id": 2,
      "name": "Sales Department",
      "operators": [
        {
          "id": 3,
          "name": "Ne0d1n"
        },
        {
          "id": 4,
          "name": "Geronimo"
        },
        {
          "id": 5,
          "name": "Fungus"
        }
      ]
    }
  ]
}

I pass the received data through normalizr and as a result I have the following data:
{
  "entities": {
    "departments": {
      "1": {
        "id": 1,
        "name": "General Department",
        "operators": [1, 2]
      },
      "2": {
        "id": 2,
        "name": "Sales Department",
        "operators": [3, 4, 5]
      }
    },
    "operators": {
      "1": {
        "id": 1,
        "name": "Artem Maximum"
      },
      "2": {
        "id": 2,
        "name": "Chehonte"
      },
      "3": {
        "id": 3,
        "name": "Ne0d1n"
      },
      "4": {
        "id": 4,
        "name": "Geronimo"
      },
      "5": {
        "id": 5,
        "name": "Fungus"
      }
    }
  },
  "result": {
    "departments": [1, 2]
  }
}

Is it possible to send data to the Reducer of both the current and foreign entities in this Action?
That is, in this case, there are two entities [Departments, Operators] and I need to update both entities in the Store.
I doubt whether it is correct that I want to send data to an external Reducer.
However, it seems to me completely impossible to process data of an extraneous entity inside the Department Reducer.
I would be very grateful if anyone could suggest the best practices for this task.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rmaksim, 2016-04-22
@rmaksim

in theory, there is no problem to call several Actions with the data they need,
because for each one only ITS data is needed? So?
here is one example on github

function receiveUserPre(userId, entities) {
  return dispatch => {
    dispatch(receiveUser(entities));
    dispatch(fetchUserData(userId, entities.users[userId].username));
  };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question