简体   繁体   中英

redux - reducer — change property of an object within an array

i need change property of an object within an array that is part of another object in me reducer, I'm going to show you that I tried and I was unsuccessful but the truth is that I don't know if it is possible to change that property

function

 function handleQuantity(e) {
    if (e.target.name === "resta" && quantityProduct > 0) {
      setQuantityProduct(quantityProduct - 1);
    }
    if (e.target.name === "suma" && quantityProduct < props.stock) {
      setQuantityProduct(quantityProduct + 1);
    }
    const actionquantity = {
      orderlineId: props.id,
      orderlineQuantity: quantityProduct,
    };
    props.changequantity(actionquantity);
  }

executor

  function handleChangeQuantity(actionquantity){
      dispatch(changeQuantityProductLine(actionquantity , id))
    }

action

export const changeQuantityProductLine = (actionquantity, userId) => async(dispatch) =>{
    try{
        dispatch({type: CHANGE_QUANTITY_PRODUCT_LINE})

        const {data} = await axios.put(`${url}users/${userId}/cart`, actionquantity);
        console.log('-----------------------------------------------------------------------------------------------------------------------------------',actionquantity)
        dispatch({
            type: CHANGE_QUANTITY_PRODUCT_LINE_SUCCESS,
            payload: actionquantity
        })
    }catch(error){
        dispatch({
            type: CHANGE_QUANTITY_PRODUCT_LINE_ERROR,
            payload:
            error.response && error.response.data.message
            ? error.response.data.message
            : error.message,
        })
    }
}

reducer


export const orderlineByIdReducer = (
  state = {
    //cart: []
    order: {},
    error: null,
    loading: false,
  }, 
  action
) => {
  switch (action.type) {
    case ORDER_LINE_BY_ID:
      return { ...state, loading: true};
    case ORDER_LINE_BY_ID_SUCCESS:
      return {...state, loading: false, order: action.payload };
    case ORDER_LINE_BY_ID_ERROR:
      return {...state, loading: false, error: action.payload };
    case REMOVE_ORDER_LINE_BY_ID:
      return { ...state, loading: true };
    case REMOVE_ORDER_LINE_SUCCESS:
      return {
        ...state,
        order: {...state.order, orderlines: state.order.orderlines.filter(
          (orderline) => orderline.product.id != action.payload
        ), }
      };
    case REMOVE_ORDER_LINE_ERROR:
      return {...state, loading: false, error: action.payload };
      case CHANGE_QUANTITY_PRODUCT_LINE:
        return { ...state, loading: true };
      case CHANGE_QUANTITY_PRODUCT_LINE_SUCCESS:
        return {
          ...state,
           order: {...state.order, orderlines: state.order.orderlines.map(
             orderline => orderline.id === action.payload.actionquantity.orderlineId
              ? Object.assign({}, orderline, {quantity: action.payload.actionquantity.orderlineQuantity})
              : orderline) }
          }
      case CHANGE_QUANTITY_PRODUCT_LINE_ERROR:
        return {...state, loading: false, error: action.payload };

    default:
      return state;
  }
};

With that attempt the action breaks, but taking out what I put in the reducer works fine... I suspect that I am not managing to change the property of that orderline

It doesn't look like your payload has actionquantity as a property. The payload is the actionquantity. So, action.payload.actionquantity.orderlineId should be action.payload.orderlineId .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM