【问题标题】:update the status of agent in redux saga更新 redux saga 中代理的状态
【发布时间】:2017-11-28 08:16:58
【问题描述】:

我需要更新代理的状态。如果状态已验证,API 需要 document_name(multiple) 和原因。如果状态为暂停并被拒绝,则 API 需要唯一的原因。

这是代理的对象。

{
    "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": "k@mail.com",
    "_id": "594f4665ae36b70029f80ba0",
    "first_name": "pm",
    "email": "k@mail.com",
}

在提交状态时,我为一个传奇触发了以下操作

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

satus 是代理的状态(已验证、暂停或拒绝)代理是 接受代理的详细信息是状态为的文件 验证的原因是状态的原因

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
    )
  );
}

如果它是已验证状态,我是否需要传递整个对象或仅传递 document_name 和原因,或者如果它处于暂停和拒绝状态,我是否需要传递原因?如果只是 document_name 和原因,我该如何考虑所有这些状态来发送它,因为在经过验证的情况下需要额外的 document_name?

【问题讨论】:

    标签: javascript reactjs redux immutable.js redux-saga


    【解决方案1】:

    解决方案取决于您是否要使用乐观更新,即使在这种情况下也可以做出一个通用的决定。理想情况下,您不应更改 saga 中的状态,而是 saga 应捕获有必要组织异步请求的操作,并根据结果生成新的操作,这些操作将已发送到 reducer。 例如,react 组件生成UPDATE_AGENT_REQUEST action,saga 捕获它并执行异步API 调用,根据结果生成UPDATE_AGENT_SUCCESSUPDATE_AGENT_FAILURE action。

    如果您希望乐观更新,则应启动与当前相同的所有动作,但如果将来出现故障信息,则需要生成将当前状态转换为先前状态的补偿动作.

    【讨论】:

      最近更新 更多