diff --git a/src/app/personside/dialogpanel/sendMelding/SendNyMeldingContainer.tsx b/src/app/personside/dialogpanel/sendMelding/SendNyMeldingContainer.tsx index 3e468db06e..0a9a2f850d 100644 --- a/src/app/personside/dialogpanel/sendMelding/SendNyMeldingContainer.tsx +++ b/src/app/personside/dialogpanel/sendMelding/SendNyMeldingContainer.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { FormEvent, useCallback, useMemo, useState } from 'react'; +import { FormEvent, useCallback, useEffect, useMemo, useState } from 'react'; import { MeldingValidator } from './validatorer'; import { Meldingstype, @@ -129,6 +129,20 @@ function SendNyMeldingContainer(props: Props) { }; const url = isOn ? `${apiBaseUri}/v2/dialog/sendmelding` : `${apiBaseUri}/dialog/${fnr}/sendmelding`; + useEffect(() => { + const promptUser = (e: BeforeUnloadEvent) => { + if (sendNyMeldingStatus.type !== SendNyMeldingStatus.POSTING) { + return; + } + + e.preventDefault(); + e.returnValue = true; + }; + + window.addEventListener('beforeunload', promptUser); + return () => window.removeEventListener('beforeunload', promptUser); + }, []); + if (MeldingValidator.erGyldigReferat(state) && state.tema) { const temagruppe = state.tema; setSendNyMeldingStatus({ type: SendNyMeldingStatus.POSTING });