Skip to content

Commit

Permalink
Fix UI bugs in send flow (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
onghwan authored and atn4z7 committed Nov 18, 2024
1 parent 210acaa commit b70e35d
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 183 deletions.
14 changes: 8 additions & 6 deletions packages/core-mobile/app/contexts/SendContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ interface SendContextState {
setError: Dispatch<string | undefined>
isSending: boolean
setIsSending: Dispatch<boolean>
isValidating: boolean
setIsValidating: Dispatch<boolean>
canValidate: boolean
setCanValidate: Dispatch<boolean>
isValid: boolean
}

Expand All @@ -51,9 +51,9 @@ export const SendContextProvider = ({
const [toAddress, setToAddress] = useState<string>()

const { data: networkFee } = useNetworkFee(activeNetwork)
const [error, setError] = useState<string | undefined>()
const [error, setError] = useState<string | undefined>(NOT_TOUCHED_ERROR)
const [isSending, setIsSending] = useState(false)
const [isValidating, setIsValidating] = useState(false)
const [canValidate, setCanValidate] = useState(false)

const [defaultMaxFeePerGas, setDefaultMaxFeePerGas] = useState<bigint>(0n)

Expand Down Expand Up @@ -88,8 +88,8 @@ export const SendContextProvider = ({
setError,
isSending,
setIsSending,
isValidating,
setIsValidating,
canValidate,
setCanValidate,
isValid: error === undefined
}
return <SendContext.Provider value={state}>{children}</SendContext.Provider>
Expand All @@ -98,3 +98,5 @@ export const SendContextProvider = ({
export function useSendContext(): SendContextState {
return useContext(SendContext)
}

const NOT_TOUCHED_ERROR = ''
18 changes: 9 additions & 9 deletions packages/core-mobile/app/screens/nft/send/NftSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import { useNavigation, useRoute } from '@react-navigation/native'
import { NFTDetailsSendScreenProps } from 'navigation/types'
import useEVMSend from 'screens/send/hooks/useEVMSend'
import { useSelector } from 'react-redux'
import { selectTokensWithBalance } from 'store/balance'
import { NetworkTokenWithBalance, TokenType } from '@avalabs/vm-module-types'
import { audioFeedback, Audios } from 'utils/AudioFeedback'
import { isUserRejectedError } from 'store/rpc/providers/walletConnect/utils'
import { showTransactionErrorToast } from 'utils/toast'
import { getJsonRpcErrorMessage } from 'utils/getJsonRpcErrorMessage'
import { useSendContext } from 'contexts/SendContext'
import QRScanSVG from 'components/svg/QRScanSVG'
import { useNativeTokenWithBalance } from 'screens/send/hooks/useNativeTokenWithBalance'

type NftSendScreenProps = {
onOpenAddressBook: () => void
Expand All @@ -57,15 +56,12 @@ export default function NftSend({
error,
isSending,
isValid,
isValidating
setCanValidate
} = useSendContext()
const { activeNetwork } = useNetworks()
const activeAccount = useSelector(selectActiveAccount)
const fromAddress = activeAccount?.addressC ?? ''
const tokens = useSelector(selectTokensWithBalance)
const nativeToken = tokens.find(
t => t.type === TokenType.NATIVE
) as NetworkTokenWithBalance
const nativeToken = useNativeTokenWithBalance()
const [touched, setTouched] = useState(false)

const { send } = useEVMSend({
Expand All @@ -77,7 +73,7 @@ export default function NftSend({
})

const canSubmit =
!isValidating && !isSending && isValid && !!toAddress && error === undefined
!isSending && isValid && !!toAddress && error === undefined && touched

const {
saveRecentContact,
Expand Down Expand Up @@ -133,7 +129,11 @@ export default function NftSend({
if (touched === false && toAddress) {
setTouched(true)
}
}, [toAddress, token, touched])
}, [toAddress, touched, setCanValidate])

useEffect(() => {
setCanValidate(touched)
}, [touched, setCanValidate])

const onContactSelected = (
item: Contact | Account,
Expand Down
6 changes: 2 additions & 4 deletions packages/core-mobile/app/screens/send/SendTokenScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import { audioFeedback, Audios } from 'utils/AudioFeedback'
import { isUserRejectedError } from 'store/rpc/providers/walletConnect/utils'
import { showTransactionErrorToast } from 'utils/toast'
import { getJsonRpcErrorMessage } from 'utils/getJsonRpcErrorMessage'
import { selectTokensWithBalance } from 'store/balance'
import { useSelector } from 'react-redux'
import {
NetworkTokenWithBalance,
TokenType,
TokenWithBalanceAVM,
TokenWithBalanceBTC,
TokenWithBalancePVM
Expand All @@ -26,6 +24,7 @@ import SendEVM from './components/SendEVM'
import SendBTC from './components/SendBTC'
import SendAVM from './components/SendAVM'
import SendPVM from './components/SendPVM'
import { useNativeTokenWithBalance } from './hooks/useNativeTokenWithBalance'

type SendTokenScreenNavigationProp = SendTokensScreenProps<
typeof AppNavigation.Send.Send
Expand All @@ -40,8 +39,7 @@ const SendTokenScreen: FC = () => {
const { setToken, setToAddress } = useSendContext()
const { activeNetwork } = useNetworks()
const activeAccount = useSelector(selectActiveAccount)
const tokens = useSelector(selectTokensWithBalance)
const nativeToken = tokens.find(t => t.type === TokenType.NATIVE)
const nativeToken = useNativeTokenWithBalance()

useEffect(() => {
if (params?.token) {
Expand Down
20 changes: 3 additions & 17 deletions packages/core-mobile/app/screens/send/components/SendAVM.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalanceAVM } from '@avalabs/vm-module-types'
Expand Down Expand Up @@ -26,17 +26,8 @@ const SendAVM = ({
onSuccess: (txHash: string) => void
onFailure: (txError: unknown) => void
}): JSX.Element => {
const {
setToAddress,
token,
setToken,
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
const { setToAddress, token, maxAmount, error, isValid, isSending, maxFee } =
useSendContext()
const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressAVM ?? ''
Expand All @@ -49,10 +40,6 @@ const SendAVM = ({
account
})

useEffect(() => {
setToken(nativeToken)
}, [nativeToken, setToken])

const handleSend = async (): Promise<void> => {
if (token === undefined) {
return
Expand All @@ -79,7 +66,6 @@ const SendAVM = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
2 changes: 0 additions & 2 deletions packages/core-mobile/app/screens/send/components/SendBTC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const SendBTC = ({
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
Expand Down Expand Up @@ -70,7 +69,6 @@ const SendBTC = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
13 changes: 2 additions & 11 deletions packages/core-mobile/app/screens/send/components/SendEVM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ const SendEVM = ({
onSuccess: (txHash: string) => void
onFailure: (txError: unknown) => void
}): JSX.Element => {
const {
setToAddress,
token,
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
const { setToAddress, token, maxAmount, error, isValid, isSending, maxFee } =
useSendContext()
const fromAddress = account?.addressC ?? ''

const { send } = useEVMSend({
Expand Down Expand Up @@ -70,7 +62,6 @@ const SendEVM = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
20 changes: 3 additions & 17 deletions packages/core-mobile/app/screens/send/components/SendPVM.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalancePVM } from '@avalabs/vm-module-types'
Expand Down Expand Up @@ -26,17 +26,8 @@ const SendPVM = ({
onSuccess: (txHash: string) => void
onFailure: (txError: unknown) => void
}): JSX.Element => {
const {
setToAddress,
token,
setToken,
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
const { setToAddress, token, maxAmount, error, isValid, isSending, maxFee } =
useSendContext()
const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressPVM ?? ''
Expand All @@ -49,10 +40,6 @@ const SendPVM = ({
account
})

useEffect(() => {
setToken(nativeToken)
}, [nativeToken, setToken])

const handleSend = async (): Promise<void> => {
if (token === undefined) {
return
Expand All @@ -79,7 +66,6 @@ const SendPVM = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
19 changes: 14 additions & 5 deletions packages/core-mobile/app/screens/send/components/SendTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const SendTokenForm = ({
addressPlaceholder,
error,
isValid,
isValidating,
isSending,
onOpenQRScanner,
onOpenAddressBook,
Expand All @@ -37,15 +36,21 @@ const SendTokenForm = ({
addressPlaceholder: string
error: string | undefined
isValid: boolean
isValidating: boolean
isSending: boolean
onOpenQRScanner: () => void
onOpenAddressBook: () => void
onSelectContact: (item: Contact | CorePrimaryAccount) => void
onSend: () => void
}): JSX.Element => {
const { setToken, token, setAmount, amount, toAddress, setToAddress } =
useSendContext()
const {
setToken,
token,
setAmount,
amount,
toAddress,
setToAddress,
setCanValidate
} = useSendContext()
const [isAddressTouched, setIsAddressTouched] = useState(false)
const [isTokenTouched, setIsTokenTouched] = useState(false)
const [isAmountTouched, setIsAmountTouched] = useState(false)
Expand Down Expand Up @@ -108,7 +113,11 @@ const SendTokenForm = ({
[isAddressTouched, isTokenTouched, isAmountTouched]
)

const canSubmit = !isValidating && !isSending && isValid
useEffect(() => {
setCanValidate(isAllFieldsTouched)
}, [isAllFieldsTouched, setCanValidate])

const canSubmit = !isSending && isValid && isAllFieldsTouched

return (
<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
Expand Down
Loading

0 comments on commit b70e35d

Please sign in to comment.