-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Display a notice if export fails (#37131)
- Loading branch information
1 parent
5b482fe
commit e67a939
Showing
2 changed files
with
50 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |