Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Replace flatten, uniqBy usage in getProducts()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 21, 2023
1 parent 16c8bbd commit 0f5c918
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions assets/js/editor-components/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable you-dont-need-lodash-underscore/flatten -- until we have an alternative to uniqBy we'll keep flatten to avoid potential introduced bugs with alternatives */
/**
* External dependencies
*/
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
import { flatten, uniqBy } from 'lodash';
import { getSetting } from '@woocommerce/settings';
import { blocksConfig } from '@woocommerce/block-settings';

Expand Down Expand Up @@ -50,6 +48,18 @@ const getProductsRequests = ( {
return requests;
};

const uniqBy = ( array, iteratee ) => {
const seen = new Map();
return array.filter( ( item ) => {
const key = iteratee( item );
if ( ! seen.has( key ) ) {
seen.set( key, item );
return true;
}
return false;
} );
};

/**
* Get a promise that resolves to a list of products from the Store API.
*
Expand All @@ -69,7 +79,8 @@ export const getProducts = ( {

return Promise.all( requests.map( ( path ) => apiFetch( { path } ) ) )
.then( ( data ) => {
const products = uniqBy( flatten( data ), 'id' );
const flatData = data.flat();
const products = uniqBy( flatData, ( item ) => item.id );
const list = products.map( ( product ) => ( {
...product,
parent: 0,
Expand Down

0 comments on commit 0f5c918

Please sign in to comment.