-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 5137.2-Requisition-line-edit-+-UI-updates…
…-to-line-columns
- Loading branch information
Showing
61 changed files
with
1,676 additions
and
206 deletions.
There are no files selected for viewing
96 changes: 37 additions & 59 deletions
96
client/packages/common/src/hooks/useConfirmOnLeaving/useConfirmOnLeaving.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 |
---|---|---|
@@ -1,73 +1,51 @@ | ||
import { useContext, useEffect, useRef } from 'react'; | ||
import { UNSAFE_NavigationContext as NavigationContext } from 'react-router-dom'; | ||
import { useCallback, useContext, useEffect } from 'react'; | ||
import { useBeforeUnload, useBlocker } from 'react-router-dom'; | ||
import { useTranslation } from '@common/intl'; | ||
import { useToggle } from '../useToggle'; | ||
import { ConfirmationModalContext } from '@openmsupply-client/common'; | ||
|
||
const promptUser = (e: BeforeUnloadEvent) => { | ||
// Cancel the event | ||
e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown | ||
// Chrome requires returnValue to be set | ||
e.returnValue = ''; | ||
}; | ||
|
||
// Ideally we'd use the `Prompt` component instead ( or usePrompt or useBlocker ) to prompt when navigating away using react-router | ||
// however, these weren't implemented in react-router-dom v6 at the time of implementation | ||
/** useConfirmOnLeaving is a hook that will prompt the user if they try to navigate away from, | ||
* or refresh the page, when there are unsaved changes. Be careful when using within a tab component though | ||
* these are unloaded, but the event handler is at the window level, and so doesn't care | ||
* or refresh the page, when there are unsaved changes. | ||
* */ | ||
export const useConfirmOnLeaving = (isUnsaved?: boolean) => { | ||
const unblockRef = useRef<any>(null); | ||
const { navigator } = useContext(NavigationContext); | ||
const t = useTranslation(); | ||
const { isOn, toggle } = useToggle(); | ||
const showConfirmation = (onOk: () => void) => { | ||
if ( | ||
confirm( | ||
`${t('heading.are-you-sure')}\n${t('messages.confirm-cancel-generic')}` | ||
) | ||
) { | ||
onOk(); | ||
} | ||
const customConfirm = (onOk: () => void) => { | ||
setOnConfirm(onOk); | ||
showConfirmation(); | ||
}; | ||
|
||
useEffect(() => { | ||
// note that multiple calls to addEventListener don't result in multiple event listeners being added | ||
// since the method called is idempotent. However, I didn't want to rely on the implementation details | ||
// so have the toggle state to ensure we only add/remove the event listener once | ||
if (isUnsaved && !isOn) { | ||
window.addEventListener('beforeunload', promptUser, { capture: true }); | ||
toggle(); | ||
const push = navigator.push; | ||
const { setOpen, setMessage, setOnConfirm, setTitle } = useContext( | ||
ConfirmationModalContext | ||
); | ||
|
||
navigator.push = (...args: Parameters<typeof push>) => { | ||
showConfirmation(() => { | ||
push(...args); | ||
}); | ||
}; | ||
const showConfirmation = useCallback(() => { | ||
setMessage(t('heading.are-you-sure')); | ||
setTitle(t('messages.confirm-cancel-generic')); | ||
setOpen(true); | ||
}, [setMessage, setTitle, setOpen]); | ||
|
||
return () => { | ||
navigator.push = push; | ||
}; | ||
} | ||
if (!isUnsaved && isOn) { | ||
window.removeEventListener('beforeunload', promptUser, { capture: true }); | ||
toggle(); | ||
unblockRef.current?.(); | ||
} | ||
}, [isUnsaved]); | ||
const blocker = useBlocker( | ||
({ currentLocation, nextLocation }) => | ||
!!isUnsaved && currentLocation.pathname !== nextLocation.pathname | ||
); | ||
|
||
// always remove the event listener on unmount, and don't check the toggle | ||
// which would be trapped in a stale closure | ||
useEffect( | ||
() => () => { | ||
window.removeEventListener('beforeunload', promptUser, { | ||
capture: true, | ||
}); | ||
unblockRef.current?.(); | ||
}, | ||
[] | ||
// handle page refresh events | ||
useBeforeUnload( | ||
useCallback( | ||
event => { | ||
// Cancel the refresh | ||
if (isUnsaved) event.preventDefault(); | ||
}, | ||
[isUnsaved] | ||
), | ||
{ capture: true } | ||
); | ||
|
||
return { showConfirmation }; | ||
useEffect(() => { | ||
if (blocker.state === 'blocked') { | ||
setOnConfirm(blocker.proceed); | ||
showConfirmation(); | ||
} | ||
}, [blocker]); | ||
|
||
return { showConfirmation: customConfirm }; | ||
}; |
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
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
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
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
Oops, something went wrong.