-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eee86ca
commit a387fd8
Showing
9 changed files
with
53 additions
and
144 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
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,50 +1,30 @@ | ||
import { memo, useState } from 'react' | ||
import { FormSpy } from 'react-final-form' | ||
import { useBlocker } from 'react-router' | ||
import { ConfirmModal } from 'oa-components' | ||
|
||
interface IProps { | ||
uploadComplete?: boolean | ||
message?: string | ||
} | ||
|
||
const CONFIRM_DIALOG_MSG = | ||
'You have unsaved changes. Are you sure you want to leave this page?' | ||
|
||
/** | ||
* When places inside a react-final-form <Form> element watches for form pristine/dirty | ||
* change and handles router and window confirmation if form contains changes | ||
**/ | ||
export const UnsavedChangesDialog = memo((props: IProps) => { | ||
// Use memo to only re-render if props change | ||
const [formIsDirty, setFormIsDirty] = useState(false) | ||
const shouldPromptUnsavedChanges = formIsDirty && !props.uploadComplete | ||
const message = props.message || CONFIRM_DIALOG_MSG | ||
type IProps = { | ||
hasChanges: boolean | ||
} | ||
|
||
export const UnsavedChangesDialog = ({ hasChanges }: IProps) => { | ||
const blocker = useBlocker( | ||
({ currentLocation, nextLocation }) => | ||
shouldPromptUnsavedChanges && | ||
currentLocation.pathname !== nextLocation.pathname, | ||
hasChanges && currentLocation.pathname !== nextLocation.pathname, | ||
) | ||
|
||
// Handle confirmaiton inside react route | ||
return ( | ||
<> | ||
<FormSpy | ||
subscription={{ dirty: true }} | ||
onChange={(form) => { | ||
setFormIsDirty(form.dirty) | ||
}} | ||
render={() => null} | ||
/> | ||
<ConfirmModal | ||
isOpen={blocker.state === 'blocked'} | ||
message={message} | ||
confirmButtonText="Yes" | ||
handleCancel={() => blocker.reset && blocker.reset()} | ||
handleConfirm={() => blocker.proceed && blocker.proceed()} | ||
/> | ||
</> | ||
<ConfirmModal | ||
isOpen={blocker.state === 'blocked'} | ||
message={CONFIRM_DIALOG_MSG} | ||
confirmButtonText="Yes" | ||
handleCancel={() => blocker.reset && blocker.reset()} | ||
handleConfirm={() => blocker.proceed && blocker.proceed()} | ||
/> | ||
) | ||
}) | ||
UnsavedChangesDialog.displayName = 'UnsavedChangesDialog' | ||
} |
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
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
Oops, something went wrong.