Skip to content

Commit e464c76

Browse files
committed
Fix the WC 6.9 compatibility problem that the shipping_time of the form state will be incorrect when the selection of shipping rate is 'complex'
1 parent b026401 commit e464c76

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

js/src/components/free-listings/setup-free-listings/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,29 @@ const SetupFreeListings = ( {
115115
} else if ( change.name === 'shipping_country_times' ) {
116116
onShippingTimesChange( values.shipping_country_times );
117117
} else if ( settingsFieldNames.includes( change.name ) ) {
118-
onSettingsChange( getSettings( values ) );
118+
// The value of `shipping_time` option is determined by the value of `shipping_rate` option.
119+
// So if the current form change is considered it needs to change `shipping_time` as well,
120+
// it schedules the processing with `formPropsDelegateeRef` and also skips the call of
121+
// `onSettingsChange` this time, and lets the call of `onSettingsChange` be triggered
122+
// when the form change of `shipping_time` happens.
123+
let shouldTriggerOnChange = true;
124+
125+
if ( change.name === 'shipping_rate' ) {
126+
// When shipping rate is 'manual', shipping time should be 'manual' as well;
127+
// When shipping rate is 'automatic' or 'flat', shipping time should be 'flat'.
128+
const nextValue = change.value === 'manual' ? 'manual' : 'flat';
129+
130+
if ( nextValue !== values.shipping_time ) {
131+
shouldTriggerOnChange = false;
132+
formPropsDelegateeRef.current.push( ( formProps ) =>
133+
formProps.setValue( 'shipping_time', nextValue )
134+
);
135+
}
136+
}
137+
138+
if ( shouldTriggerOnChange ) {
139+
onSettingsChange( getSettings( values ) );
140+
}
119141
} else if ( targetAudienceFields.includes( change.name ) ) {
120142
onTargetAudienceChange( pick( values, targetAudienceFields ) );
121143

js/src/components/shipping-rate-section/shipping-rate-section.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,9 @@ import FlatShippingRatesInputCards from './flat-shipping-rates-input-cards';
2020
*/
2121

2222
const ShippingRateSection = ( { formProps, audienceCountries } ) => {
23-
const { getInputProps, values, setValue } = formProps;
23+
const { getInputProps, values } = formProps;
2424
const inputProps = getInputProps( 'shipping_rate' );
2525

26-
const getShippingRateOptionChangeHandler = ( onChange ) => ( value ) => {
27-
switch ( value ) {
28-
case 'automatic':
29-
case 'flat':
30-
setValue( 'shipping_time', 'flat' );
31-
break;
32-
33-
case 'manual':
34-
setValue( 'shipping_time', 'manual' );
35-
break;
36-
}
37-
38-
onChange( value );
39-
};
40-
4126
return (
4227
<Section
4328
title={ __( 'Shipping rates', 'google-listings-and-ads' ) }
@@ -78,9 +63,6 @@ const ShippingRateSection = ( { formProps, audienceCountries } ) => {
7863
) }
7964
value="automatic"
8065
collapsible
81-
onChange={ getShippingRateOptionChangeHandler(
82-
inputProps.onChange
83-
) }
8466
>
8567
<RadioHelperText>
8668
{ __(
@@ -97,9 +79,6 @@ const ShippingRateSection = ( { formProps, audienceCountries } ) => {
9779
) }
9880
value="flat"
9981
collapsible
100-
onChange={ getShippingRateOptionChangeHandler(
101-
inputProps.onChange
102-
) }
10382
/>
10483
<AppRadioContentControl
10584
{ ...inputProps }
@@ -109,9 +88,6 @@ const ShippingRateSection = ( { formProps, audienceCountries } ) => {
10988
) }
11089
value="manual"
11190
collapsible
112-
onChange={ getShippingRateOptionChangeHandler(
113-
inputProps.onChange
114-
) }
11591
>
11692
<RadioHelperText>
11793
{ createInterpolateElement(

0 commit comments

Comments
 (0)