通过api添加新数据时如何更新存储状态

时间:2019-07-19 10:47:01

标签: react-native redux redux-thunk

我是React-native和Redux的新手,我正在尝试执行CRUD操作。通过api添加数据时如何更新存储状态。我正在致电Get Business Api,可能要采取行动。然后将其存储到商店中。

商业行为:

export const getBusinessByAliases = (aliases) => {
  return (dispatch) => {
      dispatch(getBusinessByAliasesData(aliases))
    //API CALL
      getBusiness(aliases)
          .then(response=>{
        //HERE I GET WHOLE BUSINESS DATA
              dispatch(getBusinessByAliasesSuccess(response.data.business))
          })
          .catch(err => {
              console.log("err",err)
              dispatch(getBusinessByAliasesFailure(err))
          })
  }
}

业务数据为:

 business:[
        "id": "17bfdde3-bc04-4a9c-87e7-7530ded1b929",
            "inserted_at": "2019-07-10T09:47:41",
            "name": "Business2",
     "employees": [
                {
                    "nickname": "xyz",
                    "settings": null,
                    "user": {
                        "email": null,
                        "first_name": null,
                        "id": "582f5d07-146e-4a81-a6c0-7dd5208b43b2",
                        "image_id": null,
                        "inserted_at": "2019-07-02T13:41:06",
                        "last_name": null,
                        "phone": "+911234567890"
                    }
                }
            ],
"services": [
                {
                    "id": "34bd8c80-41e1-459a-bc09-d88a6894bd42",
                    "name": "Gsgsha",
                    "settings": {
                        "color": "#039BE5",
                        "duration": 4,
                        "price": 6
                    }
                }
            ],
]

现在,我通过在另一个操作中调用create employee api添加客户 员工行动:

export const createNewEmployee = (businessName,data) => {
  //console.log("whole data", data)
  return (dispatch) => {
      dispatch(createEmployee(businessName,data))
    // API CALL
      createEmployees(businessName,data)
          .then(response=>{
            //IN RESPONSE I GET SUCCESS MESSAGE WITH TRANSACTION ID
              dispatch(createEmployeeSuccess(response.data.transaction_id))
          })
          .catch(err => {
              console.log("errEmp",err)
              dispatch(createEmployeeFailure(err))
            })
  }
}

现在,如何根据包含所有数据以及新添加的员工条目的业务状态进行更新?

1 个答案:

答案 0 :(得分:0)

我认为最好的解决方案是您的端点应与所有雇员或仅由新雇员列表来响应新业务对象。

请记住,许多应用程序客户端都可以修改您的服务器,如果需要保持信息更新,则需要实施Firebase协议来每次更新商店。