Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post author selector: try downshift #7478

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 78 additions & 16 deletions editor/components/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,107 @@ import { __ } from '@wordpress/i18n';
import { withInstanceId } from '@wordpress/components';
import { Component, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import apiRequest from '@wordpress/api-request';

/**
* Internal dependencies
*/
import PostAuthorCheck from './check';

/**
* External dependencies
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the external dependencies are defined above all other deps, but this is not an official guideline :)

import Downshift from 'downshift';
import { debounce } from 'underscore';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use lodash instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, right :)


export class PostAuthor extends Component {
constructor() {
super( ...arguments );

this.setAuthorId = this.setAuthorId.bind( this );
this.suggestAuthors = this.suggestAuthors.bind( this );
}

setAuthorId( event ) {
setAuthorId( selected ) {
const { onUpdateAuthor } = this.props;
const { value } = event.target;
onUpdateAuthor( Number( value ) );
const { id } = selected;
onUpdateAuthor( Number( id ) );
}

suggestAuthors( query, args ) {
if ( ! args.isOpen ) {
return;
}
const payload = '?search=' + encodeURIComponent( query );
this.setState( { searching: true } );
apiRequest( { path: '/wp/v2/users' + payload } ).done( ( results ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leverage the data module instead? We already have a users reducer their which I believe should be refactored to be an entity and build support to searching/pagination into the entities. related #6395

this.setState( { searching: false } );
this.setState( { authors: results.map( ( author ) => ( { id: author.id, name: author.name } ) ) } );
} );
}

render() {
const { postAuthor, instanceId, authors } = this.props;
const { instanceId, authors, postAuthor } = this.props;
const selectId = 'post-author-selector-' + instanceId;

// Disable reason: A select with an onchange throws a warning
const currentPostAuthor = postAuthor.length > 0 ? postAuthor[ 0 ].name : '';
const allAuthors = this.state && this.state.authors ? this.state.authors : authors;
const isSearching = this.state && this.state.searching;

/* eslint-disable jsx-a11y/no-onchange */
return (
currentPostAuthor && allAuthors &&
<PostAuthorCheck>
<label htmlFor={ selectId }>{ __( 'Author' ) }</label>
<select
id={ selectId }
value={ postAuthor }
<Downshift
onChange={ this.setAuthorId }
className="editor-post-author__select"
itemToString={ ( author ) => ( author ? author.value : '' ) }
defaultInputValue={ currentPostAuthor }
onInputValueChange={ debounce( this.suggestAuthors, 300 ) }
>
{ authors.map( ( author ) => (
<option key={ author.id } value={ author.id }>{ author.name }</option>
) ) }
</select>
{ ( {
getInputProps,
getItemProps,
getLabelProps,
getMenuProps,
isOpen,
inputValue,
highlightedIndex,
selectedItem,
} ) => (
<div>
<label { ...getLabelProps() }>{ __( 'Author' ) }</label>
<input { ...getInputProps() } />
<span
className={ 'spinner' + ( isSearching ? ' is-active' : '' ) }
style={ { position: 'absolute', right: '10px' } }
/>
<ul { ...getMenuProps() } >
{ isOpen ?
allAuthors
.map( ( author ) => ( { id: author.id, value: author.name } ) )
.filter( ( author ) =>
! inputValue ||
author.value.toLowerCase().includes( inputValue.toLowerCase() ) )
.map( ( author, index ) => (
<li
{ ...getItemProps( {
key: author.id,
index,
item: author,
style: {
backgroundColor:
highlightedIndex === index ? 'lightgray' : 'white',
fontWeight: selectedItem === author ? 'bold' : 'normal',
},
} ) }
>
{ author.value }
</li>
) ) :
null }
</ul>
</div>
) }
</Downshift>
</PostAuthorCheck>
);
/* eslint-enable jsx-a11y/no-onchange */
Expand All @@ -53,7 +115,7 @@ export class PostAuthor extends Component {
export default compose( [
withSelect( ( select ) => {
return {
postAuthor: select( 'core/editor' ).getEditedPostAttribute( 'author' ),
postAuthor: select( 'core' ).getAuthor( select( 'core/editor' ).getEditedPostAttribute( 'author' ) ),
authors: select( 'core' ).getAuthors(),
};
} ),
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"cross-env": "3.2.4",
"deep-freeze": "0.0.1",
"doctrine": "2.1.0",
"downshift": "2.0.7",
"eslint": "4.16.0",
"eslint-config-wordpress": "2.0.0",
"eslint-plugin-jest": "21.5.0",
Expand Down
13 changes: 12 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ export async function* getCategories() {
* Requests authors from the REST API.
*/
export async function* getAuthors() {
const users = await apiRequest( { path: '/wp/v2/users/?who=authors&per_page=-1' } );
const users = await apiRequest( { path: '/wp/v2/users/?who=authors&per_page=100' } );
yield receiveUserQuery( 'authors', users );
}

/**
* Requests author details from the REST API.
*
* @param {Object} state State tree
* @param {number} id Author id.
*/
export async function* getAuthor( state, id ) {
const author = await apiRequest( { path: `/wp/v2/users/${ id }` } );
yield receiveUserQuery( 'author', author );
}

/**
* Requests an entity's record from the REST API.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ export function getAuthors( state ) {
return getUserQueryResults( state, 'authors' );
}

/**
* Returns all the post author.
*
* @param {Object} state Data state.
*
* @return {Array} Authors list.
*/
export function getAuthor( state ) {
return getUserQueryResults( state, 'author' );
}

/**
* Returns all the users returned by a query ID.
*
Expand Down