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
|
|
|
|
*/
|
|
|
|
import { CartState, defaultCartState } from '../default-states';
|
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;
|
|
|
|
billingData: BillingAddress;
|
|
|
|
} => {
|
|
|
|
return {
|
|
|
|
shippingAddress: state.cartData.shippingAddress,
|
|
|
|
billingData: state.cartData.billingAddress,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|