diff --git a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/index.js b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/index.js index a533c40d0c4efb..392238d006e970 100644 --- a/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/index.js +++ b/apps/full-site-editing/full-site-editing-plugin/starter-page-templates/page-template-modal/index.js @@ -4,10 +4,11 @@ import { isEmpty, reduce } from 'lodash'; import { __, sprintf } from '@wordpress/i18n'; import { compose } from '@wordpress/compose'; -import { Button, Modal } from '@wordpress/components'; +import { Button, Modal, Spinner } from '@wordpress/components'; import { registerPlugin } from '@wordpress/plugins'; import { withDispatch, withSelect } from '@wordpress/data'; import { Component } from '@wordpress/element'; +import ensureAssets from './utils/ensure-assets'; import '@wordpress/nux'; /** @@ -35,6 +36,8 @@ class PageTemplateModal extends Component { slug: '', title: '', blocks: {}, + error: null, + isOpen: false, }; constructor( props ) { @@ -64,19 +67,38 @@ class PageTemplateModal extends Component { } setTemplate = ( slug, title ) => { - this.setState( { isOpen: false } ); - trackSelection( this.props.segment.id, this.props.vertical.id, slug ); + this.setState( { + error: null, + isLoading: true, + } ); - this.props.saveTemplateChoice( slug ); + trackSelection( this.props.segment.id, this.props.vertical.id, slug ); - const previewBlocks = this.state.blocks[ slug ]; + const blocks = this.state.blocks[ slug ]; // Skip inserting if there's nothing to insert. - if ( ! previewBlocks || ! previewBlocks.length ) { + if ( ! blocks || ! blocks.length ) { return; } - this.props.insertTemplate( title, previewBlocks ); + // Make sure all blocks use local assets before inserting. + ensureAssets( blocks ) + .then( blocksWithAssets => { + // Don't insert anything if the user clicked Cancel/Close + // before we loaded everything. + if ( ! this.state.isOpen ) { + return; + } + this.props.saveTemplateChoice( slug ); + this.props.insertTemplate( title, blocksWithAssets ); + this.setState( { isOpen: false } ); + } ) + .catch( error => { + this.setState( { + isLoading: false, + error, + } ); + } ); }; handleConfirmation = () => this.setTemplate( this.state.slug, this.state.title ); @@ -123,22 +145,31 @@ class PageTemplateModal extends Component { overlayClassName="page-template-modal-screen-overlay" >