Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix updating state in unmounted components
  • Loading branch information
kevin940726 committed Dec 6, 2021
commit 3964bc3d9af86bf6f3721e6591e4c05c0ca05a69
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function NewTemplatePart( { postType } ) {
const { saveEntityRecord } = useDispatch( coreStore );
const { getLastEntitySaveError } = useSelect( coreStore );

async function createTemplatePart( { title, area } ) {
async function createTemplatePart( { title, area }, { closeModal } ) {
if ( ! title ) {
createErrorNotice( __( 'Title is not defined.' ), {
type: 'snackbar',
Expand Down Expand Up @@ -55,6 +55,8 @@ export default function NewTemplatePart( { postType } ) {
throw lastEntitySaveError;
}

closeModal();

// Navigate to the created template part editor.
history.push( {
postId: templatePart.id,
Expand All @@ -71,6 +73,8 @@ export default function NewTemplatePart( { postType } ) {
);

createErrorNotice( errorMessage, { type: 'snackbar' } );

closeModal();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export default function CreateTemplatePartModal( { closeModal, onCreate } ) {
return;
}
setIsSubmitting( true );
await onCreate( { title, area } );
setIsSubmitting( false );
closeModal();
await onCreate( { title, area }, { closeModal } );
} }
>
<TextControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function EditTemplatePartMenuItem( { selectedClientId, onClose } ) {

const linkProps = useLink(
{
postId: selectedTemplatePart.id,
postType: selectedTemplatePart.type,
postId: selectedTemplatePart?.id,
postType: selectedTemplatePart?.type,
},
{
fromTemplateId: params.postId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ export default function ConvertToTemplatePart( { clientIds, blocks } ) {
createSuccessNotice( __( 'Template part created.' ), {
type: 'snackbar',
} );

// The modal and this component will be unmounted because of `replaceBlocks` above,
// so no need to call `closeModal` or `onClose`.
};

return (
<>
<BlockSettingsMenuControls>
{ ( { onClose } ) => (
{ () => (
<MenuItem
onClick={ () => {
setIsModalOpen( true );
onClose();
} }
>
{ __( 'Make template part' ) }
Expand Down