-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proposal - styled modal that warns the user is leaving a form. (#3966)
Co-authored-by: Smith <[email protected]>
- Loading branch information
1 parent
0bb6ba9
commit 925c663
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
source/frontend/src/components/common/ConfirmNavigation.tsx
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,42 @@ | ||
import { Location } from 'history'; | ||
import { useEffect, useState } from 'react'; | ||
import { Prompt } from 'react-router-dom'; | ||
|
||
import { getCancelModalProps, useModalContext } from '@/hooks/useModalContext'; | ||
|
||
interface Props { | ||
when?: boolean | undefined; | ||
navigate: (path: string) => void; | ||
shouldBlockNavigation: (location: Location) => boolean; | ||
} | ||
|
||
const ConfirmNavigation = ({ when, navigate, shouldBlockNavigation }: Props) => { | ||
const { setDisplayModal, setModalContent } = useModalContext(); | ||
const [lastLocation, setLastLocation] = useState<Location | null>(null); | ||
const [confirmedNavigation, setConfirmedNavigation] = useState(false); | ||
|
||
const handleConfirmNavigationClick = () => { | ||
setDisplayModal(false); | ||
setConfirmedNavigation(true); | ||
}; | ||
|
||
const handleBlockedNavigation = (nextLocation: Location): boolean => { | ||
if (!confirmedNavigation && shouldBlockNavigation(nextLocation)) { | ||
setModalContent({ ...getCancelModalProps(), handleOk: () => handleConfirmNavigationClick() }); | ||
setDisplayModal(true); | ||
setLastLocation(nextLocation); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
useEffect(() => { | ||
if (confirmedNavigation && lastLocation) { | ||
// Navigate to the previous blocked location with your navigate function | ||
navigate(lastLocation.pathname); | ||
} | ||
}, [confirmedNavigation, lastLocation, navigate]); | ||
|
||
return <Prompt when={when} message={handleBlockedNavigation} />; | ||
}; | ||
export default ConfirmNavigation; |
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