2020-02-25 09:32:59 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
2020-03-05 19:54:05 +00:00
|
|
|
import { useSelectShippingRate } from '@woocommerce/base-hooks';
|
2020-02-25 09:32:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Package from './package';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const Packages = ( {
|
|
|
|
className,
|
|
|
|
noResultsMessage,
|
|
|
|
renderOption,
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates = [],
|
2020-02-25 09:32:59 +00:00
|
|
|
} ) => {
|
2020-03-05 19:54:05 +00:00
|
|
|
const { selectShippingRate, selectedShippingRates } = useSelectShippingRate(
|
|
|
|
shippingRates
|
|
|
|
);
|
2020-02-25 09:32:59 +00:00
|
|
|
return shippingRates.map( ( shippingRate, i ) => (
|
|
|
|
<Package
|
2020-03-05 19:54:05 +00:00
|
|
|
key={ shippingRate.package_id }
|
2020-02-25 09:32:59 +00:00
|
|
|
className={ className }
|
|
|
|
noResultsMessage={ noResultsMessage }
|
|
|
|
onChange={ ( newShippingRate ) => {
|
2020-03-05 19:54:05 +00:00
|
|
|
selectShippingRate( newShippingRate, i );
|
2020-02-25 09:32:59 +00:00
|
|
|
} }
|
|
|
|
renderOption={ renderOption }
|
2020-03-05 19:54:05 +00:00
|
|
|
selected={ selectedShippingRates[ i ] }
|
2020-02-25 09:32:59 +00:00
|
|
|
shippingRate={ shippingRate }
|
|
|
|
showItems={ shippingRates.length > 1 }
|
2020-03-03 12:38:21 +00:00
|
|
|
title={ shippingRates.length > 1 ? shippingRate.name : null }
|
2020-02-25 09:32:59 +00:00
|
|
|
/>
|
|
|
|
) );
|
|
|
|
};
|
|
|
|
|
|
|
|
Packages.propTypes = {
|
|
|
|
renderOption: PropTypes.func.isRequired,
|
|
|
|
className: PropTypes.string,
|
|
|
|
noResultsMessage: PropTypes.string,
|
|
|
|
shippingRates: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
2020-03-05 19:54:05 +00:00
|
|
|
items: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
key: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
quantity: PropTypes.number,
|
|
|
|
} )
|
|
|
|
).isRequired,
|
|
|
|
package_id: PropTypes.number,
|
|
|
|
name: PropTypes.string,
|
|
|
|
destination: PropTypes.object,
|
|
|
|
shipping_rates: PropTypes.arrayOf( PropTypes.object ),
|
2020-02-25 09:32:59 +00:00
|
|
|
} ).isRequired
|
|
|
|
).isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Packages;
|