Skip to content

Commit

Permalink
Add debounced search to categories
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 committed Jan 12, 2021
1 parent f1264a4 commit 6ca009b
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
__experimentalNavigationItem as NavigationItem,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
Expand All @@ -15,21 +14,28 @@ import { useSelect } from '@wordpress/data';
import { MENU_CONTENT_CATEGORIES, MENU_ROOT } from '../constants';
import ContentNavigationItem from '../content-navigation-item';
import SearchResults from '../search-results';
import useDebouncedSearch from '../use-debounced-search';

export default function ContentCategoriesMenu() {
const [ search, setSearch ] = useState( '' );
const onSearch = useCallback( ( value ) => {
setSearch( value );
} );
const {
search,
searchQuery,
onSearch,
isDebouncing,
} = useDebouncedSearch();

const categories = useSelect(
( select ) =>
select( 'core' ).getEntityRecords( 'taxonomy', 'category', {
per_page: -1,
search: searchQuery,
} ),
[]
[ searchQuery ]
);

const isLoading = ! search && categories === null;
const shouldShowLoadingForDebouncing = search && isDebouncing;
const showLoading = isLoading || shouldShowLoadingForDebouncing;

return (
<NavigationMenu
menu={ MENU_CONTENT_CATEGORIES }
Expand All @@ -39,7 +45,7 @@ export default function ContentCategoriesMenu() {
onSearch={ onSearch }
search={ search }
>
{ search && (
{ search && ! isDebouncing && (
<SearchResults items={ categories } search={ search } />
) }

Expand All @@ -51,7 +57,7 @@ export default function ContentCategoriesMenu() {
/>
) ) }

{ ! search && categories === null && (
{ showLoading && (
<NavigationItem title={ __( 'Loading…' ) } isText />
) }
</NavigationMenu>
Expand Down

0 comments on commit 6ca009b

Please sign in to comment.