Skip to content

Commit 2f4af27

Browse files
committed
Decouple input disable logic from amount type support
1 parent 59cfac6 commit 2f4af27

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

src/components/scenes/RampCreateScene.tsx

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ type Props = (
7575
direction: 'buy' | 'sell'
7676
}
7777

78-
// Helper function to determine which input types should be disabled
79-
interface AmountTypeSupport {
80-
fiatInputDisabled: boolean
81-
cryptoInputDisabled: boolean
82-
}
83-
8478
export const RampCreateScene: React.FC<Props> = (props: Props) => {
8579
const { direction, navigation, route } = props
8680
const { regionCode: initialRegionCode, forcedWalletResult } =
@@ -216,16 +210,15 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
216210
}, [selectedFiatCurrencyCode])
217211

218212
// Determine which input types should be disabled
219-
const { fiatInputDisabled, cryptoInputDisabled } =
220-
getAmountTypeSupport(supportedPlugins)
213+
const amountTypeSupport = getAmountTypeSupport(supportedPlugins)
221214

222215
// On first entry, initialize the fiat amount to approximately $500 USD
223216
React.useEffect(() => {
224217
const applyInitial = async (): Promise<void> => {
225218
// Don't override if the user has started typing or fiat input is disabled
226219
if (
227220
hasAppliedInitialAmount.current ||
228-
fiatInputDisabled ||
221+
amountTypeSupport.onlyCrypto ||
229222
'empty' in exchangeAmount ||
230223
lastUsedInput != null ||
231224
shouldShowRegionSelect
@@ -267,7 +260,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
267260

268261
applyInitial().catch(() => {})
269262
}, [
270-
fiatInputDisabled,
263+
amountTypeSupport.onlyCrypto,
271264
isLightAccount,
272265
lastUsedInput,
273266
selectedWallet,
@@ -291,8 +284,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
291284

292285
// Guard against creating request with disabled input type
293286
if (
294-
(lastUsedInput === 'fiat' && fiatInputDisabled) ||
295-
(lastUsedInput === 'crypto' && cryptoInputDisabled)
287+
(lastUsedInput === 'fiat' && amountTypeSupport.onlyCrypto) ||
288+
(lastUsedInput === 'crypto' && amountTypeSupport.onlyFiat)
296289
) {
297290
return null
298291
}
@@ -320,8 +313,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
320313
lastUsedInput,
321314
countryCode,
322315
stateProvinceCode,
323-
fiatInputDisabled,
324-
cryptoInputDisabled,
316+
amountTypeSupport.onlyCrypto,
317+
amountTypeSupport.onlyFiat,
325318
direction
326319
])
327320

@@ -390,7 +383,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
390383
// Derived state for display values
391384
const displayFiatAmount = React.useMemo(() => {
392385
// Don't show any value if fiat input is disabled
393-
if (fiatInputDisabled) return ''
386+
if (amountTypeSupport.onlyCrypto) return ''
394387
if ('empty' in exchangeAmount) return ''
395388

396389
if ('max' in exchangeAmount) {
@@ -416,7 +409,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
416409
return ''
417410
}
418411
}, [
419-
fiatInputDisabled,
412+
amountTypeSupport.onlyCrypto,
420413
maxQuoteForMaxFlow,
421414
exchangeAmount,
422415
lastUsedInput,
@@ -425,7 +418,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
425418

426419
const displayCryptoAmount = React.useMemo(() => {
427420
// Don't show any value if crypto input is disabled
428-
if (cryptoInputDisabled) return ''
421+
if (amountTypeSupport.onlyFiat) return ''
429422
if ('empty' in exchangeAmount || lastUsedInput === null) return ''
430423

431424
if ('max' in exchangeAmount) {
@@ -451,7 +444,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
451444
return ''
452445
}
453446
}, [
454-
cryptoInputDisabled,
447+
amountTypeSupport.onlyFiat,
455448
maxQuoteForMaxFlow,
456449
exchangeAmount,
457450
lastUsedInput,
@@ -574,11 +567,14 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
574567
!isLoadingQuotes
575568
) {
576569
// Persist the chosen amount so it remains after returning
577-
if (!fiatInputDisabled && maxQuoteForMaxFlow.fiatAmount != null) {
570+
if (
571+
!amountTypeSupport.onlyCrypto &&
572+
maxQuoteForMaxFlow.fiatAmount != null
573+
) {
578574
setLastUsedInput('fiat')
579575
setExchangeAmount({ amount: maxQuoteForMaxFlow.fiatAmount })
580576
} else if (
581-
!cryptoInputDisabled &&
577+
!amountTypeSupport.onlyFiat &&
582578
maxQuoteForMaxFlow.cryptoAmount != null
583579
) {
584580
setLastUsedInput('crypto')
@@ -597,8 +593,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
597593
isLoadingQuotes,
598594
rampQuoteRequest,
599595
navigation,
600-
fiatInputDisabled,
601-
cryptoInputDisabled,
596+
amountTypeSupport.onlyCrypto,
597+
amountTypeSupport.onlyFiat,
602598
exchangeAmount
603599
])
604600

@@ -619,6 +615,13 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
619615
)
620616
}
621617

618+
const fiatInputDisabled =
619+
'max' in exchangeAmount || amountTypeSupport.onlyCrypto
620+
const cryptoInputDisabled =
621+
isLoadingPersistedCryptoSelection ||
622+
'max' in exchangeAmount ||
623+
amountTypeSupport.onlyFiat
624+
622625
// Render trade form view
623626
return (
624627
<>
@@ -675,7 +678,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
675678
maxDecimals={2}
676679
returnKeyType="done"
677680
showSpinner={isFetchingQuotes && lastUsedInput === 'crypto'}
678-
disabled={'max' in exchangeAmount || fiatInputDisabled}
681+
disabled={fiatInputDisabled}
679682
expand
680683
/>
681684
</View>
@@ -720,11 +723,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
720723
maxDecimals={6}
721724
returnKeyType="done"
722725
showSpinner={isFetchingQuotes && lastUsedInput === 'fiat'}
723-
disabled={
724-
isLoadingPersistedCryptoSelection ||
725-
'max' in exchangeAmount ||
726-
cryptoInputDisabled
727-
}
726+
disabled={cryptoInputDisabled}
728727
expand
729728
/>
730729
</>
@@ -833,8 +832,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
833832
lastUsedInput === null ||
834833
supportedPlugins.length === 0 ||
835834
sortedQuotes.length === 0 ||
836-
(lastUsedInput === 'fiat' && fiatInputDisabled) ||
837-
(lastUsedInput === 'crypto' && cryptoInputDisabled)
835+
(lastUsedInput === 'fiat' && amountTypeSupport.onlyCrypto) ||
836+
(lastUsedInput === 'crypto' && amountTypeSupport.onlyFiat)
838837
}
839838
/>
840839
</>
@@ -915,11 +914,17 @@ const getStyles = cacheStyles((theme: ReturnType<typeof useTheme>) => ({
915914
}
916915
}))
917916

917+
// Helper function to determine which input types should be disabled
918+
interface AmountTypeSupport {
919+
onlyCrypto: boolean
920+
onlyFiat: boolean
921+
}
922+
918923
function getAmountTypeSupport(
919924
supportedPlugins: SupportedPluginResult[]
920925
): AmountTypeSupport {
921926
if (supportedPlugins.length === 0) {
922-
return { fiatInputDisabled: false, cryptoInputDisabled: false }
927+
return { onlyCrypto: false, onlyFiat: false }
923928
}
924929

925930
// Collect all supported amount types from all plugins
@@ -945,7 +950,7 @@ function getAmountTypeSupport(
945950
allSupportedTypes.has('crypto') && !allSupportedTypes.has('fiat')
946951

947952
return {
948-
fiatInputDisabled: onlyCrypto,
949-
cryptoInputDisabled: onlyFiat
953+
onlyCrypto,
954+
onlyFiat
950955
}
951956
}

0 commit comments

Comments
 (0)