-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add warning when navigating away with unsaved changes in environment settings #5049
Open
Zaimwa9
wants to merge
12
commits into
Flagsmith:main
Choose a base branch
from
Zaimwa9:refactor/migrate-environment-settings-page-to-ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b48b8ca
refactor: migrated-feature-iso-to-typescript
Zaimwa9 5a4ea6e
refactor: implemented-not-saved-form-modal-hook
Zaimwa9 84061ab
refactor: removed-environment-settings-page-js
Zaimwa9 64d946a
Merge branch 'main' of https://github.com/zaimwa9/flagsmith into refa…
Zaimwa9 ba0ee6a
refactor: removed-unnecessary-logs
Zaimwa9 eb709d6
refactor: reseted-docker-compose
Zaimwa9 c0b0b70
refactor: removed-unecessary-log
Zaimwa9 9a5d7fd
refactor: removed-react-fc-type-of-children
Zaimwa9 9410fc8
refactor: reverted-project-dev-local-api-url
Zaimwa9 7a1da9e
refactor: lint-prettier
Zaimwa9 974a20e
refactor: renamed-discard-and-confirm-navigation
Zaimwa9 e5cd0dd
refactor: renamed-environment-settings-page-component
Zaimwa9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type Webhook = { | ||
id: string | ||
url: string | ||
secret: string | ||
enabled: boolean | ||
created_at: string | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! I guess we can start using this in a lot of places |
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,104 @@ | ||
import { useState, useEffect, useRef, useCallback } from "react" | ||
import { RouterChildContext } from "react-router-dom" | ||
import { Modal, ModalHeader, ModalBody } from "reactstrap" | ||
|
||
/** | ||
* useFormNotSavedModal | ||
* @param {history: RouterChildContext['router']['history']} history - The history object | ||
* @param {string} warningMessage - The message to show when user attempts to leave | ||
* @returns {[React.FC, Function, boolean]} | ||
*/ | ||
|
||
type UseFormNotSavedModalReturn = [React.FC, React.Dispatch<React.SetStateAction<boolean>>, boolean] | ||
|
||
interface UseFormNotSavedModalOptions { | ||
warningMessage?: string | ||
onDiscard?: () => void | ||
} | ||
|
||
const useFormNotSavedModal = ( | ||
history: RouterChildContext['router']['history'], | ||
options: UseFormNotSavedModalOptions = {} | ||
): UseFormNotSavedModalReturn => { | ||
const { onDiscard, warningMessage = "You have unsaved changes, are you sure you want to leave?" } = options | ||
|
||
const [isDirty, setIsDirty] = useState(false) | ||
const [isNavigating, setIsNavigating] = useState(false); | ||
const [nextLocation, setNextLocation] = useState<Location | null>(null); | ||
|
||
const unblockRef = useRef<(() => void) | null>(null); | ||
useEffect(() => { | ||
if (!isDirty) return; | ||
|
||
const unblock = history.block((transition: Location) => { | ||
setNextLocation(transition); | ||
setIsNavigating(true); | ||
return false; | ||
}); | ||
|
||
unblockRef.current = unblock; | ||
return () => { | ||
if (unblockRef.current) { | ||
unblockRef.current(); | ||
} | ||
unblockRef.current = null; | ||
}; | ||
}, [isDirty, history]); | ||
|
||
const confirmNavigation = useCallback(() => { | ||
// allow the route change to happen | ||
if (unblockRef.current) { | ||
unblockRef.current(); // unblocks | ||
unblockRef.current = null; | ||
} | ||
// navigate | ||
if (nextLocation) { | ||
history.push(`${nextLocation.pathname}${nextLocation.search}`); | ||
} | ||
if (onDiscard) { | ||
onDiscard() | ||
} | ||
setIsDirty(false) | ||
setIsNavigating(false); | ||
setNextLocation(null); | ||
}, [nextLocation, history, onDiscard]); | ||
|
||
const cancelNavigation = useCallback(() => { | ||
history.push(`${history.location.pathname}${history.location.search}`); | ||
setIsNavigating(false); | ||
setNextLocation(null); | ||
}, [history]); | ||
|
||
// Listen for browser/tab close (the 'beforeunload' event) | ||
useEffect(() => { | ||
const handleBeforeUnload = (event: BeforeUnloadEvent) => { | ||
if (!isDirty) return | ||
event.preventDefault() | ||
event.returnValue = warningMessage | ||
} | ||
|
||
window.addEventListener("beforeunload", handleBeforeUnload) | ||
return () => { | ||
window.removeEventListener("beforeunload", handleBeforeUnload) | ||
} | ||
}, [isDirty, warningMessage]) | ||
|
||
const DirtyFormModal = () => ( | ||
<Modal isOpen={isDirty && isNavigating} toggle={cancelNavigation}> | ||
<ModalHeader> | ||
<h5>Unsaved Changes</h5> | ||
</ModalHeader> | ||
<ModalBody> | ||
<p>{warningMessage}</p> | ||
</ModalBody> | ||
<div className="modal-footer"> | ||
<Button onClick={confirmNavigation} theme="secondary" className="mr-2">Yes, discard changes</Button> | ||
<Button onClick={cancelNavigation} class="btn-primary">Cancel</Button> | ||
</div> | ||
</Modal> | ||
) | ||
|
||
return [DirtyFormModal, setIsDirty, isDirty] | ||
} | ||
|
||
export default useFormNotSavedModal |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very specific implementation related to the hook. The selected tab would keep the focus on after interacting with the newly implemented modal