diff --git a/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal.js b/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal.js index cd85db26bf7ece..15d81dc6e77287 100644 --- a/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal.js +++ b/packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal.js @@ -16,27 +16,38 @@ import { TextControl, } from '@wordpress/components'; -function AddCustomGenericTemplateModal( { onClose, createTemplate } ) { +/** + * Internal dependencies + */ +import TemplateActionsLoadingScreen from './template-actions-loading-screen'; + +function AddCustomGenericTemplateModal( { + onClose, + createTemplate, + isCreatingTemplate, +} ) { const [ title, setTitle ] = useState( '' ); const defaultTitle = __( 'Custom Template' ); const [ isBusy, setIsBusy ] = useState( false ); async function onCreateTemplate( event ) { event.preventDefault(); - if ( isBusy ) { return; } - setIsBusy( true ); - - createTemplate( - { - slug: - 'wp-custom-template-' + kebabCase( title || defaultTitle ), - title: title || defaultTitle, - }, - false - ); + try { + await createTemplate( + { + slug: + 'wp-custom-template-' + + kebabCase( title || defaultTitle ), + title: title || defaultTitle, + }, + false + ); + } finally { + setIsBusy( false ); + } } return ( + { isCreatingTemplate && }
diff --git a/packages/edit-site/src/components/add-new-template/add-custom-template-modal.js b/packages/edit-site/src/components/add-new-template/add-custom-template-modal.js index 3b425966b4b12d..e686e612405269 100644 --- a/packages/edit-site/src/components/add-new-template/add-custom-template-modal.js +++ b/packages/edit-site/src/components/add-new-template/add-custom-template-modal.js @@ -21,6 +21,7 @@ import { useEntityRecords } from '@wordpress/core-data'; /** * Internal dependencies */ +import TemplateActionsLoadingScreen from './template-actions-loading-screen'; import { mapToIHasNameAndId } from './utils'; const EMPTY_ARRAY = []; @@ -160,7 +161,12 @@ function SuggestionList( { entityForSuggestions, onSelect } ) { ); } -function AddCustomTemplateModal( { onClose, onSelect, entityForSuggestions } ) { +function AddCustomTemplateModal( { + onClose, + onSelect, + entityForSuggestions, + isCreatingTemplate, +} ) { const [ showSearchEntities, setShowSearchEntities ] = useState( entityForSuggestions.hasGeneralTemplate ); @@ -176,6 +182,7 @@ function AddCustomTemplateModal( { onClose, onSelect, entityForSuggestions } ) { closeLabel={ __( 'Close' ) } onRequestClose={ onClose } > + { isCreatingTemplate && } { ! showSearchEntities && ( <>

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..c88134f41072c7 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 @@ -44,6 +44,7 @@ import { usePostTypeArchiveMenuItems, } from './utils'; import AddCustomGenericTemplateModal from './add-custom-generic-template-modal'; +import TemplateActionsLoadingScreen from './template-actions-loading-screen'; import { useHistory } from '../routes'; import { store as editSiteStore } from '../../store'; @@ -86,6 +87,7 @@ export default function NewTemplate( { postType } ) { setShowCustomGenericTemplateModal, ] = useState( false ); const [ entityForSuggestions, setEntityForSuggestions ] = useState( {} ); + const [ isCreatingTemplate, setIsCreatingTemplate ] = useState( false ); const history = useHistory(); const { saveEntityRecord } = useDispatch( coreStore ); @@ -94,6 +96,10 @@ export default function NewTemplate( { postType } ) { const { setTemplate } = useDispatch( editSiteStore ); async function createTemplate( template, isWPSuggestion = true ) { + if ( isCreatingTemplate ) { + return; + } + setIsCreatingTemplate( true ); try { const { title, description, slug, templatePrefix } = template; let templateContent = template.content; @@ -151,6 +157,8 @@ export default function NewTemplate( { postType } ) { createErrorNotice( errorMessage, { type: 'snackbar', } ); + } finally { + setIsCreatingTemplate( false ); } } @@ -176,53 +184,60 @@ export default function NewTemplate( { postType } ) { } } > { () => ( - - - { missingTemplates.map( ( template ) => { - const { - title, - description, - slug, - onClick, - icon, - } = template; - return ( - - onClick - ? onClick( template ) - : createTemplate( template ) - } - > - { title } - - ); - } ) } - - - - setShowCustomGenericTemplateModal( true ) - } - > - { __( 'Custom template' ) } - - - + <> + { isCreatingTemplate && ( + + ) } + + + { missingTemplates.map( ( template ) => { + const { + title, + description, + slug, + onClick, + icon, + } = template; + return ( + + onClick + ? onClick( template ) + : createTemplate( template ) + } + > + { title } + + ); + } ) } + + + + setShowCustomGenericTemplateModal( + true + ) + } + > + { __( 'Custom template' ) } + + + + ) } { showCustomTemplateModal && ( @@ -230,12 +245,14 @@ export default function NewTemplate( { postType } ) { onClose={ () => setShowCustomTemplateModal( false ) } onSelect={ createTemplate } entityForSuggestions={ entityForSuggestions } + isCreatingTemplate={ isCreatingTemplate } /> ) } { showCustomGenericTemplateModal && ( setShowCustomGenericTemplateModal( false ) } createTemplate={ createTemplate } + isCreatingTemplate={ isCreatingTemplate } /> ) } diff --git a/packages/edit-site/src/components/add-new-template/style.scss b/packages/edit-site/src/components/add-new-template/style.scss index 46ec1f28fa6084..51fb9af347c2ce 100644 --- a/packages/edit-site/src/components/add-new-template/style.scss +++ b/packages/edit-site/src/components/add-new-template/style.scss @@ -158,3 +158,22 @@ .edit-site-custom-generic-template__modal-actions { margin-top: $grid-unit-15; } + +.edit-site-template-actions-loading-screen-modal { + backdrop-filter: none; + background-color: transparent; + + &.is-full-screen { + background-color: $white; + box-shadow: 0 0 0 transparent; + min-width: 100%; + min-height: 100%; + } + + &__content { + display: flex; + align-items: center; + justify-content: center; + height: 100%; + } +} diff --git a/packages/edit-site/src/components/add-new-template/template-actions-loading-screen.js b/packages/edit-site/src/components/add-new-template/template-actions-loading-screen.js new file mode 100644 index 00000000000000..a57880d8551f7c --- /dev/null +++ b/packages/edit-site/src/components/add-new-template/template-actions-loading-screen.js @@ -0,0 +1,23 @@ +/** + * WordPress dependencies + */ +import { Spinner, Modal } from '@wordpress/components'; + +export default function TemplateActionsLoadingScreen() { + const baseCssClass = 'edit-site-template-actions-loading-screen-modal'; + return ( + {} } + __experimentalHideHeader + className={ baseCssClass } + > +

+ +
+ + ); +}