This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handpicked product selections when a store has over 100 products. (…
…#4534) * Convert withSearchedProducts to typescript * isLargeCatalog query needs no limit * Pass tests
- Loading branch information
1 parent
26108fc
commit 5f34c86
Showing
5 changed files
with
123 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useState, useCallback } from '@wordpress/element'; | ||
import { blocksConfig } from '@woocommerce/block-settings'; | ||
import { getProducts } from '@woocommerce/editor-components/utils'; | ||
import { useDebouncedCallback } from 'use-debounce'; | ||
import type { ProductResponseItem } from '@woocommerce/types'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { formatError } from '../base/utils/errors.js'; | ||
|
||
/** | ||
* A higher order component that enhances the provided component with products from a search query. | ||
*/ | ||
const withSearchedProducts = ( | ||
OriginalComponent: React.FunctionComponent< Record< string, unknown > > | ||
) => { | ||
return ( { selected, ...props }: { selected: number[] } ): JSX.Element => { | ||
const [ isLoading, setIsLoading ] = useState( true ); | ||
const [ error, setError ] = useState< { | ||
message: string; | ||
type: string; | ||
} | null >( null ); | ||
const [ productsList, setProductsList ] = useState< | ||
ProductResponseItem[] | ||
>( [] ); | ||
const isLargeCatalog = blocksConfig.productCount > 100; | ||
|
||
const setErrorState = async ( e: { | ||
message: string; | ||
type: string; | ||
} ) => { | ||
const formattedError = ( await formatError( e ) ) as { | ||
message: string; | ||
type: string; | ||
}; | ||
setError( formattedError ); | ||
setIsLoading( false ); | ||
}; | ||
|
||
useEffect( () => { | ||
getProducts( { selected } ) | ||
.then( ( results ) => { | ||
setProductsList( results as ProductResponseItem[] ); | ||
setIsLoading( false ); | ||
} ) | ||
.catch( setErrorState ); | ||
}, [ selected ] ); | ||
|
||
const [ debouncedSearch ] = useDebouncedCallback( | ||
( search: string ) => { | ||
getProducts( { selected, search } ) | ||
.then( ( results ) => { | ||
setProductsList( results as ProductResponseItem[] ); | ||
setIsLoading( false ); | ||
} ) | ||
.catch( setErrorState ); | ||
}, | ||
400 | ||
); | ||
|
||
const onSearch = useCallback( | ||
( search: string ) => { | ||
setIsLoading( true ); | ||
debouncedSearch( search ); | ||
}, | ||
[ setIsLoading, debouncedSearch ] | ||
); | ||
|
||
return ( | ||
<OriginalComponent | ||
{ ...props } | ||
selected={ selected } | ||
error={ error } | ||
products={ productsList } | ||
isLoading={ isLoading } | ||
onSearch={ isLargeCatalog ? onSearch : null } | ||
/> | ||
); | ||
}; | ||
}; | ||
|
||
export default withSearchedProducts; |