-
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.
413 tlačidlo gpdr pri registrácií opraviť (#448)
* GDPR now opens on new page * Added useNavigationTrap and modal to reg form * Changed style and texts of the modal * Fixed lint errors * Fixed typo * Blocking now occurs on closing and refreshing page * larger link for GDPR in Registration ("tu" -> "najdete tu") * Changed dialog to custom component * use `disableSpacing` on DialogActions, add comment --------- Co-authored-by: rtrembecky <[email protected]>
- Loading branch information
1 parent
1485862
commit 4c7f127
Showing
3 changed files
with
161 additions
and
64 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {useRouter} from 'next/router' | ||
import {useCallback, useEffect, useRef} from 'react' | ||
|
||
interface NavigationTrapProps { | ||
shouldBlockNavigation: boolean | ||
onNavigate: () => void | ||
} | ||
|
||
export const useNavigationTrap = ({shouldBlockNavigation, onNavigate}: NavigationTrapProps) => { | ||
const router = useRouter() | ||
|
||
const currentPath = router.asPath | ||
const nextPath = useRef('') | ||
const navConfirmed = useRef(false) | ||
|
||
const killNavigation = useCallback(() => { | ||
router.events.emit('routeChangeError', '', '', {shallow: false}) | ||
|
||
// eslint-disable-next-line no-throw-literal | ||
throw 'Canceling navigation due to unsaved changes in the page' | ||
}, [router]) | ||
|
||
useEffect(() => { | ||
const pageNavigate = (path: string) => { | ||
if (navConfirmed.current) return | ||
if (shouldBlockNavigation && path !== currentPath) { | ||
nextPath.current = path | ||
onNavigate() | ||
killNavigation() | ||
} | ||
} | ||
|
||
const pageExit = (e: BeforeUnloadEvent) => { | ||
e.preventDefault() | ||
} | ||
|
||
router.events.on('routeChangeStart', pageNavigate) | ||
window.addEventListener('beforeunload', pageExit) | ||
|
||
return () => { | ||
router.events.off('routeChangeStart', pageNavigate) | ||
window.removeEventListener('beforeunload', pageExit) | ||
} | ||
}, [shouldBlockNavigation, currentPath, killNavigation, onNavigate, router.events]) | ||
|
||
const continueNavigation = () => { | ||
navConfirmed.current = true | ||
router.push(nextPath.current) | ||
} | ||
|
||
return {nextPath, continueNavigation} | ||
} |