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

97 lines
2.4 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { select } from '@wordpress/data';
Allow third party methods to appear in local pickup area (https://github.com/woocommerce/woocommerce-blocks/pull/8256) * Add get_collectible_method_ids function * Add collectibleMethodIds to asset data registry * Check whether method id is pickup_location/in collectibleMethodIds * Allow selectShippingRate to be called without a package id * Prevent collectible methods showing in the main shipping area * Remove unnecessary pluck and add pickup_location to returned array * No longer insert pickup_location in collectibleMethodIds * Allow third party methods to influence low/high collection price * Update useShippingData to consider any collectible method * Add hasSelectedLocalPickup to shipping types * Add dependency to selectShippingRate in useShippingData * Register collectibleMethodIds as a callback This is so the shipping methods get change to register before this is called. Passing a callback to `add` means it won't be called until just before it is output. * Update supports key to 'local_pickup' * Rename utils/shipping-rates to TS * Convert to TS, add isPackageRateCollectible & hasCollectableRate * Add tests for hasCollectableRate and isPackageRateCollectible * Update shipping controller to output only method names * Make PickupLocation shipping method support local_pickup * Set prefersCollection based on rate ID being collectible * Remove need to retrieve settings and use helper function instead * rename hasCollectableRate to hasCollectibleRate * Use array_reduce and update comments in get_local_pickup_method_ids * Switch order of array_unique and array_values * Remove unneeded dependency * Hyphenate local-pickup so it follows the same format as other features * Update use of collectible to collectable * Change supports feature to be hyphenated
2023-02-03 16:00:24 +00:00
import { hasCollectableRate } from '@woocommerce/base-utils';
import { isString, objectHasProp } from '@woocommerce/types';
Convert checkout context to data store - part 1 (https://github.com/woocommerce/woocommerce-blocks/pull/6232) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Delete empty types.ts file * remove merge conflict from docs * Correct linting in docs Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
2022-06-10 16:33:15 +00:00
/**
* Internal dependencies
*/
import { STATUS } from './constants';
import { CheckoutState } from './default-state';
import { STORE_KEY as cartStoreKey } from '../cart/constants';
Convert checkout context to data store - part 1 (https://github.com/woocommerce/woocommerce-blocks/pull/6232) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Delete empty types.ts file * remove merge conflict from docs * Correct linting in docs Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
2022-06-10 16:33:15 +00:00
export const getCustomerId = ( state: CheckoutState ) => {
return state.customerId;
};
export const getOrderId = ( state: CheckoutState ) => {
return state.orderId;
};
Convert checkout context to data store - part 1 (https://github.com/woocommerce/woocommerce-blocks/pull/6232) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Delete empty types.ts file * remove merge conflict from docs * Correct linting in docs Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
2022-06-10 16:33:15 +00:00
export const getOrderNotes = ( state: CheckoutState ) => {
return state.orderNotes;
};
export const getRedirectUrl = ( state: CheckoutState ) => {
return state.redirectUrl;
};
export const getUseShippingAsBilling = ( state: CheckoutState ) => {
return state.useShippingAsBilling;
};
export const getExtensionData = ( state: CheckoutState ) => {
return state.extensionData;
};
export const getShouldCreateAccount = ( state: CheckoutState ) => {
return state.shouldCreateAccount;
};
export const getCheckoutStatus = ( state: CheckoutState ) => {
return state.status;
};
Convert checkout context to data store - part 1 (https://github.com/woocommerce/woocommerce-blocks/pull/6232) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Delete empty types.ts file * remove merge conflict from docs * Correct linting in docs Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
2022-06-10 16:33:15 +00:00
export const hasError = ( state: CheckoutState ) => {
return state.hasError;
};
export const hasOrder = ( state: CheckoutState ) => {
return !! state.orderId;
};
export const isComplete = ( state: CheckoutState ) => {
return state.status === STATUS.COMPLETE;
};
export const isIdle = ( state: CheckoutState ) => {
return state.status === STATUS.IDLE;
};
export const isBeforeProcessing = ( state: CheckoutState ) => {
return state.status === STATUS.BEFORE_PROCESSING;
};
export const isAfterProcessing = ( state: CheckoutState ) => {
return state.status === STATUS.AFTER_PROCESSING;
};
export const isProcessing = ( state: CheckoutState ) => {
return state.status === STATUS.PROCESSING;
};
export const isCalculating = ( state: CheckoutState ) => {
return state.calculatingCount > 0;
};
2022-10-04 12:02:28 +00:00
export const prefersCollection = ( state: CheckoutState ) => {
if ( state.prefersCollection === undefined ) {
const shippingRates = select( cartStoreKey ).getShippingRates();
if ( ! shippingRates || ! shippingRates.length ) {
return false;
}
const selectedRate = shippingRates[ 0 ].shipping_rates.find(
( rate ) => rate.selected
);
Allow third party methods to appear in local pickup area (https://github.com/woocommerce/woocommerce-blocks/pull/8256) * Add get_collectible_method_ids function * Add collectibleMethodIds to asset data registry * Check whether method id is pickup_location/in collectibleMethodIds * Allow selectShippingRate to be called without a package id * Prevent collectible methods showing in the main shipping area * Remove unnecessary pluck and add pickup_location to returned array * No longer insert pickup_location in collectibleMethodIds * Allow third party methods to influence low/high collection price * Update useShippingData to consider any collectible method * Add hasSelectedLocalPickup to shipping types * Add dependency to selectShippingRate in useShippingData * Register collectibleMethodIds as a callback This is so the shipping methods get change to register before this is called. Passing a callback to `add` means it won't be called until just before it is output. * Update supports key to 'local_pickup' * Rename utils/shipping-rates to TS * Convert to TS, add isPackageRateCollectible & hasCollectableRate * Add tests for hasCollectableRate and isPackageRateCollectible * Update shipping controller to output only method names * Make PickupLocation shipping method support local_pickup * Set prefersCollection based on rate ID being collectible * Remove need to retrieve settings and use helper function instead * rename hasCollectableRate to hasCollectibleRate * Use array_reduce and update comments in get_local_pickup_method_ids * Switch order of array_unique and array_values * Remove unneeded dependency * Hyphenate local-pickup so it follows the same format as other features * Update use of collectible to collectable * Change supports feature to be hyphenated
2023-02-03 16:00:24 +00:00
if (
objectHasProp( selectedRate, 'method_id' ) &&
isString( selectedRate.method_id )
) {
return hasCollectableRate( selectedRate?.method_id );
}
}
2022-10-04 12:02:28 +00:00
return state.prefersCollection;
};