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';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
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';
|
2021-03-05 14:35:53 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2021-04-02 21:35:31 +00:00
|
|
|
import { addHistoryListener } from '@woocommerce/navigation';
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-02 21:35:31 +00:00
|
|
|
import { 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
|
|
|
|
2021-03-05 14:35:53 +00:00
|
|
|
const Container = () => {
|
|
|
|
const { menuItems } = useSelect( ( select ) => {
|
|
|
|
return {
|
|
|
|
menuItems: select( NAVIGATION_STORE_NAME ).getMenuItems(),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
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 );
|
2021-03-05 14:35:53 +00:00
|
|
|
setActiveLevel( matchedItem.parent );
|
2020-10-13 01:40:53 +00:00
|
|
|
}
|
|
|
|
}, 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';
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const classes = clsx( 'woocommerce-navigation', {
|
2021-02-08 20:41:08 +00:00
|
|
|
'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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-05 14:35:53 +00:00
|
|
|
export default Container;
|