0

I need to update the status of the agent. If the status is verified, an API needs document_name(multiple) and reason. If the status is on hold and rejected, an API needs the only reason.

This is the object of an agent.

{
    "active": true,
    "agent_info": [], // an array of object where document_name and reason has to be updated with
    "user_role": "enduser",
    "last_name": "m",
    "middle_name": null,
    "username": "[email protected]",
    "_id": "594f4665ae36b70029f80ba0",
    "first_name": "pm",
    "email": "[email protected]",
}

when submitting the status, I trigger the following action for a saga

this.props.updateAgentStatus(status, agent, accepted, reason);

satus is the status of agent(verified, on hold or rejected) agent is the detail of agent accepted is the document in case the status is verified reason is the reason for the status

function* updateAgent(action) {
  const agentId = action.agent._id;
  const status = action.status;
  let agent = yield select(selectAgent(), action);
  let updatedAgent;
  let statusUpdatedAgent = agent.set("agent_status", fromJS(action.status));
  if (status === "verified") {
    updatedAgent = statusUpdatedAgent
      .setIn(["agent_info", 0, "approval_documents"], fromJS(action.accepted))
      .setIn(["agent_info", 0, "reason"], fromJS(action.reason));
  } else {
    updatedAgent = statusUpdatedAgent.setIn(
      ["agent_info", 0, "reason"],
      fromJS(action.reason)
    );
  }
  console.log("updateAgentStatus", updatedAgent.toJS()); 
  // above log gives me the whole object of agent by updating the agent_info object with 
  // document_name and reason which is inside of agent_info block and agent_status which is
  // outside the agent_info block.
  yield fork(
    XcelTrip.put(
      `api/agent/applicant/status/${agentId}/?status=${status}`,
      agentStatusUpdated,
      agentStatusUpdatingError,
      updatedAgent.toJS(),
      agentId,
      status
    )
  );
}

Do I need to pass the whole object or just the document_name and reason if it's a verified status or just the reason if it's on hold and rejected status? If it's just document_name and reason, how can I send it considering all those statuses because in the case of verified additional document_name is needed?

1 Answer 1

0

The solution depends on whether you are going to use optimistic updating though even in this case it is possible to make one universal decision. Ideally you shan't change a state in the saga, instead the saga shall catch actions on which it is necessary to organize asynchronous requests, and following the results to generate new actions which will already go to reducer. For example, react component generates UPDATE_AGENT_REQUEST action, saga catches it and perform async API call, which generates UPDATE_AGENT_SUCCESS or UPDATE_AGENT_FAILURE actions depends on result.

If you wish optimistic updating, then shall initiate all the same the action identical to the present, but in case of information on a failure in the future, it is necessary to generate the compensating actions which turning current state in the previous state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.