Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add initial error handling for conversion process
  • Loading branch information
getdave committed Mar 3, 2022
commit 8161482568ffaa0017a17d81711983c9e5debd6d
16 changes: 15 additions & 1 deletion packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,17 @@ function Navigation( {

useEffect( () => {
if (
! classicMenuConversionState?.isFetching &&
classicMenuConversionState?.status === 'success' &&
classicMenuConversionState.navMenu
) {
handleUpdateMenu( classicMenuConversionState.navMenu?.id );
hideClassicMenuConversionErrorNotice();
}

if ( classicMenuConversionState?.status === 'error' ) {
showClassicMenuConversionErrorNotice(
classicMenuConversionState.error
);
}
}, [ classicMenuConversionState ] );

Expand Down Expand Up @@ -386,6 +393,13 @@ function Navigation( {
}
);

const [
showClassicMenuConversionErrorNotice,
hideClassicMenuConversionErrorNotice,
] = useNavigationNotice( {
name: 'block-library/core/navigation/classic-menu-conversion/error',
} );

useEffect( () => {
if ( ! isSelected && ! isInnerBlockSelected ) {
hideCantEditNotice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ function useConvertClassicToBlockMenu( clientId ) {
.resolveSelect( coreStore )
.getMenuItems( menuItemsParameters );

// Offline response results in `null`
if ( classicMenuItems === null ) {
throw new Error(
// TODO - i18n
`Unable to fetch classic menu "${ menuName }" from API.`,
{
menuId,
menuName,
}
);
}

// 2. Convert the classic items into blocks.
const { innerBlocks } = menuItemsToBlocks( classicMenuItems );

Expand Down Expand Up @@ -109,9 +121,10 @@ function useConvertClassicToBlockMenu( clientId ) {
navMenu,
} );
} )
.catch( () => {
.catch( ( e ) => {
safeDispatch( {
type: 'ERROR',
error: e?.message,
} );
} );
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { useRef } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { store as noticeStore } from '@wordpress/notices';

function useNavigationNotice( { name, message } = {} ) {
function useNavigationNotice( { name, message = '' } = {} ) {
const noticeRef = useRef();

const { createWarningNotice, removeNotice } = useDispatch( noticeStore );

const showNotice = () => {
const showNotice = ( customMsg ) => {
if ( noticeRef.current ) {
return;
}

noticeRef.current = name;

createWarningNotice( message, {
createWarningNotice( customMsg || message, {
id: noticeRef.current,
type: 'snackbar',
} );
Expand Down