Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exhaustive deps warning #49291

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,53 +120,41 @@ export default function UnsavedInnerBlocks( {
const { hasResolvedNavigationMenus } = useNavigationMenu();

// Automatically save the uncontrolled blocks.
useEffect(
() => {
// The block will be disabled when used in a BlockPreview.
// In this case avoid automatic creation of a wp_navigation post.
// Otherwise the user will be spammed with lots of menus!
//
// Also ensure other navigation menus have loaded so an
// accurate name can be created.
//
// Don't try saving when another save is already
// in progress.
//
// And finally only create the menu when the block is selected,
// which is an indication they want to start editing.
if (
isDisabled ||
isSaving ||
! hasResolvedDraftNavigationMenus ||
! hasResolvedNavigationMenus ||
! hasSelection ||
! innerBlocksAreDirty
) {
return;
}
useEffect( () => {
// The block will be disabled when used in a BlockPreview.
// In this case avoid automatic creation of a wp_navigation post.
// Otherwise the user will be spammed with lots of menus!
//
// Also ensure other navigation menus have loaded so an
// accurate name can be created.
//
// Don't try saving when another save is already
// in progress.
//
// And finally only create the menu when the block is selected,
// which is an indication they want to start editing.
if (
isDisabled ||
isSaving ||
! hasResolvedDraftNavigationMenus ||
! hasResolvedNavigationMenus ||
! hasSelection ||
! innerBlocksAreDirty
) {
return;
}

createNavigationMenu( null, blocks );
},
/* The dependency "blocks" is intentionally omitted here.
* This is because making blocks a dependency would cause
* createNavigationMenu to run on every block change whereas
* we only want it to run when the blocks are first detected
* as dirty.
* A better solution might be to add a hard saving lock using
* a ref to avoid having to disbale theses eslint rules.
*/
/* eslint-disable react-hooks/exhaustive-deps */
[
createNavigationMenu,
isDisabled,
isSaving,
hasResolvedDraftNavigationMenus,
hasResolvedNavigationMenus,
innerBlocksAreDirty,
hasSelection,
]
/* eslint-enable react-hooks/exhaustive-deps */
);
createNavigationMenu( null, blocks );
}, [
blocks,
createNavigationMenu,
isDisabled,
isSaving,
hasResolvedDraftNavigationMenus,
hasResolvedNavigationMenus,
innerBlocksAreDirty,
hasSelection,
] );

const Wrapper = isSaving ? Disabled : 'div';

Expand Down
35 changes: 31 additions & 4 deletions packages/e2e-test-utils-playwright/src/request-utils/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ export async function createNavigationMenu(
export async function deleteAllMenus( this: RequestUtils ) {
const navMenus = await this.rest< NavigationMenu[] >( {
path: `/wp/v2/navigation/`,
data: {
status: [
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
],
},
} );

if ( navMenus.length ) {
Expand All @@ -93,6 +105,18 @@ export async function deleteAllMenus( this: RequestUtils ) {

const classicMenus = await this.rest< NavigationMenu[] >( {
path: `/wp/v2/menus/`,
data: {
status: [
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
],
},
} );

if ( classicMenus.length ) {
Expand All @@ -108,15 +132,18 @@ export async function deleteAllMenus( this: RequestUtils ) {
/**
* Get latest navigation menus
*
* @param args
* @param args.status
* @return {string} Menu content.
*/
export async function getNavigationMenus( this: RequestUtils ) {
export async function getNavigationMenus(
this: RequestUtils,
args: { status: 'publish' }
) {
const navigationMenus = await this.rest< NavigationMenu[] >( {
method: 'GET',
path: `/wp/v2/navigation/`,
data: {
status: 'publish',
},
data: args,
} );
return navigationMenus;
}
Loading