forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a confirmation modal when the user tries to navigate away with un…
…saved changes (woocommerce#35625) * Add a confirmation modal when the user tries to navigate away with unsaved changes * Add support for react router navigation * Fix unit tests
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
plugins/woocommerce-admin/client/hooks/usePreventLeavingPage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useContext, useEffect, useMemo } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { UNSAFE_NavigationContext as NavigationContext } from 'react-router-dom'; | ||
|
||
export default function usePreventLeavingPage( | ||
hasUnsavedChanges: boolean, | ||
/** | ||
* Some browsers ignore this message currently on before unload event. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#compatibility_notes | ||
*/ | ||
message?: string | ||
) { | ||
const confirmMessage = useMemo( | ||
() => | ||
message ?? | ||
__( 'Changes you made may not be saved.', 'woocommerce' ), | ||
[ message ] | ||
); | ||
const { navigator } = useContext( NavigationContext ); | ||
|
||
// This effect prevent react router from navigate and show | ||
// a confirmation message. It's a work around to beforeunload | ||
// because react router does not triggers that event. | ||
useEffect( () => { | ||
if ( hasUnsavedChanges ) { | ||
const push = navigator.push; | ||
|
||
navigator.push = ( ...args: Parameters< typeof push > ) => { | ||
/* eslint-disable-next-line no-alert */ | ||
const result = window.confirm( confirmMessage ); | ||
if ( result !== false ) { | ||
push( ...args ); | ||
} | ||
}; | ||
|
||
return () => { | ||
navigator.push = push; | ||
}; | ||
} | ||
}, [ navigator, hasUnsavedChanges, confirmMessage ] ); | ||
|
||
// This effect listen to the native beforeunload event to show | ||
// a confirmation message | ||
useEffect( () => { | ||
if ( hasUnsavedChanges ) { | ||
function onBeforeUnload( event: BeforeUnloadEvent ) { | ||
event.preventDefault(); | ||
return ( event.returnValue = confirmMessage ); | ||
} | ||
|
||
window.addEventListener( 'beforeunload', onBeforeUnload, { | ||
capture: true, | ||
} ); | ||
|
||
return () => { | ||
window.removeEventListener( 'beforeunload', onBeforeUnload, { | ||
capture: true, | ||
} ); | ||
}; | ||
} | ||
}, [ hasUnsavedChanges, confirmMessage ] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
plugins/woocommerce/changelog/enhancement-35194-confirm-when-exiting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: enhancement | ||
|
||
Add a confirmation modal when the user tries to navigate away with unsaved changes |