2018-01-22

1

创建部分

const createProductionOrder = (data, storeId) => (dispatch) => {
  dispatch({ type: actionTypes.SUBMIT_PRODUCTION_ORDER_OVERVIEW_REQUEST });

  dispatch(startFetch());
  // e.g data.productionOrderItemsAttributes[0].listingId = 358708
   let listingIds = [];
   let atts = data.productionOrderItemsAttributes;
   for (let i = 0; i < atts.length; i++) {
     listingIds.push(atts[i].listingId)
   }
  dispatch(getListingBillOfMaterialCollection(listingIds, storeId))
    .then(resp => {
      dispatch(stopFetch());
      const billOfMaterialsData = resp.data;
      data.productionOrderItemsAttributes.map((obj) =>{
        let index = obj.listingId.toString()
        obj.productionOrderItemMaterialsAttributes = billOfMaterialsData[index]
      })

      return dispatch(create(data, storeId)).then(resp => {
        const { data, error } = resp;
        if (!error) {
          let { id } = data;
          routeHelper.goProductionOrders(storeId, id);
        } else {
          alert(error.message);
          dispatch({ type: actionTypes.SUBMIT_PRODUCTION_ORDER_OVERVIEW_FAILURE, error });
        }
     })
  })
};

更新po部分

const updateProductionOrder = (data, id, storeId) => (dispatch) => {
  dispatch({ type: actionTypes.SUBMIT_PRODUCTION_ORDER_OVERVIEW_REQUEST });
  dispatch(startFetch());
   let listingIds = [];
   let atts = data.productionOrderItemsAttributes;
   for (let i = 0; i < atts.length; i++) {
     listingIds.push(atts[i].listingId)
   }
  dispatch(getListingBillOfMaterialCollection(listingIds, storeId))
    .then(resp => {
      dispatch(stopFetch());
      const billOfMaterialsData = resp.data;
      data.productionOrderItemsAttributes.map((obj) => {
        let index = obj.listingId.toString()
        obj.productionOrderItemMaterials = billOfMaterialsData[index]
      })
      return dispatch(update(data, id, storeId))
        .then(resp => {
          const { error, data } = resp;
          if (!error) {
            dispatch({ type: actionTypes.SUBMIT_PRODUCTION_ORDER_OVERVIEW_SUCCESS, data });
          } else {
            alert(error.message);
            dispatch({ type: actionTypes.SUBMIT_PRODUCTION_ORDER_OVERVIEW_FAILURE, error });
          }
        });
    })
  };

你可能感兴趣的:(2018-01-22)