This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathblock.tsx
195 lines (180 loc) · 5.5 KB
/
block.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* External dependencies
*/
import { _n, __ } from '@wordpress/i18n';
import {
useState,
useEffect,
useCallback,
createInterpolateElement,
} from '@wordpress/element';
import { useShippingData, useStoreCart } from '@woocommerce/base-context/hooks';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
import {
FormattedMonetaryAmount,
RadioControlOptionType,
} from '@woocommerce/blocks-components';
import { decodeEntities } from '@wordpress/html-entities';
import { getSetting } from '@woocommerce/settings';
import { Icon, mapMarker } from '@wordpress/icons';
import { CartShippingPackageShippingRate } from '@woocommerce/types';
import {
isPackageRateCollectable,
getShippingRatesPackageCount,
} from '@woocommerce/base-utils';
import { ExperimentalOrderLocalPickupPackages } from '@woocommerce/blocks-checkout';
import { LocalPickupSelect } from '@woocommerce/base-components/cart-checkout/local-pickup-select';
/**
* Internal dependencies
*/
import ShippingRatesControlPackage from '../../../../base/components/cart-checkout/shipping-rates-control-package';
const getPickupLocation = (
option: CartShippingPackageShippingRate
): string => {
if ( option?.meta_data ) {
const match = option.meta_data.find(
( meta ) => meta.key === 'pickup_location'
);
return match ? match.value : '';
}
return '';
};
const getPickupAddress = (
option: CartShippingPackageShippingRate
): string => {
if ( option?.meta_data ) {
const match = option.meta_data.find(
( meta ) => meta.key === 'pickup_address'
);
return match ? match.value : '';
}
return '';
};
const getPickupDetails = (
option: CartShippingPackageShippingRate
): string => {
if ( option?.meta_data ) {
const match = option.meta_data.find(
( meta ) => meta.key === 'pickup_details'
);
return match ? match.value : '';
}
return '';
};
const renderPickupLocation = (
option: CartShippingPackageShippingRate,
packageCount: number
): RadioControlOptionType => {
const priceWithTaxes = getSetting( 'displayCartPricesIncludingTax', false )
? parseInt( option.price, 10 ) + parseInt( option.taxes, 10 )
: option.price;
const location = getPickupLocation( option );
const address = getPickupAddress( option );
const details = getPickupDetails( option );
// Default to showing "free" as the secondary label. Price checks below will update it if needed.
let secondaryLabel = (
<em>{ __( 'free', 'woo-gutenberg-products-block' ) }</em>
);
// If there is a cost for local pickup, show the cost per package.
if ( parseInt( priceWithTaxes, 10 ) > 0 ) {
// If only one package, show the price and not the package count.
if ( packageCount === 1 ) {
secondaryLabel = (
<FormattedMonetaryAmount
currency={ getCurrencyFromPriceResponse( option ) }
value={ priceWithTaxes }
/>
);
} else {
secondaryLabel = createInterpolateElement(
/* translators: <price/> is the price of the package, <packageCount/> is the number of packages. These must appear in the translated string. */
_n(
'<price/> x <packageCount/> package',
'<price/> x <packageCount/> packages',
packageCount,
'woo-gutenberg-products-block'
),
{
price: (
<FormattedMonetaryAmount
currency={ getCurrencyFromPriceResponse( option ) }
value={ priceWithTaxes }
/>
),
packageCount: <>{ packageCount }</>,
}
);
}
}
return {
value: option.rate_id,
label: location
? decodeEntities( location )
: decodeEntities( option.name ),
secondaryLabel,
description: decodeEntities( details ),
secondaryDescription: address ? (
<>
<Icon
icon={ mapMarker }
className="wc-block-editor-components-block-icon"
/>
{ decodeEntities( address ) }
</>
) : undefined,
};
};
const Block = (): JSX.Element | null => {
const { shippingRates, selectShippingRate } = useShippingData();
// Get pickup locations from the first shipping package.
const pickupLocations = ( shippingRates[ 0 ]?.shipping_rates || [] ).filter(
isPackageRateCollectable
);
const [ selectedOption, setSelectedOption ] = useState< string >(
() => pickupLocations.find( ( rate ) => rate.selected )?.rate_id || ''
);
const onSelectRate = useCallback(
( rateId: string ) => {
selectShippingRate( rateId );
},
[ selectShippingRate ]
);
// Prepare props to pass to the ExperimentalOrderLocalPickupPackages slot fill.
// We need to pluck out receiveCart.
// eslint-disable-next-line no-unused-vars
const { extensions, receiveCart, ...cart } = useStoreCart();
const slotFillProps = {
extensions,
cart,
components: {
ShippingRatesControlPackage,
LocalPickupSelect,
},
renderPickupLocation,
};
// Update the selected option if there is no rate selected on mount.
useEffect( () => {
if ( ! selectedOption && pickupLocations[ 0 ] ) {
setSelectedOption( pickupLocations[ 0 ].rate_id );
onSelectRate( pickupLocations[ 0 ].rate_id );
}
}, [ onSelectRate, pickupLocations, selectedOption ] );
const packageCount = getShippingRatesPackageCount( shippingRates );
return (
<>
<ExperimentalOrderLocalPickupPackages.Slot { ...slotFillProps } />
<ExperimentalOrderLocalPickupPackages>
<LocalPickupSelect
title={ shippingRates[ 0 ].name }
setSelectedOption={ setSelectedOption }
onSelectRate={ onSelectRate }
selectedOption={ selectedOption }
renderPickupLocation={ renderPickupLocation }
pickupLocations={ pickupLocations }
packageCount={ packageCount }
/>
</ExperimentalOrderLocalPickupPackages>
</>
);
};
export default Block;