Skip to content

Commit

Permalink
refactor: use AutocompleteWithSuggestions from newspack-components (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo authored May 21, 2021
1 parent c04cb80 commit e3fa2da
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 278 deletions.
369 changes: 283 additions & 86 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"@wordpress/plugins": "^2.21.0",
"@wordpress/url": "^2.18.0",
"lodash": "^4.17.20",
"newspack-components": "^1.4.0"
"newspack-components": "^1.5.1"
},
"devDependencies": {
"@automattic/calypso-build": "^6.3.0",
Expand Down
16 changes: 3 additions & 13 deletions src/blocks/list-container/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

const ListContainerEditorComponent = ( {
clientId,
innerBlocks,
isSelected,
parent,
setAttributes,
} ) => {
const ListContainerEditorComponent = ( { clientId, innerBlocks, parent, setAttributes } ) => {
const parentAttributes = parent.attributes || {};
const { queryMode, queryOptions, isSelected: parentIsSelected, showSortUi } = parentAttributes;
const { queryMode, queryOptions, showSortUi } = parentAttributes;
const { order } = queryOptions;

// Sync parent attributes to list container attributes, so that we can use parent attributes in the PHP render callback.
Expand Down Expand Up @@ -105,11 +99,7 @@ const ListContainerEditorComponent = ( {
'newspack-listings/marketplace',
'newspack-listings/place',
] }
renderAppender={ () =>
queryMode || ( ! isSelected && ! parentIsSelected ) ? null : (
<InnerBlocks.ButtonBlockAppender />
)
}
renderAppender={ () => ( queryMode ? null : <InnerBlocks.ButtonBlockAppender /> ) }
/>
</div>
);
Expand Down
49 changes: 45 additions & 4 deletions src/blocks/listing/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import apiFetch from '@wordpress/api-fetch';
import { Button, Placeholder, Spinner } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import { Listing } from './listing';
import { QueryControls } from '../../components';
import { AutocompleteWithSuggestions } from 'newspack-components';
import { capitalize, getIcon } from '../../editor/utils';

const ListingEditorComponent = ( {
Expand Down Expand Up @@ -103,14 +104,54 @@ const ListingEditorComponent = ( {
label={ capitalize( listingTypeSlug ) }
icon={ getIcon( listingTypeSlug ) }
>
<QueryControls
<AutocompleteWithSuggestions
label={
__( 'Search for a ', 'newspack-listings' ) +
listingTypeSlug +
__( ' listing to display.', 'newspack-listings' )
}
listingType={ listingType }
listingTypeSlug={ listingTypeSlug }
fetchSavedPosts={ async postIDs => {
const posts = await apiFetch( {
path: addQueryArgs( 'newspack-listings/v1/listings', {
per_page: 100,
include: postIDs.join( ',' ),
_fields: 'id,title',
} ),
} );

return posts.map( _post => ( {
value: _post.id,
label: decodeEntities( _post.title ) || __( '(no title)', 'newspack-listings' ),
} ) );
} }
fetchSuggestions={ async search => {
const posts = await apiFetch( {
path: addQueryArgs( '/newspack-listings/v1/listings', {
search,
per_page: 10,
_fields: 'id,title',
type: listingType,
} ),
} );

// Only show suggestions if they aren't already in the list.
const result = posts.reduce( ( acc, _post ) => {
if (
listItems.indexOf( _post.id ) < 0 &&
listItems.indexOf( _post.id.toString() ) < 0
) {
acc.push( {
value: _post.id,
label: decodeEntities( _post.title ) || __( '(no title)', 'newspack-listings' ),
} );
}

return acc;
}, [] );
return result;
} }
postType={ listingType }
postTypeSlug={ listingTypeSlug }
maxLength={ 1 }
onChange={ _listing => {
if ( _listing.length ) {
Expand Down
23 changes: 0 additions & 23 deletions src/blocks/listing/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,6 @@
}
}

&__search-suggestions {
border-radius: 2px;
border: 1px solid var( --newspack-listings--grey-medium );
max-height: 204px;
padding: 4px;
overflow: scroll;

.components-button {
color: var( --newspack-listings--grey-dark );
display: block;
margin: 0;
padding: 10px 12px;
text-decoration: none;
width: 100%;

&:active:not( :disabled ),
&:hover:not( :disabled ) {
background: var( --wp-admin-theme-color );
color: white;
}
}
}

&__error {
margin: 1rem auto 2rem;
}
Expand Down
1 change: 0 additions & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as SidebarQueryControls } from './sidebar-query-controls';
export { default as QueryControls } from './query-controls';
150 changes: 0 additions & 150 deletions src/components/query-controls.js

This file was deleted.

0 comments on commit e3fa2da

Please sign in to comment.