-
-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): unsaved changes alert (#1409)
- Loading branch information
Showing
14 changed files
with
369 additions
and
315 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
83 changes: 83 additions & 0 deletions
83
packages/console/src/components/UnsavedChangesAlertModal/index.tsx
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,83 @@ | ||
import type { Blocker, Transition } from 'history'; | ||
import React, { useCallback, useContext, useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { UNSAFE_NavigationContext, Navigator } from 'react-router-dom'; | ||
|
||
import ConfirmModal from '../ConfirmModal'; | ||
|
||
type BlockerNavigator = Navigator & { | ||
location: Location; | ||
block(blocker: Blocker): () => void; | ||
}; | ||
|
||
type Props = { | ||
hasUnsavedChanges: boolean; | ||
}; | ||
|
||
const UnsavedChangesAlertModal = ({ hasUnsavedChanges }: Props) => { | ||
const { navigator } = useContext(UNSAFE_NavigationContext); | ||
|
||
const [displayAlert, setDisplayAlert] = useState(false); | ||
const [transition, setTransition] = useState<Transition>(); | ||
|
||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); | ||
|
||
useEffect(() => { | ||
if (!hasUnsavedChanges) { | ||
return; | ||
} | ||
|
||
const { | ||
block, | ||
location: { pathname }, | ||
} = navigator as BlockerNavigator; | ||
|
||
const unblock = block((transition) => { | ||
const { | ||
location: { pathname: targetPathname }, | ||
} = transition; | ||
|
||
// Note: We don't want to show the alert if the user is navigating to the same page. | ||
if (targetPathname === pathname) { | ||
return; | ||
} | ||
|
||
setDisplayAlert(true); | ||
|
||
setTransition({ | ||
...transition, | ||
retry() { | ||
unblock(); | ||
transition.retry(); | ||
}, | ||
}); | ||
}); | ||
|
||
return unblock; | ||
}, [navigator, hasUnsavedChanges]); | ||
|
||
const leavePage = useCallback(() => { | ||
transition?.retry(); | ||
setDisplayAlert(false); | ||
}, [transition]); | ||
|
||
const stayOnPage = useCallback(() => { | ||
setDisplayAlert(false); | ||
}, [setDisplayAlert]); | ||
|
||
return ( | ||
<ConfirmModal | ||
isOpen={displayAlert} | ||
confirmButtonType="primary" | ||
confirmButtonText="admin_console.general.stay_on_page" | ||
cancelButtonText="admin_console.general.leave_page" | ||
onCancel={leavePage} | ||
onConfirm={stayOnPage} | ||
onClose={stayOnPage} | ||
> | ||
{t('general.unsaved_changes_warning')} | ||
</ConfirmModal> | ||
); | ||
}; | ||
|
||
export default UnsavedChangesAlertModal; |
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
39 changes: 29 additions & 10 deletions
39
packages/console/src/pages/ApplicationDetails/components/AdvancedSettings.tsx
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.