Skip to content

Commit

Permalink
Fix issue with pattern sync filter randomly updating
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed Sep 15, 2023
1 parent 6c51286 commit 8e47b94
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
import { Modal } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import PatternExplorerSidebar from './sidebar';
import PatternList from './patterns-list';
import { usePatternsCategories } from '../block-patterns-tab';
import { store as blockEditorStore } from '../../../store';

function PatternsExplorer( { initialCategory, rootClientId } ) {
const [ searchValue, setSearchValue ] = useState( '' );
const [ patternSourceFilter, setPatternSourceFilter ] = useState( 'all' );
const patternSyncFilter = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return settings.patternsSyncFilter || 'all';
}, [] );

const [ selectedCategory, setSelectedCategory ] = useState(
initialCategory?.name
);
Expand All @@ -47,7 +41,6 @@ function PatternsExplorer( { initialCategory, rootClientId } ) {
selectedCategory={ selectedCategory }
patternCategories={ patternCategories }
patternSourceFilter={ patternSourceFilter }
patternSyncFilter={ patternSyncFilter }
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useMemo, useEffect, useRef } from '@wordpress/element';
import { useMemo, useEffect, useRef, useState } from '@wordpress/element';
import { _n, sprintf } from '@wordpress/i18n';
import { useDebounce } from '@wordpress/compose';
import { __experimentalHeading as Heading } from '@wordpress/components';
Expand Down Expand Up @@ -67,8 +67,8 @@ function PatternList( {
patternSourceFilter,
selectedCategory,
patternCategories,
patternSyncFilter,
} ) {
const [ patternSyncFilter, setPatternSyncFilter ] = useState( 'all' );
const container = useRef();
const debouncedSpeak = useDebounce( speak, 500 );
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
Expand Down Expand Up @@ -171,7 +171,12 @@ function PatternList( {

<InserterListbox>
{ patternSourceFilter === PATTERN_TYPES.user &&
! searchValue && <BlockPatternsSyncFilter /> }
! searchValue && (
<BlockPatternsSyncFilter
patternSyncFilter={ patternSyncFilter }
setPatternSyncFilter={ setPatternSyncFilter }
/>
) }

{ hasItems && (
<BlockPatternsList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
*/
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
export const SYNC_TYPES = {
full: 'fully',
unsynced: 'unsynced',
Expand All @@ -20,28 +18,17 @@ const patternSyncOptions = [
{ value: SYNC_TYPES.unsynced, label: __( 'Standard' ) },
];

export function BlockPatternsSyncFilter() {
const { updateSettings } = useDispatch( blockEditorStore );

const syncFilter = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return settings.patternsSyncFilter || 'all';
}, [] );

const handleUpdateSyncFilter = ( value ) => {
updateSettings( {
patternsSyncFilter: value,
} );
};

export function BlockPatternsSyncFilter( {
setPatternSyncFilter,
patternSyncFilter,
} ) {
return (
<SelectControl
className="block-editor-patterns__sync-status-filter"
label={ __( 'Syncing' ) }
options={ patternSyncOptions }
value={ syncFilter }
onChange={ ( value ) => handleUpdateSyncFilter( value ) }
value={ patternSyncFilter }
onChange={ setPatternSyncFilter }
aria-label={ __( 'Filter patterns by sync type' ) }
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
import { Icon, chevronRight, chevronLeft } from '@wordpress/icons';
import { focus } from '@wordpress/dom';
import { speak } from '@wordpress/a11y';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -39,7 +38,6 @@ import {
BlockPatternsSyncFilter,
SYNC_TYPES,
} from './block-patterns-sync-filter';
import { store as blockEditorStore } from '../../store';

const noop = () => {};

Expand Down Expand Up @@ -198,11 +196,8 @@ export function BlockPatternsCategoryPanel( {
onInsert,
rootClientId
);
const patternSyncFilter = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return settings.patternsSyncFilter || 'all';
}, [] );
const [ patternSyncFilter, setPatternSyncFilter ] = useState( 'all' );

const availableCategories = usePatternsCategories(
rootClientId,
patternFilter
Expand Down Expand Up @@ -256,6 +251,7 @@ export function BlockPatternsCategoryPanel( {
);

// Hide block pattern preview on unmount.
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect( () => () => onHover( null ), [] );

return (
Expand All @@ -268,7 +264,11 @@ export function BlockPatternsCategoryPanel( {
</div>
<p>{ category.description }</p>
{ patternFilter === PATTERN_TYPES.user && (
<BlockPatternsSyncFilter />
<BlockPatternsSyncFilter
patternSyncFilter={ patternSyncFilter }
setPatternSyncFilter={ setPatternSyncFilter }
test={ 'bob' }
/>
) }
{ ! currentCategoryPatterns.length && (
<div>{ __( 'No results found' ) }</div>
Expand Down

0 comments on commit 8e47b94

Please sign in to comment.