-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Show discard confirmation modal when going back from a page with unsaved changes #54176
base: main
Are you sure you want to change the base?
Changes from 6 commits
53beacb
a478017
dfaf517
d72f8a0
22a35f0
93328ea
0ccb5f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import useSideModalStackScreenOptions from '@libs/Navigation/AppNavigator/useSid | |
import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator'; | ||
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; | ||
import type {AuthScreensParamList, RightModalNavigatorParamList} from '@navigation/types'; | ||
import CONST from '@src/CONST'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import SCREENS from '@src/SCREENS'; | ||
import Overlay from './Overlay'; | ||
|
@@ -53,6 +54,9 @@ function RightModalNavigator({navigation, route}: RightModalNavigatorProps) { | |
} | ||
isExecutingRef.current = true; | ||
navigation.goBack(); | ||
setTimeout(() => { | ||
isExecutingRef.current = false; | ||
}, CONST.ANIMATED_TRANSITION); | ||
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. isExecutingRef prevent the user to click the overlay multiple times when going back (I can't repro the multiple click issue even after removing the ref). But since we are preventing the going back when there is unsaved change, the next press won't do anything. So, we need to reset isExecutingRef back. |
||
}} | ||
/> | ||
)} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {useNavigation} from '@react-navigation/native'; | ||
import type {NavigationAction} from '@react-navigation/native'; | ||
import React, {memo, useCallback, useRef, useState} from 'react'; | ||
import ConfirmModal from '@components/ConfirmModal'; | ||
import useBeforeRemove from '@hooks/useBeforeRemove'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
|
||
type DiscardChangesConfirmationProps = { | ||
getHasUnsavedChanges: () => boolean; | ||
}; | ||
|
||
function DiscardChangesConfirmation({getHasUnsavedChanges}: DiscardChangesConfirmationProps) { | ||
const navigation = useNavigation(); | ||
const {translate} = useLocalize(); | ||
const [isVisible, setIsVisible] = useState(false); | ||
const blockedNavigationAction = useRef<NavigationAction>(); | ||
|
||
useBeforeRemove( | ||
useCallback( | ||
(e) => { | ||
if (!getHasUnsavedChanges()) { | ||
return; | ||
} | ||
|
||
e.preventDefault(); | ||
blockedNavigationAction.current = e.data.action; | ||
setIsVisible(true); | ||
}, | ||
[getHasUnsavedChanges], | ||
), | ||
); | ||
|
||
return ( | ||
<ConfirmModal | ||
isVisible={isVisible} | ||
title={translate('discardChangesConfirmation.title')} | ||
prompt={translate('discardChangesConfirmation.body')} | ||
danger | ||
confirmText={translate('discardChangesConfirmation.confirmText')} | ||
cancelText={translate('common.cancel')} | ||
onConfirm={() => { | ||
setIsVisible(false); | ||
if (blockedNavigationAction.current) { | ||
navigation.dispatch(blockedNavigationAction.current); | ||
} | ||
}} | ||
onCancel={() => setIsVisible(false)} | ||
/> | ||
); | ||
} | ||
|
||
export default memo(DiscardChangesConfirmation); |
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.
Asked for the translation verification here: https://expensify.slack.com/archives/C01GTK53T8Q/p1734322609246139
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.
It approved, Let's add the Slack link in author checklist
Link to Slack message: