2020-10-13 01:40:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-11-12 17:10:42 +00:00
|
|
|
import { useEffect, useMemo, useState, useRef } from '@wordpress/element';
|
2021-02-08 20:41:08 +00:00
|
|
|
import classnames from 'classnames';
|
2020-10-13 01:40:53 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2021-02-22 18:54:27 +00:00
|
|
|
import { Navigation } from '@woocommerce/experimental';
|
|
|
|
import { NAVIGATION_STORE_NAME, useUser } from '@woocommerce/data';
|
2020-12-02 18:16:54 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2020-10-13 01:40:53 +00:00
|
|
|
import { withSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-02-22 18:54:27 +00:00
|
|
|
import {
|
|
|
|
addHistoryListener,
|
|
|
|
getMappedItemsCategories,
|
|
|
|
getMatchingItem,
|
|
|
|
} from '../../utils';
|
2020-10-13 01:40:53 +00:00
|
|
|
import Header from '../header';
|
2021-02-22 18:54:27 +00:00
|
|
|
import { PrimaryMenu } from './primary-menu';
|
|
|
|
import { SecondaryMenu } from './secondary-menu';
|
2021-02-18 18:06:28 +00:00
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
const Container = ( { menuItems } ) => {
|
|
|
|
useEffect( () => {
|
|
|
|
// Collapse the original WP Menu.
|
2021-02-12 16:32:26 +00:00
|
|
|
document.documentElement.classList.remove( 'wp-toolbar' );
|
2021-02-12 21:42:04 +00:00
|
|
|
document.body.classList.add( 'has-woocommerce-navigation' );
|
2020-10-13 01:40:53 +00:00
|
|
|
const adminMenu = document.getElementById( 'adminmenumain' );
|
2021-02-12 16:32:26 +00:00
|
|
|
|
|
|
|
if ( ! adminMenu ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
adminMenu.classList.add( 'folded' );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const [ activeItem, setActiveItem ] = useState( 'woocommerce-home' );
|
|
|
|
const [ activeLevel, setActiveLevel ] = useState( 'woocommerce' );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const initialMatchedItem = getMatchingItem( menuItems );
|
2021-02-16 16:12:25 +00:00
|
|
|
if ( initialMatchedItem && activeItem !== initialMatchedItem ) {
|
2020-11-11 18:16:12 +00:00
|
|
|
setActiveItem( initialMatchedItem );
|
|
|
|
setActiveLevel( initialMatchedItem.parent );
|
|
|
|
}
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
const removeListener = addHistoryListener( () => {
|
|
|
|
setTimeout( () => {
|
|
|
|
const matchedItem = getMatchingItem( menuItems );
|
|
|
|
if ( matchedItem ) {
|
|
|
|
setActiveItem( matchedItem );
|
|
|
|
}
|
|
|
|
}, 0 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return removeListener;
|
|
|
|
}, [ menuItems ] );
|
|
|
|
|
2021-02-22 18:54:27 +00:00
|
|
|
const { currentUserCan } = useUser();
|
|
|
|
|
|
|
|
const { categories, items } = useMemo(
|
|
|
|
() => getMappedItemsCategories( menuItems, currentUserCan ),
|
|
|
|
[ menuItems, currentUserCan ]
|
2020-10-21 17:01:46 +00:00
|
|
|
);
|
|
|
|
|
2020-11-12 17:10:42 +00:00
|
|
|
const navDomRef = useRef( null );
|
|
|
|
|
2021-02-22 18:54:27 +00:00
|
|
|
const onBackClick = ( id ) => {
|
2020-12-02 18:16:54 +00:00
|
|
|
recordEvent( 'navigation_back_click', {
|
|
|
|
category: id,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-02-08 20:41:08 +00:00
|
|
|
const isRoot = activeLevel === 'woocommerce';
|
|
|
|
|
|
|
|
const classes = classnames( 'woocommerce-navigation', {
|
|
|
|
'is-root': isRoot,
|
|
|
|
} );
|
2021-01-04 17:00:42 +00:00
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
return (
|
2021-02-08 20:41:08 +00:00
|
|
|
<div className={ classes }>
|
2020-10-13 01:40:53 +00:00
|
|
|
<Header />
|
2020-11-12 17:10:42 +00:00
|
|
|
<div className="woocommerce-navigation__wrapper" ref={ navDomRef }>
|
|
|
|
<Navigation
|
|
|
|
activeItem={ activeItem ? activeItem.id : null }
|
|
|
|
activeMenu={ activeLevel }
|
|
|
|
onActivateMenu={ ( ...args ) => {
|
|
|
|
if ( navDomRef && navDomRef.current ) {
|
|
|
|
navDomRef.current.scrollTop = 0;
|
|
|
|
}
|
2020-11-06 20:52:01 +00:00
|
|
|
|
2020-11-12 17:10:42 +00:00
|
|
|
setActiveLevel( ...args );
|
|
|
|
} }
|
|
|
|
>
|
2021-02-22 18:54:27 +00:00
|
|
|
{ Object.values( categories ).map( ( category ) => {
|
|
|
|
const categoryItems = items[ category.id ];
|
2021-02-16 16:12:25 +00:00
|
|
|
|
2021-02-22 18:54:27 +00:00
|
|
|
return (
|
|
|
|
!! categoryItems && [
|
|
|
|
<PrimaryMenu
|
2021-02-08 20:41:08 +00:00
|
|
|
key={ category.id }
|
2021-02-22 18:54:27 +00:00
|
|
|
category={ category }
|
|
|
|
onBackClick={ onBackClick }
|
|
|
|
primaryItems={ [
|
|
|
|
...categoryItems.primary,
|
|
|
|
...categoryItems.favorites,
|
|
|
|
] }
|
|
|
|
pluginItems={ categoryItems.plugins }
|
|
|
|
/>,
|
|
|
|
<SecondaryMenu
|
2021-02-08 20:41:08 +00:00
|
|
|
key={ `secondary/${ category.id }` }
|
2021-02-22 18:54:27 +00:00
|
|
|
category={ category }
|
|
|
|
onBackClick={ onBackClick }
|
|
|
|
items={ categoryItems.secondary }
|
|
|
|
/>,
|
|
|
|
]
|
|
|
|
);
|
2020-11-12 17:10:42 +00:00
|
|
|
} ) }
|
|
|
|
</Navigation>
|
|
|
|
</div>
|
2020-10-13 01:40:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
|
|
|
const { getActiveItem, getMenuItems } = select( NAVIGATION_STORE_NAME );
|
|
|
|
|
|
|
|
return {
|
|
|
|
activeItem: getActiveItem(),
|
|
|
|
menuItems: getMenuItems(),
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Container );
|