Skip to content

Commit

Permalink
delete via entity delete
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed May 22, 2020
1 parent bcd4a59 commit dcb6b9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function DeleteMenuButton( { menuId, onDelete } ) {
const deleteMenu = async ( recordId ) => {
const path = `/__experimental/menus/${ recordId }?force=true`;
const deletedRecord = await apiFetch( {
path,
method: 'DELETE',
} );
return deletedRecord.previous;
};

export default function DeleteMenuButton( { onDelete } ) {
const askToDelete = async () => {
if (
// eslint-disable-next-line no-alert
window.confirm(
__( 'Are you sure you want to delete this navigation?' )
)
) {
const deletedMenu = await deleteMenu( menuId );
onDelete( deletedMenu.id );
onDelete();
}
};

Expand Down
25 changes: 8 additions & 17 deletions packages/edit-navigation/src/components/menus-editor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import {
Button,
Expand Down Expand Up @@ -31,20 +31,19 @@ export default function MenusEditor( { blockEditorSettings } ) {
false
);

const { deleteMenu } = useDispatch( 'core' );

useEffect( () => {
if ( ! hasCompletedFirstLoad && hasLoadedMenus ) {
setHasCompletedFirstLoad( true );
}
}, [ hasLoadedMenus ] );

const [ menuId, setMenuId ] = useState();
const [ stateMenus, setStateMenus ] = useState();
const [ showCreateMenuPanel, setShowCreateMenuPanel ] = useState( false );

useEffect( () => {
if ( menus?.length ) {
setStateMenus( menus );

// Only set menuId if it's currently unset.
if ( ! menuId ) {
setMenuId( menus[ 0 ].id );
Expand All @@ -56,7 +55,7 @@ export default function MenusEditor( { blockEditorSettings } ) {
return <Spinner />;
}

const hasMenus = !! stateMenus?.length;
const hasMenus = !! menus?.length;
const isCreateMenuPanelVisible =
hasCompletedFirstLoad && ( ! hasMenus || showCreateMenuPanel );

Expand All @@ -74,7 +73,7 @@ export default function MenusEditor( { blockEditorSettings } ) {
<SelectControl
className="edit-navigation-menus-editor__menu-select-control"
label={ __( 'Select navigation to edit:' ) }
options={ stateMenus?.map( ( menu ) => ( {
options={ menus?.map( ( menu ) => ( {
value: menu.id,
label: menu.name,
} ) ) }
Expand All @@ -95,7 +94,7 @@ export default function MenusEditor( { blockEditorSettings } ) {
</Card>
{ isCreateMenuPanelVisible && (
<CreateMenuPanel
menus={ stateMenus }
menus={ menus }
onCancel={
// User can only cancel out of menu creation if there
// are other menus to fall back to showing.
Expand All @@ -113,16 +112,8 @@ export default function MenusEditor( { blockEditorSettings } ) {
<MenuEditor
menuId={ menuId }
blockEditorSettings={ blockEditorSettings }
onDeleteMenu={ ( deletedMenu ) => {
const newStateMenus = stateMenus.filter( ( menu ) => {
return menu.id !== deletedMenu;
} );
setStateMenus( newStateMenus );
if ( newStateMenus.length ) {
setMenuId( newStateMenus[ 0 ].id );
} else {
setMenuId();
}
onDeleteMenu={ async () => {
await deleteMenu( menuId, '' );
} }
/>
) }
Expand Down

0 comments on commit dcb6b9e

Please sign in to comment.