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

Editor: Ensure the current author is included in the dropdown #58716

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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
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;
Loading