Skip to content

Commit

Permalink
Editor: Ensure the current author is included in the dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Feb 6, 2024
1 parent 77878ea commit 9fabc68
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions packages/editor/src/components/post-author/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,47 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '../../store';
import { AUTHORS_QUERY } from './constants';

function PostAuthorSelect() {
export default function PostAuthorSelect() {
const { editPost } = useDispatch( editorStore );
const { postAuthor, authors } = useSelect( ( select ) => {
const { authorId, postAuthor, authors } = useSelect( ( select ) => {
const { getUser, getUsers } = select( coreStore );
const { getEditedPostAttribute } = select( editorStore );
const _authorId = getEditedPostAttribute( 'author' );

return {
postAuthor:
select( editorStore ).getEditedPostAttribute( 'author' ),
authors: select( coreStore ).getUsers( AUTHORS_QUERY ),
authorId: _authorId,
authors: getUsers( AUTHORS_QUERY ),
postAuthor: getUser( _authorId, {
context: 'view',
} ),
};
}, [] );

const authorOptions = useMemo( () => {
return ( authors ?? [] ).map( ( author ) => {
const fetchedAuthors = ( authors ?? [] ).map( ( author ) => {
return {
value: author.id,
label: decodeEntities( author.name ),
};
} );
}, [ authors ] );

// Ensure the current author is included in the dropdown list.
const foundAuthor = fetchedAuthors.findIndex(
( { value } ) => postAuthor?.id === value
);

if ( foundAuthor < 0 && postAuthor ) {
return [
{
value: postAuthor.id,
label: decodeEntities( postAuthor.name ),
},
...fetchedAuthors,
];
}

return fetchedAuthors;
}, [ authors, postAuthor ] );

const setAuthorId = ( value ) => {
const author = Number( value );
Expand All @@ -46,9 +69,7 @@ function PostAuthorSelect() {
label={ __( 'Author' ) }
options={ authorOptions }
onChange={ setAuthorId }
value={ postAuthor }
value={ authorId }
/>
);
}

export default PostAuthorSelect;

0 comments on commit 9fabc68

Please sign in to comment.