Skip to content

Commit

Permalink
Merge pull request #2726 from woocommerce/dev/1426-eliminate-duplicat…
Browse files Browse the repository at this point in the history
…e-functions

Eliminate the duplicate functions used to group shipping time data
  • Loading branch information
eason9487 authored Dec 16, 2024
2 parents ac11774 + c9a7d61 commit af019af
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
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
* @typedef { import("~/data/actions").ShippingTime } ShippingTime
* @typedef { import("~/data/actions").AggregatedShippingTime } AggregatedShippingTime
*/

/**
Expand All @@ -36,7 +35,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 ) {
Expand Down
4 changes: 2 additions & 2 deletions js/src/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions js/src/data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion js/src/data/test/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe( 'reducer', () => {
const action = {
type: TYPES.UPSERT_SHIPPING_TIMES,
shippingTime: {
countryCodes: [ 'JP', 'CA' ],
countries: [ 'JP', 'CA' ],
time: 15,
maxTime: 30,
},
Expand Down
25 changes: 1 addition & 24 deletions js/src/hooks/useSaveShippingTimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/**
Expand All @@ -37,28 +36,6 @@ const getDeletedCountryCodes = ( newShippingTimes, oldShippingTimes ) => {
);
};

/**
* Get aggregated shipping time groups from shipping times.
*
* @param {Array<ShippingTime>} shippingTimes Array of shipping time.
* @return {Array<AggregatedShippingTime>} 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();
Expand Down
5 changes: 0 additions & 5 deletions js/src/utils/getShippingTimeMapKey.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Internal dependencies
*/
import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey';

/**
* Groups shipping times based on time.
*
Expand All @@ -27,7 +22,7 @@ import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey';
* },
* ]
*
* const result = getCountriesTimeArray( shippingTimes );
* const result = getShippingTimesGroups( shippingTimes );
*
* // result:
* // [
Expand All @@ -47,12 +42,12 @@ import getShippingTimeMapKey from '~/utils/getShippingTimeMapKey';
* @param {Array<ShippingTime>} shippingTimes Array of individual shipping times in the format of `{ countryCode, time }`.
* @return {Array<AggregatedShippingTime>} Array of shipping times grouped by time.
*/
const getCountriesTimeArray = ( shippingTimes ) => {
const getShippingTimesGroups = ( shippingTimes ) => {
const timeGroupMap = new Map();

shippingTimes.forEach( ( shippingTime ) => {
const { countryCode, time, maxTime } = shippingTime;
const mapKey = getShippingTimeMapKey( time, maxTime );
const mapKey = `${ time }-${ maxTime }`;
const group = timeGroupMap.get( mapKey ) || {
countries: [],
time,
Expand All @@ -65,7 +60,7 @@ const getCountriesTimeArray = ( shippingTimes ) => {
return Array.from( timeGroupMap.values() );
};

export default getCountriesTimeArray;
export default getShippingTimesGroups;

/**
* @typedef { import("~/data/actions").ShippingTime } ShippingTime
Expand Down

0 comments on commit af019af

Please sign in to comment.