Skip to content

Commit

Permalink
Add notice when model name is empty on publish (#124)
Browse files Browse the repository at this point in the history
Co-authored-by: Luis Felipe Zaguini <luisfelipezaguini@gmail.com>
  • Loading branch information
candy02058912 and zaguiini authored Sep 5, 2024
1 parent 6f5a33a commit 5320c21
Showing 1 changed file with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,62 @@ import { store as noticesStore } from '@wordpress/notices';
export const useContentModelNameLengthRestrictor = () => {
const { editPost, lockPostSaving, unlockPostSaving } =
useDispatch( editorStore );
const { createNotice } = useDispatch( noticesStore );
const { createNotice, removeNotice } = useDispatch( noticesStore );

const title = useSelect( ( select ) =>
select( editorStore ).getEditedPostAttribute( 'title' )
const { title, isPublishSidebarOpened, isPublishingPost } = useSelect(
( select ) => ( {
title: select( editorStore ).getEditedPostAttribute( 'title' ),
isPublishSidebarOpened:
select( editorStore ).isPublishSidebarOpened(),
isPublishingPost: select( editorStore ).isPublishingPost(),
} )
);

useEffect( () => {
const trimmedTitle = title?.trim() ?? '';

if ( trimmedTitle.length === 0 ) {
lockPostSaving( 'title-empty-lock' );
return;
}
} else {
unlockPostSaving( 'title-empty-lock' );
removeNotice( 'title-empty-notice' );

unlockPostSaving( 'title-empty-lock' );
editPost( { title: trimmedTitle.substring( 0, 20 ) } );

editPost( { title: trimmedTitle.substring( 0, 20 ) } );
if ( trimmedTitle.length > 20 ) {
createNotice(
'warning',
__( 'Model name is limited to 20 characters.' ),
{
id: 'title-length-notice',
type: 'snackbar',
isDismissible: true,
}
);
}
}

if ( trimmedTitle.length > 20 ) {
if (
( isPublishSidebarOpened || isPublishingPost ) &&
trimmedTitle.length === 0
) {
createNotice(
'warning',
__( 'Title is limited to 20 characters.' ),
__( 'Please enter a model name in order to publish.' ),
{
type: 'snackbar',
id: 'title-empty-notice',
isDismissible: true,
}
);
}
}, [ title, lockPostSaving, unlockPostSaving, editPost, createNotice ] );
}, [
title,
isPublishSidebarOpened,
isPublishingPost,
lockPostSaving,
unlockPostSaving,
editPost,
createNotice,
removeNotice,
] );
};

0 comments on commit 5320c21

Please sign in to comment.