woocommerce/plugins/woocommerce-blocks/assets/js/data/cart/selectors.ts

156 lines
4.2 KiB
TypeScript
Raw Normal View History

Add TypeScript support and convert cart data store to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/3768) * add typescript support * Add type declarations for Cart and CartResponse interfaces * make sure we’re resolving .ts files as well as .js files on imports * add more types * type the cart data store * Apply suggestions from code review (implement .tsx in configs) Co-authored-by: Jon Surrell <jon.surrell@automattic.com> * remove global fetchMock declaration and directly import where used. * rename type * remove named action types and just infer by returning action creator values as const * use interface instead of type * rename * renames * create CartAction type as union of action creator returned types and implement in reducer * remove unused imports * refresh package-lock after rebase * Add base TS config that projects will inherit from * Add tsconfig for assets/js/data project * Ignore TS error on cart store registration We will address this in cooldown when we have time to investigate further * Add tsc to build step to catch TypeScript errors * add a separate command for tsc and tweak build command to use * restore checkJs and allowJs values in config and remove ts check from build command * Add ts:check-all command * Add TypeScript checking workflows * Change triggers for TypeScript workflow * Use npm ci instead of npm install * Remove ts:check-all from TypeScript workflow * Remove TS Check GitHub workflow * Remove type-defs dir from TS include, and remove ts:check-all script We no longer need the ts:check-all script because ts:check will do this for us, the old ts:check did nothing and did not work. * fix coupon loading issues * include .ts files only from type-defs folder Co-authored-by: Jon Surrell <jon.surrell@automattic.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2021-02-24 01:36:24 +00:00
/**
* External dependencies
*/
import type { Cart, CartTotals, CartMeta, CartItem } from '@woocommerce/types';
/**
* Internal dependencies
*/
import { CartState, defaultCartState } from '../default-states';
import { ResponseError } from '../types';
/**
* 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;
};
/**
* 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 > => {
return state.errors || [];
};
/**
* 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;
};
/**
* 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.
*
* @param {CartState} state The current state.
* @param {string} cartItemKey Key for a cart item.
* @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.
*
* @param {CartState} state The current state.
* @param {string} cartItemKey Key for a cart item.
* @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.
*
* @param {CartState} state The current state.
* @param {string} cartItemKey Key for a cart item.
* @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;
};