woocommerce/plugins/woocommerce-blocks/assets/js/base/components/cart-checkout/pickup-location/index.tsx

79 lines
2.2 KiB
TypeScript
Raw Normal View History

Add "Collection from..." in Checkout sidebar when selecting local pickup (https://github.com/woocommerce/woocommerce-blocks/pull/8305) * Add get_collectible_method_ids function * Add collectibleMethodIds to asset data registry * Remove unnecessary pluck and add pickup_location to returned array * Add hasSelectedLocalPickup to shipping types * show shipping address even if collecting * Make checkout store set prefersCollection based on IDs from settings * Move areRatesCollectible outside of hook * Add pickup location component * Show pickup location if user prefers collection * Move prefersCollection check into ShippingAddress component * Remove spread for collectibleMethodIds Not needed now since pickup_location is included in the setting by default * Check address metadata has a value before displaying it * Add tests for ShippingAddress component * Move PickupLocation specific tests to new file * Ensure TotalsShipping shows only one package rate if local pickup chosen * Update prefersCollection selector to use typeof check * Use isPackageRateCollectible rather than checking against settings * Do not show calculator button if local pickup rate is selected * Update test to mock correct setting * Remove unused method from ShippingController * Check isPackageRateCollectable rather than checking settings array * Update test to mock correct setting * Change spelling of collectible to collectable * Improve mocked useSelect function Old one returned incorrect data shape for prefersCollection * Remove duplicate import
2023-04-06 11:56:47 +00:00
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { isObject, objectHasProp } from '@woocommerce/types';
import { isPackageRateCollectable } from '@woocommerce/base-utils';
/**
* Shows a formatted pickup location.
*/
const PickupLocation = (): JSX.Element | null => {
const { pickupAddress, pickupMethod } = useSelect( ( select ) => {
const cartShippingRates = select( 'wc/store/cart' ).getShippingRates();
const flattenedRates = cartShippingRates.flatMap(
( cartShippingRate ) => cartShippingRate.shipping_rates
);
const selectedCollectableRate = flattenedRates.find(
( rate ) => rate.selected && isPackageRateCollectable( rate )
);
// If the rate has an address specified in its metadata.
if (
isObject( selectedCollectableRate ) &&
objectHasProp( selectedCollectableRate, 'meta_data' )
) {
const selectedRateMetaData = selectedCollectableRate.meta_data.find(
( meta ) => meta.key === 'pickup_address'
);
if (
isObject( selectedRateMetaData ) &&
objectHasProp( selectedRateMetaData, 'value' ) &&
selectedRateMetaData.value
) {
const selectedRatePickupAddress = selectedRateMetaData.value;
return {
pickupAddress: selectedRatePickupAddress,
pickupMethod: selectedCollectableRate.name,
};
}
}
if ( isObject( selectedCollectableRate ) ) {
return {
pickupAddress: undefined,
pickupMethod: selectedCollectableRate.name,
};
}
return {
pickupAddress: undefined,
pickupMethod: undefined,
};
} );
// If the method does not contain an address, or the method supporting collection was not found, return early.
if (
typeof pickupAddress === 'undefined' &&
typeof pickupMethod === 'undefined'
) {
return null;
}
// Show the pickup method's name if we don't have an address to show.
return (
<span className="wc-block-components-shipping-address">
{ sprintf(
/* translators: %s: shipping method name, e.g. "Amazon Locker" */
__( 'Collection from %s', 'woo-gutenberg-products-block' ),
typeof pickupAddress === 'undefined'
? pickupMethod
: pickupAddress
) + ' ' }
</span>
);
};
export default PickupLocation;