-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2969 from navikt/fjern-react-router-prompt
Fjerne avhengigheten til react router prompt, og forenkle bruken til …
- Loading branch information
Showing
4 changed files
with
57 additions
and
33 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 |
---|---|---|
@@ -1,35 +1,65 @@ | ||
import React, { FC } from 'react'; | ||
import React, { FC, useCallback } from 'react'; | ||
import { useApp } from '../../App/context/AppContext'; | ||
import { ModalWrapper } from './ModalWrapper'; | ||
import ReactRouterPrompt from 'react-router-prompt'; | ||
import { BlockerFunction, useBeforeUnload, useBlocker } from 'react-router-dom'; | ||
|
||
export const UlagretDataModal: FC = () => { | ||
const { nullstillIkkePersisterteKomponenter, ulagretData } = useApp(); | ||
const skalBlokkere = React.useCallback<BlockerFunction>( | ||
({ currentLocation, nextLocation }) => | ||
ulagretData && currentLocation.pathname !== nextLocation.pathname, | ||
[ulagretData] | ||
); | ||
const blocker = useBlocker(skalBlokkere); | ||
|
||
React.useEffect(() => { | ||
if (blocker.state === 'blocked' && ulagretData === false) { | ||
blocker.reset(); | ||
} | ||
}, [blocker, ulagretData]); | ||
|
||
const onAvbryt = () => blocker.state === 'blocked' && blocker.reset(); | ||
const onForlatSiden = () => blocker.state === 'blocked' && blocker.proceed(); | ||
|
||
/** | ||
* Denne trengs for å fange opp når noen refresher siden eller prøver å gå ut av selve siden. | ||
* Da kommer nettleserens innebygde prompt opp | ||
*/ | ||
useBeforeUnload( | ||
useCallback( | ||
(event) => { | ||
if (ulagretData) { | ||
event.preventDefault(); | ||
} | ||
}, | ||
[ulagretData] | ||
), | ||
{ capture: true } | ||
); | ||
|
||
return ( | ||
<ReactRouterPrompt when={ulagretData}> | ||
{({ isActive, onConfirm, onCancel }) => ( | ||
<ModalWrapper | ||
tittel={ | ||
'Du har ikke lagret dine siste endringer og vil miste disse om du forlater siden' | ||
} | ||
visModal={isActive} | ||
onClose={onCancel} | ||
aksjonsknapper={{ | ||
hovedKnapp: { | ||
onClick: onCancel, | ||
tekst: 'Gå tilbake for å lagre', | ||
}, | ||
lukkKnapp: { | ||
onClick: () => { | ||
onConfirm(); | ||
setTimeout(nullstillIkkePersisterteKomponenter, 10); | ||
}, | ||
tekst: 'Forlat siden', | ||
blocker.state === 'blocked' && ( | ||
<ModalWrapper | ||
tittel={ | ||
'Du har ikke lagret dine siste endringer og vil miste disse om du forlater siden' | ||
} | ||
visModal={true} | ||
onClose={onAvbryt} | ||
aksjonsknapper={{ | ||
hovedKnapp: { | ||
onClick: onAvbryt, | ||
tekst: 'Gå tilbake for å lagre', | ||
}, | ||
lukkKnapp: { | ||
onClick: () => { | ||
onForlatSiden(); | ||
setTimeout(nullstillIkkePersisterteKomponenter, 10); | ||
}, | ||
marginTop: 4, | ||
}} | ||
/> | ||
)} | ||
</ReactRouterPrompt> | ||
tekst: 'Forlat siden', | ||
}, | ||
marginTop: 4, | ||
}} | ||
/> | ||
) | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -6554,11 +6554,6 @@ [email protected]: | |
dependencies: | ||
react-router "7.0.2" | ||
|
||
react-router-prompt@^0.7.1: | ||
version "0.7.1" | ||
resolved "https://registry.yarnpkg.com/react-router-prompt/-/react-router-prompt-0.7.1.tgz#1e5326d221824d092f47876147704de2447d5001" | ||
integrity sha512-U8G2gPMDkS6fdmcHebhkgKv9v/oxsErQFVQYuQ/DXKD/CUSxvXPQpikE//Ij+vZdXleOJwW3RJIkoa2ctVqnuw== | ||
|
||
[email protected]: | ||
version "7.0.2" | ||
resolved "https://registry.yarnpkg.com/react-router/-/react-router-7.0.2.tgz#2820e107cb8cec8acc5db15a17470c056ea86022" | ||
|