Skip to content

Commit d9755f9

Browse files
authored
Prevent saving content model without a name (#123)
1 parent 11846ab commit d9755f9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

includes/manager/src/hooks/use-content-model-name-length-restrictor.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@ import { store as editorStore } from '@wordpress/editor';
55
import { store as noticesStore } from '@wordpress/notices';
66

77
export const useContentModelNameLengthRestrictor = () => {
8-
const { editPost } = useDispatch( editorStore );
8+
const { editPost, lockPostSaving, unlockPostSaving } =
9+
useDispatch( editorStore );
910
const { createNotice } = useDispatch( noticesStore );
1011

1112
const title = useSelect( ( select ) =>
1213
select( editorStore ).getEditedPostAttribute( 'title' )
1314
);
1415

1516
useEffect( () => {
16-
if ( title && title.length > 20 ) {
17-
editPost( { title: title.substring( 0, 20 ) } );
17+
const trimmedTitle = title?.trim() ?? '';
18+
19+
if ( trimmedTitle.length === 0 ) {
20+
lockPostSaving( 'title-empty-lock' );
21+
return;
22+
}
23+
24+
unlockPostSaving( 'title-empty-lock' );
25+
26+
editPost( { title: trimmedTitle.substring( 0, 20 ) } );
27+
28+
if ( trimmedTitle.length > 20 ) {
1829
createNotice(
1930
'warning',
2031
__( 'Title is limited to 20 characters.' ),
@@ -24,5 +35,5 @@ export const useContentModelNameLengthRestrictor = () => {
2435
}
2536
);
2637
}
27-
}, [ title, editPost, createNotice ] );
38+
}, [ title, lockPostSaving, unlockPostSaving, editPost, createNotice ] );
2839
};

0 commit comments

Comments
 (0)