-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missi…
…ng (#663) * ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missing * create first draft of useErmForm hook * ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missing * add useHistory * ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missing * move props from hook to ERMForm * keep navigationCheck prop in hook and default to true * ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missing * pass string instead of object to ConfirmationModals heading * ERM-3041 Dashboard: the "unsaved changes" confirmation modal is missing * add translations * * make FormComponent a node * Revert "* make FormComponent a node" This reverts commit d58a208. * * remove empty ghost file * ERM-3041 * try useCallback * fix: WIP Swap to ref based checks Currently not reading properly on useEffect cleanup, got to work out why formSpyRef isn't properly read in unblock cleanup function * feat: ERM Form Split out ERM Form into a proper component, and leave the hook implementation as is. Cleans up implementation in our apps --------- Co-authored-by: Ethan Freestone <[email protected]>
- Loading branch information
1 parent
1287952
commit 5ca9f56
Showing
7 changed files
with
179 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import PropTypes from 'prop-types'; | ||
import { Form as FinalForm, FormSpy } from 'react-final-form'; | ||
import createDecorator from 'final-form-focus'; | ||
import arrayMutators from 'final-form-arrays'; | ||
import { FormattedMessage, useIntl } from 'react-intl'; | ||
import { LastVisitedContext } from '@folio/stripes/core'; | ||
import { ConfirmationModal } from '@folio/stripes/components'; | ||
|
||
import { useErmForm } from '../hooks'; | ||
|
||
const focusOnErrors = createDecorator(); | ||
|
||
const ERMForm = ({ | ||
children, | ||
decorators = [], | ||
initialValues, | ||
navigationCheck = true, | ||
mutators = [], | ||
onSubmit, | ||
subscription = {}, | ||
...formOptions // Any other options we sould pass to final form | ||
}) => { | ||
const { | ||
openModal, | ||
continueNavigation, | ||
closeModal, | ||
formSpyRef, | ||
_isMounted, | ||
} = useErmForm({ navigationCheck }); | ||
const intl = useIntl(); | ||
|
||
return ( | ||
<LastVisitedContext.Consumer> | ||
{(ctx) => ( | ||
<> | ||
<FinalForm | ||
{...formOptions} | ||
decorators={[focusOnErrors, ...decorators]} | ||
initialValues={initialValues} | ||
mutators={{ ...mutators, ...arrayMutators }} | ||
onSubmit={onSubmit} | ||
render={(formProps) => ( | ||
<> | ||
{children(formProps)} | ||
<FormSpy | ||
onChange={(state) => { | ||
if (_isMounted.current) { | ||
formSpyRef.current = state; | ||
} | ||
}} | ||
subscription={{ | ||
dirty: true, | ||
submitSucceeded: true, | ||
invalid: true, | ||
submitting: true, | ||
}} | ||
{...formProps} | ||
/> | ||
</> | ||
)} | ||
subscription={{ | ||
dirty: true, | ||
submitSucceeded: true, | ||
invalid: true, | ||
submitting: true, | ||
initialValues: true, | ||
pristine: true, | ||
...subscription, | ||
}} | ||
/> | ||
<ConfirmationModal | ||
cancelLabel={<FormattedMessage id="stripes-erm-components.closeWithoutSaving" />} | ||
confirmLabel={<FormattedMessage id="stripes-erm-components.keepEditing" />} | ||
heading={intl.formatMessage({ id: 'stripes-erm-components.areYouSure' })} | ||
id="cancel-editing-confirmation" | ||
message={<FormattedMessage id="stripes-erm-components.unsavedChanges" />} | ||
onCancel={() => continueNavigation(ctx)} | ||
onConfirm={closeModal} | ||
open={openModal} | ||
/> | ||
</> | ||
)} | ||
</LastVisitedContext.Consumer> | ||
); | ||
}; | ||
|
||
ERMForm.propTypes = { | ||
children: PropTypes.elementType.isRequired, | ||
decorators: PropTypes.arrayOf(PropTypes.object), | ||
initialValues: PropTypes.object, | ||
navigationCheck: PropTypes.bool, | ||
mutators: PropTypes.object, | ||
onSubmit: PropTypes.func, | ||
subscription: PropTypes.object, | ||
}; | ||
|
||
export default ERMForm; |
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 @@ | ||
export { default } from './ERMForm'; |
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,72 @@ | ||
import { useState, useEffect, useRef } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
|
||
const useErmForm = ({ navigationCheck = true } = {}) => { | ||
const [openModal, setOpenModal] = useState(false); | ||
const [nextLocation, setNextLocation] = useState(null); | ||
|
||
const unblock = useRef(); | ||
const formSpyRef = useRef(); | ||
const _isMounted = useRef(false); | ||
|
||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
_isMounted.current = true; | ||
|
||
const handleBlockedNavigation = (nextLoc) => { | ||
const shouldPrompt = | ||
!!formSpyRef.current && | ||
formSpyRef.current.dirty && | ||
!formSpyRef.current.submitSucceeded && | ||
!formSpyRef.current.submitting; | ||
|
||
if (shouldPrompt) { | ||
setOpenModal(true); | ||
setNextLocation(nextLoc); | ||
} | ||
|
||
return !shouldPrompt; | ||
}; | ||
|
||
// Set up the history.block listener | ||
if (navigationCheck) { | ||
unblock.current = history.block(handleBlockedNavigation); | ||
} | ||
|
||
// Clean up the history.block listener on unmount | ||
return () => { | ||
_isMounted.current = false; | ||
if (unblock.current) { | ||
unblock.current(); | ||
unblock.current = null; | ||
} | ||
}; | ||
}, [history, navigationCheck]); | ||
|
||
const continueNavigation = (ctx) => { | ||
const { pathname, search } = nextLocation; | ||
|
||
ctx.cachePreviousUrl(); | ||
if (unblock.current) { | ||
unblock.current(); | ||
unblock.current = null; | ||
} | ||
setOpenModal(false); | ||
history.push(`${pathname}${search}`); | ||
}; | ||
|
||
const closeModal = () => { | ||
setOpenModal(false); | ||
}; | ||
|
||
return { | ||
openModal, | ||
continueNavigation, | ||
closeModal, | ||
formSpyRef, | ||
_isMounted, | ||
}; | ||
}; | ||
|
||
export default useErmForm; |
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