Skip to content

Commit

Permalink
Site Editor: Display a notice if export fails (#37131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Dec 7, 2021
1 parent bd81e9c commit cc43fbb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
34 changes: 2 additions & 32 deletions packages/edit-site/src/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
/**
* External dependencies
*/
import downloadjs from 'downloadjs';

/**
* WordPress dependencies
*/
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
import apiFetch from '@wordpress/api-fetch';
import { download } from '@wordpress/icons';

/**
* Internal dependencies
*/
import ToolsMoreMenuGroup from '../components/header/tools-more-menu-group';
import SiteExport from './site-export';
import WelcomeGuideMenuItem from './welcome-guide-menu-item';

registerPlugin( 'edit-site', {
render() {
return (
<>
<ToolsMoreMenuGroup>
<MenuItem
role="menuitem"
icon={ download }
onClick={ () =>
apiFetch( {
path: '/wp-block-editor/v1/export',
parse: false,
} )
.then( ( res ) => res.blob() )
.then( ( blob ) =>
downloadjs(
blob,
'edit-site-export.zip',
'application/zip'
)
)
}
info={ __(
'Download your templates and template parts.'
) }
>
{ __( 'Export' ) }
</MenuItem>
<SiteExport />
<WelcomeGuideMenuItem />
</ToolsMoreMenuGroup>
</>
Expand Down
48 changes: 48 additions & 0 deletions packages/edit-site/src/plugins/site-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* External dependencies
*/
import downloadjs from 'downloadjs';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { download } from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

export default function SiteExport() {
const { createErrorNotice } = useDispatch( noticesStore );

async function handleExport() {
try {
const response = await apiFetch( {
path: '/wp-block-editor/v1/export',
parse: false,
} );
const blob = await response.blob();

downloadjs( blob, 'edit-site-export.zip', 'application/zip' );
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the site export.' );

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

return (
<MenuItem
role="menuitem"
icon={ download }
onClick={ handleExport }
info={ __( 'Download your templates and template parts.' ) }
>
{ __( 'Export' ) }
</MenuItem>
);
}

0 comments on commit cc43fbb

Please sign in to comment.