Migrate @woocommerce/data navigation store to TS (#34239)
* Migrate woo data navigation store to TS * Add changelog * Fix action type * Update packages/js/data/src/navigation/reducer.ts Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add a doc comment to MenuItem type Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
This commit is contained in:
parent
a6d656f363
commit
02b47a67db
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: dev
|
||||||
|
|
||||||
|
Migrate navigation store to TS
|
|
@ -11,6 +11,6 @@ const TYPES = {
|
||||||
REMOVE_FAVORITE_FAILURE: 'REMOVE_FAVORITE_FAILURE',
|
REMOVE_FAVORITE_FAILURE: 'REMOVE_FAVORITE_FAILURE',
|
||||||
REMOVE_FAVORITE_REQUEST: 'REMOVE_FAVORITE_REQUEST',
|
REMOVE_FAVORITE_REQUEST: 'REMOVE_FAVORITE_REQUEST',
|
||||||
REMOVE_FAVORITE_SUCCESS: 'REMOVE_FAVORITE_SUCCESS',
|
REMOVE_FAVORITE_SUCCESS: 'REMOVE_FAVORITE_SUCCESS',
|
||||||
};
|
} as const;
|
||||||
|
|
||||||
export default TYPES;
|
export default TYPES;
|
|
@ -9,50 +9,51 @@ import { getPersistedQuery } from '@woocommerce/navigation';
|
||||||
*/
|
*/
|
||||||
import TYPES from './action-types';
|
import TYPES from './action-types';
|
||||||
import { WC_ADMIN_NAMESPACE } from '../constants';
|
import { WC_ADMIN_NAMESPACE } from '../constants';
|
||||||
|
import { MenuItem } from './types';
|
||||||
|
|
||||||
export function setMenuItems( menuItems ) {
|
export function setMenuItems( menuItems: MenuItem[] ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.SET_MENU_ITEMS,
|
type: TYPES.SET_MENU_ITEMS,
|
||||||
menuItems,
|
menuItems,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addMenuItems( menuItems ) {
|
export function addMenuItems( menuItems: MenuItem[] ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.ADD_MENU_ITEMS,
|
type: TYPES.ADD_MENU_ITEMS,
|
||||||
menuItems,
|
menuItems,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFavoritesFailure( error ) {
|
export function getFavoritesFailure( error: unknown ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.GET_FAVORITES_FAILURE,
|
type: TYPES.GET_FAVORITES_FAILURE,
|
||||||
error,
|
error,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFavoritesRequest( favorites ) {
|
export function getFavoritesRequest( favorites?: string[] ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.GET_FAVORITES_REQUEST,
|
type: TYPES.GET_FAVORITES_REQUEST,
|
||||||
favorites,
|
favorites,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getFavoritesSuccess( favorites ) {
|
export function getFavoritesSuccess( favorites: string[] ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.GET_FAVORITES_SUCCESS,
|
type: TYPES.GET_FAVORITES_SUCCESS,
|
||||||
favorites,
|
favorites,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addFavoriteRequest( favorite ) {
|
export function addFavoriteRequest( favorite: string ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.ADD_FAVORITE_REQUEST,
|
type: TYPES.ADD_FAVORITE_REQUEST,
|
||||||
favorite,
|
favorite,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addFavoriteFailure( favorite, error ) {
|
export function addFavoriteFailure( favorite: string, error: unknown ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.ADD_FAVORITE_FAILURE,
|
type: TYPES.ADD_FAVORITE_FAILURE,
|
||||||
favorite,
|
favorite,
|
||||||
|
@ -60,21 +61,21 @@ export function addFavoriteFailure( favorite, error ) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addFavoriteSuccess( favorite ) {
|
export function addFavoriteSuccess( favorite: string ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.ADD_FAVORITE_SUCCESS,
|
type: TYPES.ADD_FAVORITE_SUCCESS,
|
||||||
favorite,
|
favorite,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeFavoriteRequest( favorite ) {
|
export function removeFavoriteRequest( favorite: string ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.REMOVE_FAVORITE_REQUEST,
|
type: TYPES.REMOVE_FAVORITE_REQUEST,
|
||||||
favorite,
|
favorite,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeFavoriteFailure( favorite, error ) {
|
export function removeFavoriteFailure( favorite: string, error: unknown ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.REMOVE_FAVORITE_FAILURE,
|
type: TYPES.REMOVE_FAVORITE_FAILURE,
|
||||||
favorite,
|
favorite,
|
||||||
|
@ -82,18 +83,13 @@ export function removeFavoriteFailure( favorite, error ) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeFavoriteSuccess( favorite, error ) {
|
export function removeFavoriteSuccess( favorite: string ) {
|
||||||
return {
|
return {
|
||||||
type: TYPES.REMOVE_FAVORITE_SUCCESS,
|
type: TYPES.REMOVE_FAVORITE_SUCCESS,
|
||||||
favorite,
|
favorite,
|
||||||
error,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* onLoad() {
|
|
||||||
yield onHistoryChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function* onHistoryChange() {
|
export function* onHistoryChange() {
|
||||||
const persistedQuery = getPersistedQuery();
|
const persistedQuery = getPersistedQuery();
|
||||||
|
|
||||||
|
@ -107,11 +103,15 @@ export function* onHistoryChange() {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* addFavorite( favorite ) {
|
export function* onLoad() {
|
||||||
|
yield onHistoryChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function* addFavorite( favorite: string ) {
|
||||||
yield addFavoriteRequest( favorite );
|
yield addFavoriteRequest( favorite );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = yield apiFetch( {
|
const results: string[] = yield apiFetch( {
|
||||||
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
|
@ -131,11 +131,11 @@ export function* addFavorite( favorite ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* removeFavorite( favorite ) {
|
export function* removeFavorite( favorite: string ) {
|
||||||
yield removeFavoriteRequest( favorite );
|
yield removeFavoriteRequest( favorite );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = yield apiFetch( {
|
const results: string[] = yield apiFetch( {
|
||||||
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
data: {
|
data: {
|
||||||
|
@ -154,3 +154,21 @@ export function* removeFavorite( favorite ) {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Action = ReturnType<
|
||||||
|
| typeof setMenuItems
|
||||||
|
| typeof addMenuItems
|
||||||
|
| typeof getFavoritesFailure
|
||||||
|
| typeof getFavoritesRequest
|
||||||
|
| typeof getFavoritesSuccess
|
||||||
|
| typeof addFavoriteRequest
|
||||||
|
| typeof addFavoriteFailure
|
||||||
|
| typeof addFavoriteSuccess
|
||||||
|
| typeof removeFavoriteRequest
|
||||||
|
| typeof removeFavoriteFailure
|
||||||
|
| typeof removeFavoriteSuccess
|
||||||
|
| ( () => {
|
||||||
|
type: typeof TYPES.ON_HISTORY_CHANGE;
|
||||||
|
persistedQuery: ReturnType< typeof getPersistedQuery >;
|
||||||
|
} )
|
||||||
|
>;
|
|
@ -1 +1 @@
|
||||||
export const STORE_NAME = 'woocommerce-navigation';
|
export const STORE_NAME = 'woocommerce-navigation' as const;
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
/**
|
|
||||||
* External dependencies
|
|
||||||
*/
|
|
||||||
import { controls } from '@wordpress/data-controls';
|
|
||||||
import { registerStore } from '@wordpress/data';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal dependencies
|
|
||||||
*/
|
|
||||||
import { STORE_NAME } from './constants';
|
|
||||||
import * as selectors from './selectors';
|
|
||||||
import * as actions from './actions';
|
|
||||||
import reducer from './reducer';
|
|
||||||
import * as resolvers from './resolvers';
|
|
||||||
import initDispatchers from './dispatchers';
|
|
||||||
|
|
||||||
registerStore( STORE_NAME, {
|
|
||||||
reducer,
|
|
||||||
actions,
|
|
||||||
controls,
|
|
||||||
resolvers,
|
|
||||||
selectors,
|
|
||||||
} );
|
|
||||||
|
|
||||||
initDispatchers();
|
|
||||||
export const NAVIGATION_STORE_NAME = STORE_NAME;
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
import { registerStore } from '@wordpress/data';
|
||||||
|
import { controls } from '@wordpress/data-controls';
|
||||||
|
import { SelectFromMap, DispatchFromMap } from '@automattic/data-stores';
|
||||||
|
import { Reducer, AnyAction } from 'redux';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import { STORE_NAME } from './constants';
|
||||||
|
import * as selectors from './selectors';
|
||||||
|
import * as actions from './actions';
|
||||||
|
import reducer, { State } from './reducer';
|
||||||
|
import * as resolvers from './resolvers';
|
||||||
|
import initDispatchers from './dispatchers';
|
||||||
|
import { WPDataActions, WPDataSelectors } from '../types';
|
||||||
|
|
||||||
|
registerStore< State >( STORE_NAME, {
|
||||||
|
reducer: reducer as Reducer< State, AnyAction >,
|
||||||
|
actions,
|
||||||
|
controls,
|
||||||
|
resolvers,
|
||||||
|
selectors,
|
||||||
|
} );
|
||||||
|
|
||||||
|
initDispatchers();
|
||||||
|
export const NAVIGATION_STORE_NAME = STORE_NAME;
|
||||||
|
|
||||||
|
declare module '@wordpress/data' {
|
||||||
|
function dispatch(
|
||||||
|
key: typeof STORE_NAME
|
||||||
|
): DispatchFromMap< typeof actions & WPDataActions >;
|
||||||
|
function select(
|
||||||
|
key: typeof STORE_NAME
|
||||||
|
): SelectFromMap< typeof selectors > & WPDataSelectors;
|
||||||
|
}
|
|
@ -1,9 +1,16 @@
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
import type { Reducer } from 'redux';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import TYPES from './action-types';
|
import TYPES from './action-types';
|
||||||
|
import { Action } from './actions';
|
||||||
|
import { NavigationState } from './types';
|
||||||
|
|
||||||
const reducer = (
|
const reducer: Reducer< NavigationState, Action > = (
|
||||||
state = {
|
state = {
|
||||||
error: null,
|
error: null,
|
||||||
menuItems: [],
|
menuItems: [],
|
||||||
|
@ -11,25 +18,25 @@ const reducer = (
|
||||||
requesting: {},
|
requesting: {},
|
||||||
persistedQuery: {},
|
persistedQuery: {},
|
||||||
},
|
},
|
||||||
{ type, error, favorite, favorites, menuItems, persistedQuery }
|
action
|
||||||
) => {
|
) => {
|
||||||
switch ( type ) {
|
switch ( action.type ) {
|
||||||
case TYPES.SET_MENU_ITEMS:
|
case TYPES.SET_MENU_ITEMS:
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
menuItems,
|
menuItems: action.menuItems,
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case TYPES.ADD_MENU_ITEMS:
|
case TYPES.ADD_MENU_ITEMS:
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
menuItems: [ ...state.menuItems, ...menuItems ],
|
menuItems: [ ...state.menuItems, ...action.menuItems ],
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case TYPES.ON_HISTORY_CHANGE:
|
case TYPES.ON_HISTORY_CHANGE:
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
persistedQuery,
|
persistedQuery: action.persistedQuery,
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case TYPES.GET_FAVORITES_FAILURE:
|
case TYPES.GET_FAVORITES_FAILURE:
|
||||||
|
@ -53,7 +60,7 @@ const reducer = (
|
||||||
case TYPES.GET_FAVORITES_SUCCESS:
|
case TYPES.GET_FAVORITES_SUCCESS:
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
favorites,
|
favorites: action.favorites,
|
||||||
requesting: {
|
requesting: {
|
||||||
...state.requesting,
|
...state.requesting,
|
||||||
getFavorites: false,
|
getFavorites: false,
|
||||||
|
@ -63,7 +70,7 @@ const reducer = (
|
||||||
case TYPES.ADD_FAVORITE_FAILURE:
|
case TYPES.ADD_FAVORITE_FAILURE:
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
error,
|
error: action.error,
|
||||||
requesting: {
|
requesting: {
|
||||||
...state.requesting,
|
...state.requesting,
|
||||||
addFavorite: false,
|
addFavorite: false,
|
||||||
|
@ -80,15 +87,15 @@ const reducer = (
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case TYPES.ADD_FAVORITE_SUCCESS:
|
case TYPES.ADD_FAVORITE_SUCCESS:
|
||||||
const newFavorites = ! state.favorites.includes( favorite )
|
const newFavorites = ! state.favorites.includes( action.favorite )
|
||||||
? [ ...state.favorites, favorite ]
|
? [ ...state.favorites, action.favorite ]
|
||||||
: state.favorites;
|
: state.favorites;
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
favorites: newFavorites,
|
favorites: newFavorites,
|
||||||
menuItems: state.menuItems.map( ( item ) => {
|
menuItems: state.menuItems.map( ( item ) => {
|
||||||
if ( item.id === favorite ) {
|
if ( item.id === action.favorite ) {
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
menuId: 'favorites',
|
menuId: 'favorites',
|
||||||
|
@ -107,7 +114,7 @@ const reducer = (
|
||||||
...state,
|
...state,
|
||||||
requesting: {
|
requesting: {
|
||||||
...state.requesting,
|
...state.requesting,
|
||||||
error,
|
error: action.error,
|
||||||
removeFavorite: false,
|
removeFavorite: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -123,14 +130,14 @@ const reducer = (
|
||||||
break;
|
break;
|
||||||
case TYPES.REMOVE_FAVORITE_SUCCESS:
|
case TYPES.REMOVE_FAVORITE_SUCCESS:
|
||||||
const filteredFavorites = state.favorites.filter(
|
const filteredFavorites = state.favorites.filter(
|
||||||
( f ) => f !== favorite
|
( f ) => f !== action.favorite
|
||||||
);
|
);
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
...state,
|
...state,
|
||||||
favorites: filteredFavorites,
|
favorites: filteredFavorites,
|
||||||
menuItems: state.menuItems.map( ( item ) => {
|
menuItems: state.menuItems.map( ( item ) => {
|
||||||
if ( item.id === favorite ) {
|
if ( item.id === action.favorite ) {
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
menuId: 'plugins',
|
menuId: 'plugins',
|
||||||
|
@ -148,4 +155,5 @@ const reducer = (
|
||||||
return state;
|
return state;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type State = ReturnType< typeof reducer >;
|
||||||
export default reducer;
|
export default reducer;
|
|
@ -17,7 +17,7 @@ export function* getFavorites() {
|
||||||
yield getFavoritesRequest();
|
yield getFavoritesRequest();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const results = yield apiFetch( {
|
const results: string[] = yield apiFetch( {
|
||||||
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
path: `${ WC_ADMIN_NAMESPACE }/navigation/favorites/me`,
|
||||||
} );
|
} );
|
||||||
|
|
|
@ -3,9 +3,14 @@
|
||||||
*/
|
*/
|
||||||
import { applyFilters } from '@wordpress/hooks';
|
import { applyFilters } from '@wordpress/hooks';
|
||||||
|
|
||||||
const MENU_ITEMS_HOOK = 'woocommerce_navigation_menu_items';
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import { NavigationState } from './types';
|
||||||
|
|
||||||
export const getMenuItems = ( state ) => {
|
const MENU_ITEMS_HOOK = 'woocommerce_navigation_menu_items' as const;
|
||||||
|
|
||||||
|
export const getMenuItems = ( state: NavigationState ) => {
|
||||||
/**
|
/**
|
||||||
* Navigation Menu Items.
|
* Navigation Menu Items.
|
||||||
*
|
*
|
||||||
|
@ -15,14 +20,17 @@ export const getMenuItems = ( state ) => {
|
||||||
return applyFilters( MENU_ITEMS_HOOK, state.menuItems );
|
return applyFilters( MENU_ITEMS_HOOK, state.menuItems );
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getFavorites = ( state ) => {
|
export const getFavorites = ( state: NavigationState ) => {
|
||||||
return state.favorites || [];
|
return state.favorites || [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isNavigationRequesting = ( state, selector ) => {
|
export const isNavigationRequesting = (
|
||||||
|
state: NavigationState,
|
||||||
|
selector: string
|
||||||
|
) => {
|
||||||
return state.requesting[ selector ] || false;
|
return state.requesting[ selector ] || false;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getPersistedQuery = ( state ) => {
|
export const getPersistedQuery = ( state: NavigationState ) => {
|
||||||
return state.persistedQuery || {};
|
return state.persistedQuery || {};
|
||||||
};
|
};
|
|
@ -14,6 +14,7 @@ const defaultState = {
|
||||||
|
|
||||||
describe( 'navigation reducer', () => {
|
describe( 'navigation reducer', () => {
|
||||||
it( 'should return a default state', () => {
|
it( 'should return a default state', () => {
|
||||||
|
// @ts-expect-error -- we're testing the reducer's default state.
|
||||||
const state = reducer( undefined, {} );
|
const state = reducer( undefined, {} );
|
||||||
expect( state ).toEqual( defaultState );
|
expect( state ).toEqual( defaultState );
|
||||||
expect( state ).not.toBe( defaultState );
|
expect( state ).not.toBe( defaultState );
|
||||||
|
@ -27,16 +28,25 @@ describe( 'navigation reducer', () => {
|
||||||
id: 'menu-item-1',
|
id: 'menu-item-1',
|
||||||
title: 'Menu Item 1',
|
title: 'Menu Item 1',
|
||||||
menuId: 'primary',
|
menuId: 'primary',
|
||||||
|
url: 'https://example.com/menu-item-1',
|
||||||
|
migrate: true,
|
||||||
|
order: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'menu-item-2',
|
id: 'menu-item-2',
|
||||||
title: 'Menu Item 2',
|
title: 'Menu Item 2',
|
||||||
menuId: 'primary',
|
menuId: 'primary',
|
||||||
|
url: 'https://example.com/menu-item-2',
|
||||||
|
migrate: true,
|
||||||
|
order: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'menu-item-3',
|
id: 'menu-item-3',
|
||||||
title: 'Menu Item 3',
|
title: 'Menu Item 3',
|
||||||
menuId: 'secondary',
|
menuId: 'secondary',
|
||||||
|
url: 'https://example.com/menu-item-3',
|
||||||
|
migrate: true,
|
||||||
|
order: 3,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
} );
|
} );
|
||||||
|
@ -55,8 +65,15 @@ describe( 'navigation reducer', () => {
|
||||||
id: 'menu-item-1',
|
id: 'menu-item-1',
|
||||||
title: 'Menu Item 1',
|
title: 'Menu Item 1',
|
||||||
menuId: 'primary',
|
menuId: 'primary',
|
||||||
|
url: 'https://example.com/menu-item-1',
|
||||||
|
migrate: true,
|
||||||
|
order: 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
error: null,
|
||||||
|
favorites: [],
|
||||||
|
requesting: {},
|
||||||
|
persistedQuery: {},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: TYPES.ADD_MENU_ITEMS,
|
type: TYPES.ADD_MENU_ITEMS,
|
||||||
|
@ -65,6 +82,9 @@ describe( 'navigation reducer', () => {
|
||||||
id: 'menu-item-2',
|
id: 'menu-item-2',
|
||||||
title: 'Menu Item 2',
|
title: 'Menu Item 2',
|
||||||
menuId: 'primary',
|
menuId: 'primary',
|
||||||
|
url: 'https://example.com/menu-item-2',
|
||||||
|
migrate: true,
|
||||||
|
order: 2,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
import { getPersistedQuery } from '@woocommerce/navigation';
|
||||||
|
|
||||||
|
// https://github.com/woocommerce/woocommerce/blob/ecec9eaa76cb7b2e36f79175837b71f4f64996b1/plugins/woocommerce/src/Admin/Features/Navigation/Menu.php
|
||||||
|
export type MenuItem = {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
url: string;
|
||||||
|
order: number;
|
||||||
|
migrate: boolean;
|
||||||
|
menuId: string;
|
||||||
|
isCategory?: boolean;
|
||||||
|
badge?: number;
|
||||||
|
backButtonLabel?: string;
|
||||||
|
parent?: string;
|
||||||
|
capability?: string;
|
||||||
|
matchExpression?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type NavigationState = {
|
||||||
|
error: null | unknown;
|
||||||
|
menuItems: MenuItem[];
|
||||||
|
favorites: string[];
|
||||||
|
requesting: {
|
||||||
|
[ key: string ]: boolean | unknown;
|
||||||
|
};
|
||||||
|
persistedQuery: ReturnType< typeof getPersistedQuery >;
|
||||||
|
};
|
|
@ -9,17 +9,20 @@ import { createElement, useRef } from '@wordpress/element';
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import { STORE_NAME } from './constants';
|
import { STORE_NAME } from './constants';
|
||||||
|
import { MenuItem } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Higher-order component used to hydrate navigation data.
|
* Higher-order component used to hydrate navigation data.
|
||||||
*
|
*
|
||||||
* @param {Object} data Data object with menu items and site information.
|
* @param {Object} data Data object with menu items and site information.
|
||||||
|
* @param {MenuItem[]} data.menuItems Menu items to hydrate.
|
||||||
*/
|
*/
|
||||||
export const withNavigationHydration = ( data ) =>
|
export const withNavigationHydration = ( data: { menuItems: MenuItem[] } ) =>
|
||||||
createHigherOrderComponent(
|
createHigherOrderComponent< Record< string, unknown > >(
|
||||||
( OriginalComponent ) => ( props ) => {
|
( OriginalComponent ) => ( props ) => {
|
||||||
const dataRef = useRef( data );
|
const dataRef = useRef( data );
|
||||||
|
|
||||||
|
// @ts-expect-error // @ts-expect-error registry is not defined in the wp.data typings
|
||||||
useSelect( ( select, registry ) => {
|
useSelect( ( select, registry ) => {
|
||||||
if ( ! dataRef.current ) {
|
if ( ! dataRef.current ) {
|
||||||
return;
|
return;
|
Loading…
Reference in New Issue