2020-03-05 19:54:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
import type { CartItem } from '@woocommerce/types';
|
2021-03-01 17:47:22 +00:00
|
|
|
import type { Reducer } from 'redux';
|
2020-03-05 19:54:05 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ACTION_TYPES as types } from './action-types';
|
2021-02-24 01:36:24 +00:00
|
|
|
import { defaultCartState, CartState } from '../default-states';
|
2021-08-13 13:28:41 +00:00
|
|
|
import { EMPTY_CART_ERRORS } from '../constants';
|
2021-02-24 01:36:24 +00:00
|
|
|
import type { CartAction } from './actions';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-02-28 02:05:10 +00:00
|
|
|
/**
|
|
|
|
* Sub-reducer for cart items array.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Array<CartItem>} state cartData.items state slice.
|
|
|
|
* @param {CartAction} action Action object.
|
2020-02-28 02:05:10 +00:00
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
const cartItemsReducer = (
|
|
|
|
state: Array< CartItem > = [],
|
2021-03-01 17:47:22 +00:00
|
|
|
action: Partial< CartAction >
|
2021-02-24 01:36:24 +00:00
|
|
|
) => {
|
2020-02-28 02:05:10 +00:00
|
|
|
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 ) => {
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( cartItem.key === action.cartItem?.key ) {
|
2020-03-03 01:08:19 +00:00
|
|
|
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.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {CartState} state The current state in the store.
|
|
|
|
* @param {CartAction} action Action object.
|
2020-02-25 11:36:53 +00:00
|
|
|
*
|
2021-02-24 01:36:24 +00:00
|
|
|
* @return {CartState} New or existing state.
|
2020-02-25 11:36:53 +00:00
|
|
|
*/
|
2021-03-01 17:47:22 +00:00
|
|
|
const reducer: Reducer< CartState > = (
|
|
|
|
state = defaultCartState,
|
|
|
|
action: Partial< CartAction >
|
|
|
|
) => {
|
2020-02-25 11:36:53 +00:00
|
|
|
switch ( action.type ) {
|
|
|
|
case types.RECEIVE_ERROR:
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.error ) {
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
errors: state.errors.concat( action.error ),
|
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
break;
|
|
|
|
case types.REPLACE_ERRORS:
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.error ) {
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
errors: [ action.error ],
|
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
break;
|
|
|
|
case types.RECEIVE_CART:
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.response ) {
|
|
|
|
state = {
|
|
|
|
...state,
|
2021-08-13 13:28:41 +00:00
|
|
|
errors: EMPTY_CART_ERRORS,
|
2022-02-22 17:45:01 +00:00
|
|
|
cartData: {
|
|
|
|
...state.cartData,
|
|
|
|
...action.response,
|
|
|
|
},
|
2021-02-24 01:36:24 +00:00
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
break;
|
|
|
|
case types.APPLYING_COUPON:
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.couponCode || action.couponCode === '' ) {
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
applyingCoupon: action.couponCode,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
break;
|
2022-06-10 16:59:25 +00:00
|
|
|
case types.SET_BILLING_ADDRESS:
|
2022-02-22 17:45:01 +00:00
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
cartData: {
|
|
|
|
...state.cartData,
|
|
|
|
billingAddress: {
|
|
|
|
...state.cartData.billingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
...action.billingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case types.SET_SHIPPING_ADDRESS:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
cartData: {
|
|
|
|
...state.cartData,
|
|
|
|
shippingAddress: {
|
|
|
|
...state.cartData.shippingAddress,
|
|
|
|
...action.shippingAddress,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
case types.REMOVING_COUPON:
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.couponCode || action.couponCode === '' ) {
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
removingCoupon: action.couponCode,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
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
|
|
|
|
);
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.isPendingQuantity && action.cartItemKey ) {
|
2020-04-07 11:03:22 +00:00
|
|
|
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
|
|
|
|
);
|
2021-02-24 01:36:24 +00:00
|
|
|
if ( action.isPendingDelete && action.cartItemKey ) {
|
2020-04-07 11:03:22 +00:00
|
|
|
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,
|
2021-08-13 13:28:41 +00:00
|
|
|
errors: EMPTY_CART_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,
|
2021-02-24 01:36:24 +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,
|
2021-02-24 01:36:24 +00:00
|
|
|
updatingSelectedRate: !! action.isResolving,
|
2020-03-31 15:40:27 +00:00
|
|
|
},
|
|
|
|
};
|
2021-03-24 13:28:11 +00:00
|
|
|
break;
|
|
|
|
case types.SET_IS_CART_DATA_STALE:
|
|
|
|
state = {
|
|
|
|
...state,
|
|
|
|
metaData: {
|
|
|
|
...state.metaData,
|
|
|
|
isCartDataStale: action.isCartDataStale,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2021-03-01 17:47:22 +00:00
|
|
|
export type State = ReturnType< typeof reducer >;
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
export default reducer;
|