From 6b7f49106f96b193737e274473dd2234759b9ccb Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 13 Dec 2024 14:31:22 +0800 Subject: [PATCH 1/4] Move `getCountriesTimeArray` to utils directory and rename to `getShippingTimesGroups`. --- .../shipping-time-setup/shipping-countries-form.js | 4 ++-- .../getShippingTimesGroups.js} | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) rename js/src/{components/free-listings/configure-product-listings/shipping-time-setup/getCountriesTimeArray.js => utils/getShippingTimesGroups.js} (91%) diff --git a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js b/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js index 71c2533b2c..2819705cb3 100644 --- a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js +++ b/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js @@ -4,7 +4,7 @@ import VerticalGapLayout from '~/components/vertical-gap-layout'; import AddTimeButton from './add-time-button'; import CountriesTimeInput from './countries-time-input'; -import getCountriesTimeArray from './getCountriesTimeArray'; +import getShippingTimesGroups from '~/utils/getShippingTimesGroups'; /** * @typedef { import("~/data/actions").CountryCode } CountryCode @@ -36,7 +36,7 @@ export default function ShippingCountriesForm( { const remainingCount = remainingCountryCodes.length; // Group countries with the same time. - const countriesTimeArray = getCountriesTimeArray( shippingTimes ); + const countriesTimeArray = getShippingTimesGroups( shippingTimes ); // Prefill to-be-added time. if ( countriesTimeArray.length === 0 ) { diff --git a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/getCountriesTimeArray.js b/js/src/utils/getShippingTimesGroups.js similarity index 91% rename from js/src/components/free-listings/configure-product-listings/shipping-time-setup/getCountriesTimeArray.js rename to js/src/utils/getShippingTimesGroups.js index b5c78b64d4..ea2742db8b 100644 --- a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/getCountriesTimeArray.js +++ b/js/src/utils/getShippingTimesGroups.js @@ -27,7 +27,7 @@ import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey'; * }, * ] * - * const result = getCountriesTimeArray( shippingTimes ); + * const result = getShippingTimesGroups( shippingTimes ); * * // result: * // [ @@ -47,7 +47,7 @@ import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey'; * @param {Array} shippingTimes Array of individual shipping times in the format of `{ countryCode, time }`. * @return {Array} Array of shipping times grouped by time. */ -const getCountriesTimeArray = ( shippingTimes ) => { +const getShippingTimesGroups = ( shippingTimes ) => { const timeGroupMap = new Map(); shippingTimes.forEach( ( shippingTime ) => { @@ -65,7 +65,7 @@ const getCountriesTimeArray = ( shippingTimes ) => { return Array.from( timeGroupMap.values() ); }; -export default getCountriesTimeArray; +export default getShippingTimesGroups; /** * @typedef { import("~/data/actions").ShippingTime } ShippingTime From 9b4fc07e51cd8e84ae9d5038f8726772afeb1775 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 13 Dec 2024 15:26:31 +0800 Subject: [PATCH 2/4] Replace the `getShippingTimesGroups` function in the `useSaveShippingTimes` hook with the one in utils. --- js/src/data/actions.js | 4 ++-- js/src/data/reducer.js | 4 ++-- js/src/data/test/reducer.test.js | 2 +- js/src/hooks/useSaveShippingTimes.js | 25 +------------------------ 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/js/src/data/actions.js b/js/src/data/actions.js index 333c9d6ce4..812dad900e 100644 --- a/js/src/data/actions.js +++ b/js/src/data/actions.js @@ -264,13 +264,13 @@ export function* fetchShippingTimes() { * @throws Will throw an error if the request failed. */ export function* upsertShippingTimes( shippingTime ) { - const { countryCodes, time, maxTime } = shippingTime; + const { countries, time, maxTime } = shippingTime; yield apiFetch( { path: `${ API_NAMESPACE }/mc/shipping/times/batch`, method: 'POST', data: { - country_codes: countryCodes, + country_codes: countries, time, max_time: maxTime, }, diff --git a/js/src/data/reducer.js b/js/src/data/reducer.js index fcffb49f26..45978a39cb 100644 --- a/js/src/data/reducer.js +++ b/js/src/data/reducer.js @@ -195,10 +195,10 @@ const reducer = ( state = DEFAULT_STATE, action ) => { } case TYPES.UPSERT_SHIPPING_TIMES: { - const { countryCodes, time, maxTime } = action.shippingTime; + const { countries, time, maxTime } = action.shippingTime; const times = [ ...state.mc.shipping.times ]; - countryCodes.forEach( ( countryCode ) => { + countries.forEach( ( countryCode ) => { const shippingTime = { countryCode, time, maxTime }; const idx = times.findIndex( ( el ) => el.countryCode === countryCode diff --git a/js/src/data/test/reducer.test.js b/js/src/data/test/reducer.test.js index 9b7d5778a2..1f74579a9e 100644 --- a/js/src/data/test/reducer.test.js +++ b/js/src/data/test/reducer.test.js @@ -291,7 +291,7 @@ describe( 'reducer', () => { const action = { type: TYPES.UPSERT_SHIPPING_TIMES, shippingTime: { - countryCodes: [ 'JP', 'CA' ], + countries: [ 'JP', 'CA' ], time: 15, maxTime: 30, }, diff --git a/js/src/hooks/useSaveShippingTimes.js b/js/src/hooks/useSaveShippingTimes.js index 03bf16d1ef..06446758c3 100644 --- a/js/src/hooks/useSaveShippingTimes.js +++ b/js/src/hooks/useSaveShippingTimes.js @@ -10,11 +10,10 @@ import { useAppDispatch } from '~/data'; import useShippingTimes from './useShippingTimes'; import getDeletedShippingTimes from '~/utils/getDeletedShippingTimes'; import getDifferentShippingTimes from '~/utils/getDifferentShippingTimes'; -import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey'; +import getShippingTimesGroups from '~/utils/getShippingTimesGroups'; /** * @typedef { import("~/data/actions").ShippingTime } ShippingTime - * @typedef { import("~/data/actions").AggregatedShippingTime } AggregatedShippingTime */ /** @@ -37,28 +36,6 @@ const getDeletedCountryCodes = ( newShippingTimes, oldShippingTimes ) => { ); }; -/** - * Get aggregated shipping time groups from shipping times. - * - * @param {Array} shippingTimes Array of shipping time. - * @return {Array} Array of shipping time group. - */ -const getShippingTimesGroups = ( shippingTimes ) => { - const timeGroupMap = new Map(); - shippingTimes.forEach( ( { countryCode, time, maxTime } ) => { - const mapKey = getShippingTimeMapKey( time, maxTime ); - const group = timeGroupMap.get( mapKey ) || { - countryCodes: [], - time, - maxTime, - }; - group.countryCodes.push( countryCode ); - timeGroupMap.set( mapKey, group ); - } ); - - return Array.from( timeGroupMap.values() ); -}; - const useSaveShippingTimes = () => { const { data: oldShippingTimes } = useShippingTimes(); const { deleteShippingTimes, upsertShippingTimes } = useAppDispatch(); From 786cd4898fb2cd75922d1fda4ca26b35acfc2c1e Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 13 Dec 2024 15:40:14 +0800 Subject: [PATCH 3/4] Internalize the `getShippingTimeMapKey` util into `getShippingTimesGroups` util. --- js/src/utils/getShippingTimeMapKey.js | 5 ----- js/src/utils/getShippingTimesGroups.js | 7 +------ 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 js/src/utils/getShippingTimeMapKey.js diff --git a/js/src/utils/getShippingTimeMapKey.js b/js/src/utils/getShippingTimeMapKey.js deleted file mode 100644 index 5e49d07885..0000000000 --- a/js/src/utils/getShippingTimeMapKey.js +++ /dev/null @@ -1,5 +0,0 @@ -const getShippingTimeMapKey = ( time, maxtime ) => { - return `${ time }-${ maxtime }`; -}; - -export default getShippingTimeMapKey; diff --git a/js/src/utils/getShippingTimesGroups.js b/js/src/utils/getShippingTimesGroups.js index ea2742db8b..59f450d8e9 100644 --- a/js/src/utils/getShippingTimesGroups.js +++ b/js/src/utils/getShippingTimesGroups.js @@ -1,8 +1,3 @@ -/** - * Internal dependencies - */ -import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey'; - /** * Groups shipping times based on time. * @@ -52,7 +47,7 @@ const getShippingTimesGroups = ( shippingTimes ) => { shippingTimes.forEach( ( shippingTime ) => { const { countryCode, time, maxTime } = shippingTime; - const mapKey = getShippingTimeMapKey( time, maxTime ); + const mapKey = `${ time }-${ maxTime }`; const group = timeGroupMap.get( mapKey ) || { countries: [], time, From c9a7d61c6fb13cd4c90a0b2e0dd174d3ea62b82a Mon Sep 17 00:00:00 2001 From: Eason Su Date: Fri, 13 Dec 2024 15:41:42 +0800 Subject: [PATCH 4/4] Remove an unreferenced `AggregatedShippingTime` type. --- .../shipping-time-setup/shipping-countries-form.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js b/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js index 2819705cb3..b613d968e8 100644 --- a/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js +++ b/js/src/components/free-listings/configure-product-listings/shipping-time-setup/shipping-countries-form.js @@ -9,7 +9,6 @@ import getShippingTimesGroups from '~/utils/getShippingTimesGroups'; /** * @typedef { import("~/data/actions").CountryCode } CountryCode * @typedef { import("~/data/actions").ShippingTime } ShippingTime - * @typedef { import("~/data/actions").AggregatedShippingTime } AggregatedShippingTime */ /**