Skip to content

Commit

Permalink
Merge branch 'develop' into wallet-init-typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Nov 30, 2021
2 parents fe28d4c + 2ba7516 commit ffb38e5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"@react-navigation/routers": "5.7.2",
"@react-navigation/stack": "5.14.5",
"@sentry/react-native": "1.4.5",
"@unstoppabledomains/resolution": "5.0.2",
"@unstoppabledomains/resolution": "6.0.3",
"@v-almonacid/react-native-hid": "5.15.4",
"add": "2.0.6",
"babel-eslint": "^10.1.0",
Expand Down
70 changes: 52 additions & 18 deletions src/Settings/WalletSettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import React from 'react'
import type {MessageDescriptor} from 'react-intl'
import {defineMessages, useIntl} from 'react-intl'
import {ScrollView, StyleSheet, Switch} from 'react-native'
import {useMutation, UseMutationOptions} from 'react-query'
import {useDispatch, useSelector} from 'react-redux'

import {DIALOG_BUTTONS, logout, showConfirmationDialog, updateWallets} from '../../legacy/actions'
import {DIALOG_BUTTONS, showConfirmationDialog, signout, updateWallets} from '../../legacy/actions'
import VotingBanner from '../../legacy/components/Catalyst/VotingBanner'
import {
NavigatedSettingsItem,
Expand All @@ -22,8 +23,7 @@ import walletManager from '../../legacy/crypto/walletManager'
import {confirmationMessages} from '../../legacy/i18n/global-messages'
import {CATALYST_ROUTES, SETTINGS_ROUTES, WALLET_ROOT_ROUTES} from '../../legacy/RoutesList'
import {easyConfirmationSelector, isSystemAuthEnabledSelector, walletNameSelector} from '../../legacy/selectors'
import {ignoreConcurrentAsyncHandler} from '../../legacy/utils/utils'
import {useSelectedWallet} from '../SelectedWallet'
import {useSelectedWallet, useSetSelectedWallet, useSetSelectedWalletMeta} from '../SelectedWallet'

export const WalletSettingsScreen = () => {
const intl = useIntl()
Expand All @@ -35,20 +35,6 @@ export const WalletSettingsScreen = () => {
const wallet = useSelectedWallet()

const dispatch = useDispatch()
// eslint-disable-next-line react-hooks/exhaustive-deps
const onLogout = React.useCallback(
ignoreConcurrentAsyncHandler(
() => async () => {
const selection = await showConfirmationDialog(confirmationMessages.logout, intl)

if (selection === DIALOG_BUTTONS.YES) {
await dispatch(logout())
}
},
500,
)(),
[],
)

const [pending, setPending] = React.useState(false)
const onSwitchWallet = async () => {
Expand All @@ -68,7 +54,7 @@ export const WalletSettingsScreen = () => {

<SettingsSection>
<PressableSettingsItem label={strings.switchWallet} onPress={onSwitchWallet} disabled={pending} />
<PressableSettingsItem label={strings.logout} onPress={onLogout} />
<LogoutButton />
</SettingsSection>

<SettingsSection title={strings.walletName}>
Expand Down Expand Up @@ -217,3 +203,51 @@ const getWalletType = (implementationId: WalletImplementationId): MessageDescrip

return messages.unknownWalletType
}

const useLogout = (options?: UseMutationOptions<void, Error>) => {
const intl = useIntl()
const dispatch = useDispatch()
const setSelectedWallet = useSetSelectedWallet()
const setSelectedWalletMeta = useSetSelectedWalletMeta()
const {closeWallet, ...mutation} = useCloseWallet({
onSuccess: () => {
setSelectedWallet(undefined)
setSelectedWalletMeta(undefined)
},
...options,
})

return {
logout: () => {
dispatch(signout()) // triggers navigation to login
setTimeout(() => closeWallet(), 1000) // wait for navigation to finish
},
logoutWithConfirmation: async () => {
const selection = await showConfirmationDialog(confirmationMessages.logout, intl)
if (selection === DIALOG_BUTTONS.YES) {
dispatch(signout()) // triggers navigation to login
setTimeout(() => closeWallet(), 1000) // wait for navigation to finish
}
},
...mutation,
}
}

const useCloseWallet = (options?: UseMutationOptions<void, Error>) => {
const mutation = useMutation({
mutationFn: () => walletManager.closeWallet(),
...options,
})

return {
...mutation,
closeWallet: () => mutation.mutate(),
}
}

const LogoutButton = () => {
const strings = useStrings()
const {logoutWithConfirmation, isLoading} = useLogout()

return <PressableSettingsItem label={strings.logout} onPress={logoutWithConfirmation} disabled={isLoading} />
}
9 changes: 4 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4002,14 +4002,13 @@
"@typescript-eslint/types" "5.0.0"
eslint-visitor-keys "^3.0.0"

"@unstoppabledomains/resolution@5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@unstoppabledomains/resolution/-/resolution-5.0.2.tgz#5390ab5d28b96ae6a3e05a2de17cb4f756ba1d3c"
integrity sha512-CHZeUi0fjqAJL1R5qLjOzpfWTTQ74Mha8g+HWbOY84Nb63OxvPA8wwJUbq4LST77exY3RJdEhq2chWjQdFOM7A==
"@unstoppabledomains/resolution@6.0.3":
version "6.0.3"
resolved "https://registry.yarnpkg.com/@unstoppabledomains/resolution/-/resolution-6.0.3.tgz#a70888840c86a5918cb3134e7ac745e054c92aa5"
integrity sha512-J57SOzDqTDbrTfTzMSybM010LNvyNgEqKtdNcC5CAFg1IeNjC9woX5j1o/IbglXd9CPp1NL7n2Q1mVn9py211g==
dependencies:
"@ethersproject/abi" "^5.0.1"
bn.js "^4.4.0"
commander "^4.1.1"
cross-fetch "^3.1.4"
elliptic "^6.5.4"
ethereum-ens-network-map "^1.0.2"
Expand Down

0 comments on commit ffb38e5

Please sign in to comment.