From f020d446680b0ad881716cf67dad6777f24657eb Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 14 Sep 2022 11:13:45 +0300 Subject: [PATCH] [Site Editor]: Fix multiple templates creation while in the process of creating one --- .../components/add-new-template/new-template.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js index ea7a1a840024e3..f4fb97a5d4bc6a 100644 --- a/packages/edit-site/src/components/add-new-template/new-template.js +++ b/packages/edit-site/src/components/add-new-template/new-template.js @@ -9,7 +9,7 @@ import { MenuItem, NavigableMenu, } from '@wordpress/components'; -import { useState } from '@wordpress/element'; +import { useState, useRef } from '@wordpress/element'; import { useDispatch } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { @@ -86,14 +86,24 @@ export default function NewTemplate( { postType } ) { setShowCustomGenericTemplateModal, ] = useState( false ); const [ entityForSuggestions, setEntityForSuggestions ] = useState( {} ); + const isCreatingTemplate = useRef(); const history = useHistory(); const { saveEntityRecord } = useDispatch( coreStore ); - const { createErrorNotice, createSuccessNotice } = + const { createErrorNotice, createSuccessNotice, createInfoNotice } = useDispatch( noticesStore ); const { setTemplate } = useDispatch( editSiteStore ); async function createTemplate( template, isWPSuggestion = true ) { + if ( isCreatingTemplate.current ) { + return; + } + isCreatingTemplate.current = true; + createInfoNotice( + // translators: displayed right after clicking the menu item to create a new template in site editor. + __( 'Template creation in process.' ), + { type: 'snackbar' } + ); try { const { title, description, slug, templatePrefix } = template; let templateContent = template.content; @@ -151,6 +161,8 @@ export default function NewTemplate( { postType } ) { createErrorNotice( errorMessage, { type: 'snackbar', } ); + } finally { + isCreatingTemplate.current = false; } }