woocommerce/plugins/woocommerce-blocks/assets/js/data/cart/reducers.js

125 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* Internal dependencies
*/
import { ACTION_TYPES as types } from './action-types';
/**
* 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 ) {
Support updating quantity of cart items & sold_individually product option (https://github.com/woocommerce/woocommerce-blocks/pull/1824) * first cut - removing an item from cart: - add actions to cart store for removing an item and keeping track of pending removal API call - add reducer logic for storing pending state on an item, and removing an item - expose removeCartItem on new useStoreCartItems hook - hook it up to remove link / trashcan icon in row item * disable cart quantity picker/remove link while API request in progress: - expose cart item pending status from store using selector - use selector to disable quantity related components in line item row * add typedef for cart items store object provided by hook * allow user to change quantity of cart items (first cut): - add action for replacing a cart item in the store - add generator action for changing quantity - expose change quantity action on useStoreCartItems hook - hook up to quantity UI in cart block (work in progress) * post-rebase fixes & fix broken typedef: - rework cart item change quantity callback - now supplies item key like remove callback - fix hook StoreCartItem return value typedef - single item with specified key, was array of all items - add quantity JSDoc for changeCartItemQuantity action - remove changeQuantity callback from UI (currently infinite looping) * fix bug in recieveCartItem reducer - check keys for equality: - was key === object * fix invalid url in POST cart/items/quantity request * hook up cart line item quantity to API: - remove internal state/ref for QuantitySelector, is now a controlled component - call changeQuantity action from QuantitySelector change callback * QuantitySelector no longer needs a ref to wrangle number input value * hoist quantity state out of QuantitySelector into story (fix storybook) * add product sold_individually option to cart item API response * limit sold_individually items to 1 per cart/order: - support optional max value in QuantitySelector - set maximum dependent on sold_individually API field * prevent user from requesting zero x cart item (API 500 errors): - add minimum limit to QuantitySelector - default limit to 1 + fix bug with limiting to maximum value in number input change handler * remove useStoreCartItems, zombie hook coming back from rebase 🧟‍♂️ * address various review feedback: - inline undefined check, don't use lodash - quantityInputOnKeyDown callback hook depends on canIncrease/canDecrease - also removed undefined check for minimum, as minimum has default 0 * use safer typeof check for presence of maximum prop
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;
} );
case types.RECEIVE_REMOVED_ITEM:
return state.filter( ( cartItem ) => {
return cartItem.key !== action.cartItemKey;
} );
}
return state;
};
/**
* 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 = {
cartItemsQuantityPending: [],
cartData: {
coupons: [],
items: [],
itemsCount: 0,
itemsWeight: 0,
needsShipping: true,
totals: {},
},
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: [],
cartData: action.response,
};
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;
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.
Support updating quantity of cart items & sold_individually product option (https://github.com/woocommerce/woocommerce-blocks/pull/1824) * first cut - removing an item from cart: - add actions to cart store for removing an item and keeping track of pending removal API call - add reducer logic for storing pending state on an item, and removing an item - expose removeCartItem on new useStoreCartItems hook - hook it up to remove link / trashcan icon in row item * disable cart quantity picker/remove link while API request in progress: - expose cart item pending status from store using selector - use selector to disable quantity related components in line item row * add typedef for cart items store object provided by hook * allow user to change quantity of cart items (first cut): - add action for replacing a cart item in the store - add generator action for changing quantity - expose change quantity action on useStoreCartItems hook - hook up to quantity UI in cart block (work in progress) * post-rebase fixes & fix broken typedef: - rework cart item change quantity callback - now supplies item key like remove callback - fix hook StoreCartItem return value typedef - single item with specified key, was array of all items - add quantity JSDoc for changeCartItemQuantity action - remove changeQuantity callback from UI (currently infinite looping) * fix bug in recieveCartItem reducer - check keys for equality: - was key === object * fix invalid url in POST cart/items/quantity request * hook up cart line item quantity to API: - remove internal state/ref for QuantitySelector, is now a controlled component - call changeQuantity action from QuantitySelector change callback * QuantitySelector no longer needs a ref to wrangle number input value * hoist quantity state out of QuantitySelector into story (fix storybook) * add product sold_individually option to cart item API response * limit sold_individually items to 1 per cart/order: - support optional max value in QuantitySelector - set maximum dependent on sold_individually API field * prevent user from requesting zero x cart item (API 500 errors): - add minimum limit to QuantitySelector - default limit to 1 + fix bug with limiting to maximum value in number input change handler * remove useStoreCartItems, zombie hook coming back from rebase 🧟‍♂️ * address various review feedback: - inline undefined check, don't use lodash - quantityInputOnKeyDown callback hook depends on canIncrease/canDecrease - also removed undefined check for minimum, as minimum has default 0 * use safer typeof check for presence of maximum prop
2020-03-03 01:08:19 +00:00
case types.RECEIVE_CART_ITEM:
case types.RECEIVE_REMOVED_ITEM:
state = {
...state,
cartData: {
...state.cartData,
items: cartItemsReducer( state.cartData.items, action ),
},
};
break;
}
return state;
};
export default reducer;