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-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
|
|
|
case types.RECEIVE_REMOVED_ITEM:
|
|
|
|
return state.filter( ( cartItem ) => {
|
|
|
|
return cartItem.key !== action.cartItemKey;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const reducer = (
|
|
|
|
state = {
|
2020-02-28 02:05:10 +00:00
|
|
|
cartItemsQuantityPending: [],
|
2020-02-25 11:36:53 +00:00
|
|
|
cartData: {
|
|
|
|
coupons: [],
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates: [],
|
2020-03-13 19:04:03 +00:00
|
|
|
shippingAddress: {
|
|
|
|
first_name: '',
|
|
|
|
last_name: '',
|
|
|
|
company: '',
|
|
|
|
address_1: '',
|
|
|
|
address_2: '',
|
|
|
|
city: '',
|
|
|
|
state: '',
|
|
|
|
postcode: '',
|
|
|
|
country: '',
|
|
|
|
},
|
2020-02-25 11:36:53 +00:00
|
|
|
items: [],
|
|
|
|
itemsCount: 0,
|
|
|
|
itemsWeight: 0,
|
|
|
|
needsShipping: true,
|
2020-03-09 12:52:25 +00:00
|
|
|
totals: {
|
|
|
|
currency_code: '',
|
|
|
|
currency_symbol: '',
|
|
|
|
currency_minor_unit: 2,
|
|
|
|
currency_decimal_separator: '.',
|
|
|
|
currency_thousand_separator: ',',
|
|
|
|
currency_prefix: '',
|
|
|
|
currency_suffix: '',
|
2020-03-12 09:41:35 +00:00
|
|
|
total_items: '0',
|
|
|
|
total_items_tax: '0',
|
|
|
|
total_fees: '0',
|
|
|
|
total_fees_tax: '0',
|
|
|
|
total_discount: '0',
|
|
|
|
total_discount_tax: '0',
|
|
|
|
total_shipping: '0',
|
|
|
|
total_shipping_tax: '0',
|
|
|
|
total_price: '0',
|
|
|
|
total_tax: '0',
|
2020-03-09 12:52:25 +00:00
|
|
|
tax_lines: [],
|
|
|
|
},
|
2020-02-25 11:36:53 +00:00
|
|
|
},
|
|
|
|
metaData: {},
|
|
|
|
errors: [],
|
|
|
|
},
|
|
|
|
action
|
|
|
|
) => {
|
|
|
|
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
|
|
|
|
|
|
|
case types.ITEM_QUANTITY_PENDING:
|
|
|
|
// Remove key by default - handles isQuantityPending==false
|
|
|
|
// and prevents duplicates when isQuantityPending===true.
|
|
|
|
const newPendingKeys = state.cartItemsQuantityPending.filter(
|
|
|
|
( key ) => key !== action.cartItemKey
|
|
|
|
);
|
|
|
|
if ( action.isQuantityPending ) {
|
|
|
|
newPendingKeys.push( action.cartItemKey );
|
|
|
|
}
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
cartItemsQuantityPending: newPendingKeys,
|
|
|
|
};
|
|
|
|
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
|
|
|
case types.RECEIVE_REMOVED_ITEM:
|
|
|
|
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-03-05 19:54:05 +00:00
|
|
|
case types.UPDATING_SHIPPING_ADDRESS:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
updatingShipping: action.isResolving,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
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;
|