2020-03-05 19:54:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { camelCase, mapKeys } from 'lodash';
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ACTION_TYPES as types } from './action-types';
|
2020-06-10 18:21:34 +00:00
|
|
|
import { defaultCartState } from '../default-states';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-02-28 02:05:10 +00:00
|
|
|
/**
|
|
|
|
* Sub-reducer for cart items array.
|
|
|
|
*
|
|
|
|
* @param {Array} state cartData.items state slice.
|
|
|
|
* @param {Object} action Action object.
|
|
|
|
*/
|
|
|
|
const cartItemsReducer = ( state = [], action ) => {
|
|
|
|
switch ( action.type ) {
|
2020-03-03 01:08:19 +00:00
|
|
|
case types.RECEIVE_CART_ITEM:
|
|
|
|
// Replace specified cart element with the new data from server.
|
|
|
|
return state.map( ( cartItem ) => {
|
|
|
|
if ( cartItem.key === action.cartItem.key ) {
|
|
|
|
return action.cartItem;
|
|
|
|
}
|
|
|
|
return cartItem;
|
|
|
|
} );
|
2020-02-28 02:05:10 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Reducer for receiving items related to the cart.
|
|
|
|
*
|
|
|
|
* @param {Object} state The current state in the store.
|
|
|
|
* @param {Object} action Action object.
|
|
|
|
*
|
|
|
|
* @return {Object} New or existing state.
|
|
|
|
*/
|
2020-06-10 18:21:34 +00:00
|
|
|
const reducer = ( state = defaultCartState, action ) => {
|
2020-02-25 11:36:53 +00:00
|
|
|
switch ( action.type ) {
|
|
|
|
case types.RECEIVE_ERROR:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
errors: state.errors.concat( action.error ),
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.REPLACE_ERRORS:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
errors: [ action.error ],
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.RECEIVE_CART:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
errors: [],
|
2020-03-05 19:54:05 +00:00
|
|
|
cartData: mapKeys( action.response, ( _, key ) =>
|
|
|
|
camelCase( key )
|
|
|
|
),
|
2020-02-25 11:36:53 +00:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.APPLYING_COUPON:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
applyingCoupon: action.couponCode,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.REMOVING_COUPON:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
removingCoupon: action.couponCode,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
2020-02-28 02:05:10 +00:00
|
|
|
|
2020-04-07 11:03:22 +00:00
|
|
|
case types.ITEM_PENDING_QUANTITY:
|
2020-02-28 02:05:10 +00:00
|
|
|
// Remove key by default - handles isQuantityPending==false
|
|
|
|
// and prevents duplicates when isQuantityPending===true.
|
2020-04-07 11:03:22 +00:00
|
|
|
const keysPendingQuantity = state.cartItemsPendingQuantity.filter(
|
2020-02-28 02:05:10 +00:00
|
|
|
( key ) => key !== action.cartItemKey
|
|
|
|
);
|
2020-04-07 11:03:22 +00:00
|
|
|
if ( action.isPendingQuantity ) {
|
|
|
|
keysPendingQuantity.push( action.cartItemKey );
|
2020-02-28 02:05:10 +00:00
|
|
|
}
|
|
|
|
state = {
|
|
|
|
...state,
|
2020-04-07 11:03:22 +00:00
|
|
|
cartItemsPendingQuantity: keysPendingQuantity,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.RECEIVE_REMOVED_ITEM:
|
|
|
|
const keysPendingDelete = state.cartItemsPendingDelete.filter(
|
|
|
|
( key ) => key !== action.cartItemKey
|
|
|
|
);
|
|
|
|
if ( action.isPendingDelete ) {
|
|
|
|
keysPendingDelete.push( action.cartItemKey );
|
|
|
|
}
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
cartItemsPendingDelete: keysPendingDelete,
|
2020-02-28 02:05:10 +00:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
// Delegate to cartItemsReducer.
|
2020-03-03 01:08:19 +00:00
|
|
|
case types.RECEIVE_CART_ITEM:
|
2020-02-28 02:05:10 +00:00
|
|
|
state = {
|
|
|
|
...state,
|
2020-03-17 11:45:33 +00:00
|
|
|
errors: [],
|
2020-02-28 02:05:10 +00:00
|
|
|
cartData: {
|
|
|
|
...state.cartData,
|
|
|
|
items: cartItemsReducer( state.cartData.items, action ),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
2020-11-20 15:13:35 +00:00
|
|
|
case types.UPDATING_CUSTOMER_DATA:
|
2020-03-05 19:54:05 +00:00
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
2020-11-20 15:13:35 +00:00
|
|
|
updatingCustomerData: action.isResolving,
|
2020-03-05 19:54:05 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
2020-03-31 15:40:27 +00:00
|
|
|
case types.UPDATING_SELECTED_SHIPPING_RATE:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
updatingSelectedRate: action.isResolving,
|
|
|
|
},
|
|
|
|
};
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default reducer;
|