Skip to content

Commit add851c

Browse files
committed
Demo for the proposal of updating the shipping config type with data management hierarchy.
Ref: #1750 (comment)
1 parent 161f254 commit add851c

File tree

6 files changed

+52
-55
lines changed

6 files changed

+52
-55
lines changed

js/src/components/free-listings/configure-product-listings/checkErrors.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { __ } from '@wordpress/i18n';
99
import isNonFreeShippingRate from '.~/utils/isNonFreeShippingRate';
1010

1111
const validlocationSet = new Set( [ 'all', 'selected' ] );
12-
const validShippingRateSet = new Set( [ 'automatic', 'flat', 'manual' ] );
13-
const validShippingTimeSet = new Set( [ 'flat', 'manual' ] );
12+
const shippingRateConfigTypesSet = new Set( [ 'automatic', 'flat', 'manual' ] );
13+
const shippingTimeConfigTypesSet = new Set( [ 'flat', 'automatic' ] );
1414
const validTaxRateSet = new Set( [ 'destination', 'manual' ] );
1515

1616
const checkErrors = ( values, shippingTimes, finalCountryCodes ) => {
@@ -32,30 +32,33 @@ const checkErrors = ( values, shippingTimes, finalCountryCodes ) => {
3232
}
3333

3434
/**
35-
* Check shipping rates.
35+
* Check shipping config type.
3636
*/
37-
if ( ! validShippingRateSet.has( values.shipping_rate ) ) {
38-
errors.shipping_rate = __(
39-
'Please select a shipping rate option.',
37+
if ( ! shippingRateConfigTypesSet.has( values.shippingConfigType ) ) {
38+
errors.shippingConfigType = __(
39+
'Please select a shipping option.',
4040
'google-listings-and-ads'
4141
);
4242
}
4343

44+
/**
45+
* Check shipping rates.
46+
*/
4447
if (
45-
values.shipping_rate === 'flat' &&
48+
values.shippingConfigType === 'flat' &&
4649
( values.shipping_country_rates.length < finalCountryCodes.length ||
4750
values.shipping_country_rates.some( ( el ) => el.rate < 0 ) )
4851
) {
49-
errors.shipping_rate = __(
52+
errors.shippingConfigType = __(
5053
'Please specify shipping rates for all the countries. And the estimated shipping rate cannot be less than 0.',
5154
'google-listings-and-ads'
5255
);
5356
}
5457

5558
/**
56-
* Check offer free shipping, only when shipping_rate is 'flat'.
59+
* Check offer free shipping, only when shippingConfigType is 'flat'.
5760
*/
58-
if ( values.shipping_rate === 'flat' ) {
61+
if ( values.shippingConfigType === 'flat' ) {
5962
if (
6063
values.offer_free_shipping === undefined &&
6164
values.shipping_country_rates.some( isNonFreeShippingRate )
@@ -83,19 +86,12 @@ const checkErrors = ( values, shippingTimes, finalCountryCodes ) => {
8386
/**
8487
* Check shipping times.
8588
*/
86-
if ( ! validShippingTimeSet.has( values.shipping_time ) ) {
87-
errors.shipping_time = __(
88-
'Please select a shipping time option.',
89-
'google-listings-and-ads'
90-
);
91-
}
92-
9389
if (
94-
values.shipping_time === 'flat' &&
90+
shippingTimeConfigTypesSet.has( values.shippingConfigType ) &&
9591
( shippingTimes.length < finalCountryCodes.length ||
9692
shippingTimes.some( ( el ) => el.time < 0 ) )
9793
) {
98-
errors.shipping_time = __(
94+
errors.shippingConfigType = __(
9995
'Please specify shipping times for all the countries. And the estimated shipping time cannot be less than 0.',
10096
'google-listings-and-ads'
10197
);

js/src/components/free-listings/setup-free-listings/form-content.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ const FormContent = ( {
3636
submitLabel = __( 'Complete setup', 'google-listings-and-ads' ),
3737
} ) => {
3838
const { values, isValidForm, handleSubmit } = formProps;
39+
const { shippingConfigType } = values;
3940
const shouldDisplayTaxRate = useDisplayTaxRate( countries );
40-
const shouldDisplayShippingTime = values.shipping_time === 'flat';
41+
// Currently, the automatic application of shipping time config is not supported,
42+
// so the user is still required to enter the config.
43+
const shouldDisplayShippingTime =
44+
shippingConfigType === 'flat' || shippingConfigType === 'automatic';
4145
const isCompleteSetupDisabled =
4246
shouldDisplayTaxRate === null || ! isValidForm;
4347

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const targetAudienceFields = [ 'locale', 'language', 'location', 'countries' ];
3030
* If we are adding a new settings field, it should be added into this array.
3131
*/
3232
const settingsFieldNames = [
33-
'shipping_rate',
34-
'shipping_time',
33+
'shippingConfigType',
3534
'tax_rate',
3635
'website_live',
3736
'checkout_process_secure',
@@ -142,27 +141,7 @@ const SetupFreeListings = ( {
142141
} else if ( change.name === 'shipping_country_times' ) {
143142
onShippingTimesChange( values.shipping_country_times );
144143
} else if ( settingsFieldNames.includes( change.name ) ) {
145-
// The value of `shipping_time` option is determined by the value of `shipping_rate` option.
146-
// So if the current form change is considered it needs to change `shipping_time` as well,
147-
// it schedules the processing with `formPropsDelegateeRef` and also skips the call of
148-
// `onSettingsChange` this time, and lets the call of `onSettingsChange` be triggered
149-
// when the form change of `shipping_time` happens.
150-
let shouldTriggerOnChange = true;
151-
152-
if ( change.name === 'shipping_rate' ) {
153-
// When shipping rate is 'manual', shipping time should be 'manual' as well;
154-
// When shipping rate is 'automatic' or 'flat', shipping time should be 'flat'.
155-
const nextValue = change.value === 'manual' ? 'manual' : 'flat';
156-
157-
if ( nextValue !== values.shipping_time ) {
158-
shouldTriggerOnChange = false;
159-
setValue( 'shipping_time', nextValue );
160-
}
161-
}
162-
163-
if ( shouldTriggerOnChange ) {
164-
onSettingsChange( getSettings( values ) );
165-
}
144+
onSettingsChange( getSettings( values ) );
166145
} else if ( targetAudienceFields.includes( change.name ) ) {
167146
onTargetAudienceChange( pick( values, targetAudienceFields ) );
168147

@@ -194,8 +173,7 @@ const SetupFreeListings = ( {
194173
location: targetAudience.location,
195174
countries: targetAudience.countries || [],
196175
// These are the fields for settings.
197-
shipping_rate: settings.shipping_rate,
198-
shipping_time: settings.shipping_time,
176+
shippingConfigType: settings.shippingConfigType,
199177
tax_rate: settings.tax_rate,
200178
website_live: settings.website_live,
201179
checkout_process_secure: settings.checkout_process_secure,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import FlatShippingRatesInputCards from './flat-shipping-rates-input-cards';
2121

2222
const ShippingRateSection = ( { formProps, audienceCountries } ) => {
2323
const { getInputProps, values } = formProps;
24-
const inputProps = getInputProps( 'shipping_rate' );
24+
const inputProps = getInputProps( 'shippingConfigType' );
2525

2626
return (
2727
<Section
@@ -110,7 +110,7 @@ const ShippingRateSection = ( { formProps, audienceCountries } ) => {
110110
</VerticalGapLayout>
111111
</Section.Card.Body>
112112
</Section.Card>
113-
{ values.shipping_rate === 'flat' && (
113+
{ values.shippingConfigType === 'flat' && (
114114
<FlatShippingRatesInputCards
115115
audienceCountries={ audienceCountries }
116116
formProps={ formProps }

js/src/data/actions.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ export function handleFetchError( error, message ) {
8484
*
8585
* @typedef {Object} SettingsData
8686
* @property {boolean} [offer_free_shipping] Whether if the merchant offers free shipping.
87-
* @property {'automatic'|'flat'|'manual'} [shipping_rate] Type of the shipping rate.
88-
* @property {'flat'|'manual'} [shipping_time] Type of the shipping time.
87+
* @property {'automatic'|'flat'|'manual'} [shippingConfigType] Type of the shipping config to be set up.
8988
* @property {string|null} [tax_rate] Type of tax rate, There are two possible values if US is selected: 'destination' and 'manual' otherwise will be null.
9089
* @property {boolean} [website_live] Whether the store website is live.
9190
* @property {boolean} [checkout_process_secure] Whether the checkout process is complete and secure.
@@ -310,9 +309,20 @@ export function* fetchSettings() {
310309
path: `${ API_NAMESPACE }/mc/settings`,
311310
} );
312311

312+
const settings = {
313+
...response,
314+
shippingConfigType: response.shipping_rate,
315+
};
316+
317+
// It doesn't have to be deleted and can be kept. The consideration here is to avoid
318+
// possible confusion when seeing `shipping_rate` and `shipping_time` when inspecting
319+
// `settings` in the application layer.
320+
delete settings.shipping_rate;
321+
delete settings.shipping_time;
322+
313323
return {
314324
type: TYPES.RECEIVE_SETTINGS,
315-
settings: response,
325+
settings,
316326
};
317327
} catch ( error ) {
318328
yield handleFetchError(
@@ -332,10 +342,20 @@ export function* fetchSettings() {
332342
* @return {Object} Action object to save target audience.
333343
*/
334344
export function* saveSettings( settings ) {
345+
const { shippingConfigType, ...data } = settings;
346+
347+
data.shipping_rate = shippingConfigType;
348+
349+
// Currently, the automatic application of shipping time config is not supported,
350+
// therefore, when shipping config type is 'automatic', the value of `shipping_time`
351+
// for calling API needs to be converted to 'flat'.
352+
data.shipping_time =
353+
shippingConfigType === 'automatic' ? 'flat' : shippingConfigType;
354+
335355
yield apiFetch( {
336356
path: `${ API_NAMESPACE }/mc/settings`,
337357
method: 'POST',
338-
data: settings,
358+
data,
339359
} );
340360

341361
return {

js/src/setup-mc/setup-stepper/saved-setup-stepper.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ const SavedSetupStepper = ( { savedStep } ) => {
6767
// Auto-save the default values for shipping options to fall back with the original implementation.
6868
// Ref: https://github.com/woocommerce/google-listings-and-ads/blob/2.0.2/js/src/setup-mc/setup-stepper/setup-free-listings/form-content.js#L33
6969
useEffect( () => {
70-
if ( settings?.shipping_rate === null ) {
70+
if ( settings?.shippingConfigType === null ) {
7171
saveSettings( {
7272
...settings,
73-
shipping_rate: 'automatic',
74-
shipping_time: 'flat',
73+
shippingConfigType: 'automatic',
7574
} );
7675
}
7776
}, [ settings, saveSettings ] );
@@ -125,7 +124,7 @@ const SavedSetupStepper = ( { savedStep } ) => {
125124
const initShippingRates = hasResolvedShippingRates ? shippingRates : null;
126125
const initShippingTimes = hasResolvedShippingTimes ? shippingTimes : null;
127126
const initTargetAudience = targetAudience?.location ? targetAudience : null;
128-
const initSettings = settings?.shipping_rate ? settings : null;
127+
const initSettings = settings?.shippingConfigType ? settings : null;
129128

130129
return (
131130
<Stepper

0 commit comments

Comments
 (0)