Fix non-matching items and post type matching (https://github.com/woocommerce/woocommerce-admin/pull/5593)

* Don't match menu items with a non-positive score

* Match single post type page to post type menu item
This commit is contained in:
Joshua T Flowers 2020-11-11 13:16:12 -05:00 committed by GitHub
parent 7d32d6db0e
commit cae0f33e38
3 changed files with 19 additions and 6 deletions

View File

@ -47,8 +47,10 @@ const Container = ( { menuItems } ) => {
useEffect( () => {
const initialMatchedItem = getMatchingItem( menuItems );
setActiveItem( initialMatchedItem );
setActiveLevel( initialMatchedItem.parent );
if ( initialMatchedItem ) {
setActiveItem( initialMatchedItem );
setActiveLevel( initialMatchedItem.parent );
}
const removeListener = addHistoryListener( () => {
setTimeout( () => {

View File

@ -69,9 +69,19 @@ export const getMatchScore = ( location, url ) => {
}
const urlParams = getParams( urlLocation );
const locationParams = getParams( location );
let matchingParamCount = 0;
// Post type match.
if (
window.wcNavigation.postType === urlParams.post_type &&
urlPathname.indexOf( 'edit.php' ) >= 0 &&
origin === urlOrigin
) {
return Number.MAX_SAFE_INTEGER - 2;
}
// Add points for each matching param.
let matchingParamCount = 0;
const locationParams = getParams( location );
Object.keys( urlParams ).forEach( ( key ) => {
if ( urlParams[ key ] === locationParams[ key ] ) {
matchingParamCount++;
@ -139,11 +149,11 @@ export const getMatchingItem = ( items ) => {
window.location,
getAdminLink( item.url )
);
if ( score >= highestMatch ) {
if ( score >= highestMatch && score > 0 ) {
matchedItem = item;
highestMatch = score;
}
} );
return matchedItem ? matchedItem : null;
return matchedItem || null;
};

View File

@ -538,6 +538,7 @@ class Menu {
$data = array(
'menuItems' => self::get_prepared_menu_item_data(),
'postType' => isset( $_GET['post'] ) ? get_post_type( $_GET['post'] ) : null,
);
$paul = wp_add_inline_script( WC_ADMIN_APP, 'window.wcNavigation = ' . wp_json_encode( $data ), 'before' );