diff --git a/native/account/registration/registration-terms.react.js b/native/account/registration/registration-terms.react.js --- a/native/account/registration/registration-terms.react.js +++ b/native/account/registration/registration-terms.react.js @@ -15,8 +15,8 @@ } from './registration-types.js'; import PrimaryButton from '../../components/primary-button.react.js'; import commSwooshSource from '../../img/comm-swoosh.png'; -import { logInActionType } from '../../navigation/action-types.js'; import type { NavigationRoute } from '../../navigation/route-names.js'; +import { usePreventUserFromLeavingScreen } from '../../navigation/use-prevent-user-from-leaving-screen.js'; import { useStyles } from '../../themes/colors.js'; import Alert from '../../utils/alert.js'; import AuthButtonContainer from '../auth-components/auth-button-container.react.js'; @@ -109,27 +109,7 @@ } }, [register, userSelections, clearCachedSelections, onNonceExpired]); - React.useEffect(() => { - if (!registrationInProgress) { - return undefined; - } - navigation.setOptions({ - gestureEnabled: false, - headerLeft: null, - }); - const removeListener = navigation.addListener('beforeRemove', e => { - if (e.data.action.type !== logInActionType) { - e.preventDefault(); - } - }); - return () => { - navigation.setOptions({ - gestureEnabled: true, - headerLeft: undefined, - }); - removeListener(); - }; - }, [navigation, registrationInProgress]); + usePreventUserFromLeavingScreen(registrationInProgress); const styles = useStyles(unboundStyles); diff --git a/native/account/restore-password-account-screen.react.js b/native/account/restore-password-account-screen.react.js --- a/native/account/restore-password-account-screen.react.js +++ b/native/account/restore-password-account-screen.react.js @@ -20,6 +20,7 @@ import { useClientBackup } from '../backup/use-client-backup.js'; import type { NavigationRoute } from '../navigation/route-names.js'; import { RestoreBackupScreenRouteName } from '../navigation/route-names.js'; +import { usePreventUserFromLeavingScreen } from '../navigation/use-prevent-user-from-leaving-screen.js'; import { useStyles } from '../themes/colors.js'; import { appOutOfDateAlertDetails, @@ -146,6 +147,8 @@ retrieveLatestBackupInfo, ]); + usePreventUserFromLeavingScreen(isProcessing); + let restoreButtonVariant = 'loading'; if (!isProcessing) { restoreButtonVariant = areCredentialsPresent ? 'enabled' : 'disabled'; diff --git a/native/account/restore-prompt-screen.react.js b/native/account/restore-prompt-screen.react.js --- a/native/account/restore-prompt-screen.react.js +++ b/native/account/restore-prompt-screen.react.js @@ -20,6 +20,7 @@ RestoreSIWEBackupRouteName, RestorePasswordAccountScreenRouteName, } from '../navigation/route-names.js'; +import { usePreventUserFromLeavingScreen } from '../navigation/use-prevent-user-from-leaving-screen.js'; import { useColors, useStyles } from '../themes/colors.js'; import { appOutOfDateAlertDetails, @@ -108,6 +109,8 @@ [props.navigation, retrieveLatestBackupInfo, walletLogIn], ); + usePreventUserFromLeavingScreen(authInProgress); + const { panelState, openPanel, diff --git a/native/navigation/use-prevent-user-from-leaving-screen.js b/native/navigation/use-prevent-user-from-leaving-screen.js new file mode 100644 --- /dev/null +++ b/native/navigation/use-prevent-user-from-leaving-screen.js @@ -0,0 +1,33 @@ +// @flow + +import { useNavigation } from '@react-navigation/core'; +import * as React from 'react'; + +import { logInActionType } from './action-types.js'; + +function usePreventUserFromLeavingScreen(condition: boolean) { + const navigation = useNavigation(); + React.useEffect(() => { + if (!condition) { + return undefined; + } + navigation.setOptions({ + gestureEnabled: false, + headerLeft: null, + }); + const removeListener = navigation.addListener('beforeRemove', e => { + if (e.data.action.type !== logInActionType) { + e.preventDefault(); + } + }); + return () => { + navigation.setOptions({ + gestureEnabled: true, + headerLeft: undefined, + }); + removeListener(); + }; + }, [condition, navigation]); +} + +export { usePreventUserFromLeavingScreen };