diff --git a/src/components/form-control/index.tsx b/src/components/form-control/index.tsx index 4fe61685..7781c950 100644 --- a/src/components/form-control/index.tsx +++ b/src/components/form-control/index.tsx @@ -1,5 +1,5 @@ import React, { memo, useEffect, useState } from 'react'; -import { useHistory, useParams } from 'react-router-dom'; +import { useParams } from 'react-router-dom'; import { Variant } from '@fellesdatakatalog/button'; import { ConceptStatus, TimeFormat } from '../../types/enums'; @@ -15,7 +15,9 @@ import SC from './styled'; interface Props { isFormDirty: boolean; - createNewConceptRevisionAndNavigate: () => void; + onNewConceptRevision: () => void; + onPatch: () => void; + onDelete: () => void; isInitialInValidForm: boolean; lastPatchedResponse: any; values: V; @@ -23,7 +25,9 @@ interface Props { const FormControl = ({ isFormDirty, - createNewConceptRevisionAndNavigate, + onNewConceptRevision, + onPatch, + onDelete, isInitialInValidForm, lastPatchedResponse, values @@ -56,11 +60,9 @@ const FormControl = ({ const toggleShowConfirmDelete = (): void => setShowConfirmDelete(!showConfirmDelete); - const { catalogId, conceptId } = useParams<{ - catalogId: string; + const { conceptId } = useParams<{ conceptId: string; }>(); - const history = useHistory(); const dispatch = useAppDispatch(); const conceptForm = useAppSelector(state => state.conceptForm); const { concept } = conceptForm; @@ -103,23 +105,23 @@ const FormControl = ({ )}.`; }; - const deleteConceptAndNavigate = async (): Promise => { + const confirmDelete = async (): Promise => { await deleteConcept(conceptId); - history.push(`/${catalogId}`); + onDelete(); }; useEffect(() => { if (patchCalled && !(isSaving || errorSaving)) { - history.go(0); + onPatch(); } - }, [isSaving, errorSaving, setPatchCalled]); + }, [isSaving, errorSaving, patchCalled, onPatch]); return concept ? ( <> {isFormDirty && published && erSistPublisert && ( - + {localization.saveDraft} @@ -245,7 +247,7 @@ const FormControl = ({ )} diff --git a/src/pages/concept-registration-page/form-concept/index.tsx b/src/pages/concept-registration-page/form-concept/index.tsx index 668f0038..09322fd9 100644 --- a/src/pages/concept-registration-page/form-concept/index.tsx +++ b/src/pages/concept-registration-page/form-concept/index.tsx @@ -32,6 +32,7 @@ import { schema as validationSchema } from './form-concept.schema'; import SC from './styled'; import { InternalInfo } from './internal-info'; +import { getConfig } from '../../../config'; export type FormValues = Pick< Concept, @@ -92,6 +93,10 @@ export const FormConceptPure: FC = ({ begrepsRelasjon: begrepsRelasjonError } = errors; const [showUserPrompt, setShowUserPrompt] = useState(true); + const [patchCalled, setPatchCalled] = useState(false); + const [deleteCalled, setDeleteCalled] = useState(false); + + const config = getConfig(); const languageEntities = useAppSelector(state => state.languages.entities); @@ -135,10 +140,38 @@ export const FormConceptPure: FC = ({ history.push(`/${catalogId}/${resourceId}`); }); + useEffect(() => { + const handler = event => { + event.preventDefault(); + event.returnValue = ''; + }; + // if the form is NOT unchanged, then set the onbeforeunload + if (dirty && showUserPrompt && !(patchCalled || deleteCalled)) { + window.addEventListener('beforeunload', handler); + // clean it up, if the dirty state changes + return () => { + window.removeEventListener('beforeunload', handler); + }; + } + + if (patchCalled) { + history.go(0); + } + if (deleteCalled) { + history.push( + config.enableConceptCatalogFrontend + ? `${config.conceptCatalogFrontendBaseUri}/${catalogId}` + : `/${catalogId}` + ); + } + // since this is not dirty, don't do anything + return () => {}; + }, [dirty, showUserPrompt, patchCalled, deleteCalled]); + return ( = ({ > isFormDirty={dirty} - createNewConceptRevisionAndNavigate={ - createNewConceptRevisionAndNavigate - } + onNewConceptRevision={createNewConceptRevisionAndNavigate} + onPatch={() => { + setPatchCalled(true); + }} + onDelete={() => { + setDeleteCalled(true); + }} isInitialInValidForm={!isValid} lastPatchedResponse={lastPatchedResponse} values={values}