2021-02-24 01:36:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-03-04 17:43:45 +00:00
|
|
|
import type {
|
|
|
|
Cart,
|
|
|
|
CartTotals,
|
|
|
|
CartMeta,
|
|
|
|
CartItem,
|
|
|
|
CartShippingRate,
|
|
|
|
} from '@woocommerce/types';
|
2022-02-22 17:45:01 +00:00
|
|
|
import { BillingAddress, ShippingAddress } from '@woocommerce/settings';
|
2021-02-24 01:36:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-07-08 05:53:24 +00:00
|
|
|
import { CartState, defaultCartState } from './default-state';
|
2021-08-13 13:28:41 +00:00
|
|
|
import type { ResponseError } from '../types';
|
2021-02-24 01:36:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves cart data from state.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {Cart} The data to return.
|
|
|
|
*/
|
|
|
|
export const getCartData = ( state: CartState ): Cart => {
|
|
|
|
return state.cartData;
|
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
export const getCustomerData = (
|
|
|
|
state: CartState
|
|
|
|
): {
|
|
|
|
shippingAddress: ShippingAddress;
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress: BillingAddress;
|
2022-02-22 17:45:01 +00:00
|
|
|
} => {
|
|
|
|
return {
|
|
|
|
shippingAddress: state.cartData.shippingAddress,
|
2022-06-10 16:59:25 +00:00
|
|
|
billingAddress: state.cartData.billingAddress,
|
2022-02-22 17:45:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-04 17:43:45 +00:00
|
|
|
/**
|
|
|
|
* Retrieves shipping rates from state.
|
|
|
|
*
|
|
|
|
* @param { CartState } state The current state.
|
|
|
|
* @return { CartShippingRate[] } The shipping rates on the cart.
|
|
|
|
*/
|
|
|
|
export const getShippingRates = ( state: CartState ): CartShippingRate[] => {
|
|
|
|
return state.cartData.shippingRates;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves whether the cart needs shipping.
|
|
|
|
*
|
|
|
|
* @param { CartState } state The current state.
|
|
|
|
* @return { boolean } True if the cart needs shipping.
|
|
|
|
*/
|
|
|
|
export const getNeedsShipping = ( state: CartState ): boolean => {
|
|
|
|
return state.cartData.needsShipping;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves whether the cart shipping has been calculated.
|
|
|
|
*
|
|
|
|
* @param { CartState } state The current state.
|
|
|
|
* @return { boolean } True if the shipping has been calculated.
|
|
|
|
*/
|
|
|
|
export const getHasCalculatedShipping = ( state: CartState ): boolean => {
|
|
|
|
return state.cartData.hasCalculatedShipping;
|
|
|
|
};
|
|
|
|
|
2021-02-24 01:36:24 +00:00
|
|
|
/**
|
|
|
|
* Retrieves cart totals from state.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {CartTotals} The data to return.
|
|
|
|
*/
|
|
|
|
export const getCartTotals = ( state: CartState ): CartTotals => {
|
|
|
|
return state.cartData.totals || defaultCartState.cartData.totals;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves cart meta from state.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {CartMeta} The data to return.
|
|
|
|
*/
|
|
|
|
export const getCartMeta = ( state: CartState ): CartMeta => {
|
|
|
|
return state.metaData || defaultCartState.metaData;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves cart errors from state.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {Array<ResponseError>} Array of errors.
|
|
|
|
*/
|
|
|
|
export const getCartErrors = ( state: CartState ): Array< ResponseError > => {
|
2021-08-13 13:28:41 +00:00
|
|
|
return state.errors;
|
2021-02-24 01:36:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any coupon is being applied.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {boolean} True if a coupon is being applied.
|
|
|
|
*/
|
|
|
|
export const isApplyingCoupon = ( state: CartState ): boolean => {
|
|
|
|
return !! state.metaData.applyingCoupon;
|
|
|
|
};
|
|
|
|
|
2021-03-24 13:28:11 +00:00
|
|
|
/**
|
|
|
|
* Returns true if cart is stale, false if it is not.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {boolean} True if the cart data is stale.
|
|
|
|
*/
|
|
|
|
export const isCartDataStale = ( state: CartState ): boolean => {
|
|
|
|
return state.metaData.isCartDataStale;
|
|
|
|
};
|
|
|
|
|
2021-02-24 01:36:24 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the coupon code currently being applied.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {string} The data to return.
|
|
|
|
*/
|
|
|
|
export const getCouponBeingApplied = ( state: CartState ): string => {
|
|
|
|
return state.metaData.applyingCoupon || '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if any coupon is being removed.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {boolean} True if a coupon is being removed.
|
|
|
|
*/
|
|
|
|
export const isRemovingCoupon = ( state: CartState ): boolean => {
|
|
|
|
return !! state.metaData.removingCoupon;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the coupon code currently being removed.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {string} The data to return.
|
|
|
|
*/
|
|
|
|
export const getCouponBeingRemoved = ( state: CartState ): string => {
|
|
|
|
return state.metaData.removingCoupon || '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns cart item matching specified key.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @param {string} cartItemKey Key for a cart item.
|
2021-02-24 01:36:24 +00:00
|
|
|
* @return {CartItem | void} Cart item object, or undefined if not found.
|
|
|
|
*/
|
|
|
|
export const getCartItem = (
|
|
|
|
state: CartState,
|
|
|
|
cartItemKey: string
|
|
|
|
): CartItem | void => {
|
|
|
|
return state.cartData.items.find(
|
|
|
|
( cartItem ) => cartItem.key === cartItemKey
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified cart item quantity is being updated.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @param {string} cartItemKey Key for a cart item.
|
2021-02-24 01:36:24 +00:00
|
|
|
* @return {boolean} True if a item has a pending request to be updated.
|
|
|
|
*/
|
|
|
|
export const isItemPendingQuantity = (
|
|
|
|
state: CartState,
|
|
|
|
cartItemKey: string
|
|
|
|
): boolean => {
|
|
|
|
return state.cartItemsPendingQuantity.includes( cartItemKey );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified cart item quantity is being updated.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @param {string} cartItemKey Key for a cart item.
|
2021-02-24 01:36:24 +00:00
|
|
|
* @return {boolean} True if a item has a pending request to be updated.
|
|
|
|
*/
|
|
|
|
export const isItemPendingDelete = (
|
|
|
|
state: CartState,
|
|
|
|
cartItemKey: string
|
|
|
|
): boolean => {
|
|
|
|
return state.cartItemsPendingDelete.includes( cartItemKey );
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Retrieves if the address is being applied for shipping.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
* @return {boolean} are shipping rates loading.
|
|
|
|
*/
|
|
|
|
export const isCustomerDataUpdating = ( state: CartState ): boolean => {
|
|
|
|
return !! state.metaData.updatingCustomerData;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves if the shipping rate selection is being persisted.
|
|
|
|
*
|
|
|
|
* @param {CartState} state The current state.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if the shipping rate selection is being persisted to
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
export const isShippingRateBeingSelected = ( state: CartState ): boolean => {
|
|
|
|
return !! state.metaData.updatingSelectedRate;
|
|
|
|
};
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the item keys for items whose quantity is currently being updated.
|
|
|
|
*/
|
|
|
|
export const getItemsPendingQuantityUpdate = ( state: CartState ): string[] => {
|
|
|
|
return state.cartItemsPendingQuantity;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Retrieves the item keys for items that are currently being deleted.
|
|
|
|
*/
|
|
|
|
export const getItemsPendingDelete = ( state: CartState ): string[] => {
|
|
|
|
return state.cartItemsPendingDelete;
|
|
|
|
};
|