11import { parseEntities } from 'parse-entities' ;
2- import { withSelect , withDispatch } from '@wordpress/data' ;
3- import { Modal , Button , Icon } from '@wordpress/components' ;
2+ import { withDispatch , withSelect } from '@wordpress/data' ;
3+ import { Button , Icon , Modal } from '@wordpress/components' ;
44import { __ , sprintf } from '@wordpress/i18n' ;
55import { compose } from '@wordpress/compose' ;
66import { page as pageIcon } from '@wordpress/icons' ;
77import { useEffect , useState } from '@wordpress/element' ;
88
99import {
10+ activateTheme ,
1011 importTemplates ,
12+ importFseTemplate ,
1113 installTheme ,
12- activateTheme ,
1314} from '../../utils/site-import' ;
1415import { fetchBulkData , getUserTemplateData } from './common' ;
1516import classnames from 'classnames' ;
@@ -71,6 +72,7 @@ const ImportTemplatesModal = ( {
7172 < >
7273 < div className = "modal-body" >
7374 < div className = "header" >
75+ { /* eslint-disable-next-line jsx-a11y/heading-has-content */ }
7476 < h1
7577 className = "is-loading"
7678 style = { {
@@ -108,14 +110,39 @@ const ImportTemplatesModal = ( {
108110
109111 const runTemplatesImport = ( ) => {
110112 setImporting ( true ) ;
111- const data = templatesData . map ( ( item , index ) => {
112- return { ...item , ...templates [ index ] } ;
113- } ) ;
114113
114+ /**
115+ * Please note that templatesData is an array of objects, but currently we only send one template at a time.
116+ * In the future we might send more than one template to bulk-import templates.
117+ */
118+ const { fseTemplates, otherTemplates } = templatesData . reduce (
119+ ( acc , item , index ) => {
120+ const templateType = item . template_type ;
121+ const mergedTemplate = { ...item , ...templates [ index ] } ;
122+
123+ if ( templateType === 'fse' ) {
124+ acc . fseTemplates . push ( mergedTemplate ) ;
125+ } else {
126+ acc . otherTemplates . push ( mergedTemplate ) ;
127+ }
128+
129+ return acc ;
130+ } ,
131+ { fseTemplates : [ ] , otherTemplates : [ ] }
132+ ) ;
133+
134+ /**
135+ * Function to import templates that are not FSE.
136+ */
115137 const callbackImportTemplate = ( ) => {
138+ if ( ! otherTemplates ) {
139+ return ;
140+ }
141+
116142 try {
117- importTemplates ( data ) . then ( ( r ) => {
143+ importTemplates ( otherTemplates ) . then ( ( r ) => {
118144 if ( ! r . success ) {
145+ // eslint-disable-next-line no-console
119146 console . log ( r . message ) ;
120147 return false ;
121148 }
@@ -124,26 +151,54 @@ const ImportTemplatesModal = ( {
124151 setImporting ( 'done' ) ;
125152 } ) ;
126153 } catch ( e ) {
154+ // eslint-disable-next-line no-console
155+ console . log ( e ) ;
156+ }
157+ } ;
158+
159+ /**
160+ * Function to import FSE templates.
161+ */
162+ const callbackImportFse = ( ) => {
163+ if ( ! fseTemplates ) {
164+ return ;
165+ }
166+
167+ try {
168+ importFseTemplate ( fseTemplates ) . then ( ( r ) => {
169+ if ( ! r . success ) {
170+ // eslint-disable-next-line no-console
171+ setError ( r . message ) ;
172+ return false ;
173+ }
174+
175+ // setImported( r.pages );
176+ setImporting ( 'done' ) ;
177+ } ) ;
178+ } catch ( e ) {
179+ // eslint-disable-next-line no-console
127180 console . log ( e ) ;
128181 }
129182 } ;
130183
131184 if (
132185 ! themeData ||
133- ( data [ 0 ] . template_site_slug === 'general' &&
134- data [ 0 ] . premade === 'yes' )
186+ ( otherTemplates [ 0 ] ? .template_site_slug === 'general' &&
187+ otherTemplates [ 0 ] ? .premade === 'yes' )
135188 ) {
136189 callbackImportTemplate ( ) ;
137190 return false ;
138191 }
139192
140193 const callbackError = ( err ) => {
194+ // eslint-disable-next-line no-console
141195 console . error ( err ) ;
142196 } ;
143197
144198 // skip activation or install for user templates
145199 if ( isUserTemplate ) {
146200 callbackImportTemplate ( ) ;
201+ callbackImportFse ( ) ;
147202 return false ;
148203 }
149204
@@ -270,33 +325,39 @@ const ImportTemplatesModal = ( {
270325 } ;
271326
272327 const description = ( ) => {
273- const map = {
274- strong : < strong > { __ ( 'does not' ) } </ strong > ,
275- } ;
328+ const {
329+ template_name : templateName ,
330+ template_type : templateType ,
331+ } = templatesData [ 0 ] ;
276332
277- const text = isSingle
333+ if ( templateType === 'fse' ) {
334+ return __ (
335+ 'This import will add a new Full Site Editing template to your site.' ,
336+ 'templates-patterns-collection'
337+ ) ;
338+ }
339+
340+ return isSingle
278341 ? sprintf (
279- /* translators: %s the name of the template */
280- __ (
281- 'The %s template will be imported as a page into your site. This import will install & activate the page builder plugin if not already installed.' ,
342+ /* translators: %s the name of the template */
343+ __ (
344+ 'The %s template will be imported as a page into your site. This import will install & activate the page builder plugin if not already installed.' ,
282345 'templates-patterns-collection'
283- ) ,
284- templatesData [ 0 ] . template_name
346+ ) ,
347+ templateName
285348 )
286349 : __ (
287350 'All the templates that are included in this starter site, will be imported as pages. This import will install & activate the page builder plugin if not already installed.' ,
288351 'templates-patterns-collection'
289352 ) ;
290-
291- return text ;
292353 } ;
293354
294- const ModalContent = ( ) => {
355+ const ModalContent = ( props ) => {
295356 if ( fetching ) {
296357 return < Mock /> ;
297358 }
298359
299- if ( error ) {
360+ if ( props . error ) {
300361 return < Error /> ;
301362 }
302363
@@ -367,7 +428,11 @@ const ImportTemplatesModal = ( {
367428 ( ! importing || importing === 'done' ) && ! fetching
368429 }
369430 >
370- { importing === 'done' ? < ImportDone /> : < ModalContent /> }
431+ { importing === 'done' && ! error ? (
432+ < ImportDone />
433+ ) : (
434+ < ModalContent error = { error } />
435+ ) }
371436 </ Modal >
372437 ) ;
373438} ;
0 commit comments