-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chrome: Adding a post author drodown
- Loading branch information
1 parent
39f459f
commit 619de3b
Showing
3 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from 'i18n'; | ||
import { PanelRow, withInstanceId } from 'components'; | ||
import { Component } from 'element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { getEditedPostAttribute } from '../../selectors'; | ||
import { editPost } from '../../actions'; | ||
|
||
class PostAuthor extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
authors: [], | ||
}; | ||
} | ||
|
||
fetchAuthors() { | ||
this.fetchAuthorsRequest = new wp.api.collections.Users().fetch( { data: { | ||
roles: 'author,editor,administrator', | ||
per_page: 100, | ||
} } ); | ||
this.fetchAuthorsRequest.then( ( authors ) => { | ||
this.setState( { authors } ); | ||
} ); | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchAuthors(); | ||
} | ||
|
||
componentWillUnmount() { | ||
if ( this.fetchAuthorsRequest ) { | ||
return this.fetchAuthorsRequest.abort(); | ||
} | ||
} | ||
|
||
render() { | ||
const { onUpdateAuthor, postAuthor, instanceId } = this.props; | ||
const { authors } = this.state; | ||
const selectId = 'post-author-selector-' + instanceId; | ||
|
||
if ( authors.length < 2 ) { | ||
return null; | ||
} | ||
|
||
// Disable reason: A select with an onchange throws a warning | ||
|
||
/* eslint-disable jsx-a11y/no-onchange */ | ||
return ( | ||
<PanelRow> | ||
<label htmlFor={ selectId }>{ __( 'Author' ) }</label> | ||
<select | ||
id={ selectId } | ||
value={ postAuthor } | ||
onChange={ ( event ) => onUpdateAuthor( event.target.value ) } | ||
className="editor-post-author__select" | ||
> | ||
{ authors.map( ( author ) => ( | ||
<option key={ author.id } value={ author.id }>{ author.name }</option> | ||
) ) } | ||
</select> | ||
</PanelRow> | ||
); | ||
/* eslint-enable jsx-a11y/no-onchange */ | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
postAuthor: getEditedPostAttribute( state, 'author' ), | ||
}; | ||
}, | ||
{ | ||
onUpdateAuthor( author ) { | ||
return editPost( { author } ); | ||
}, | ||
}, | ||
)( withInstanceId( PostAuthor ) ); |
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,3 @@ | ||
.editor-post-author__select { | ||
margin: -5px 0; | ||
} |
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