Skip to content

Commit

Permalink
Add supply error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Sep 14, 2023
1 parent 095a34c commit e8e3367
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useSwap} from '@yoroi/swap'
import {Balance} from '@yoroi/types'
import React, {useRef} from 'react'
import {defineMessages, useIntl} from 'react-intl'
Expand Down Expand Up @@ -40,6 +41,10 @@ export const AmountCard = ({
const amountInputRef = useRef<TextInput>(inputRef?.current ?? null)

const tokenInfo = useTokenInfo({wallet, tokenId})
const {createOrder} = useSwap()

const isSell = tokenId === createOrder.amounts.sell.tokenId

const noTokenSelected = !touched

const name = tokenInfo.ticker ?? tokenInfo.name
Expand All @@ -58,7 +63,19 @@ export const AmountCard = ({

<View style={styles.content}>
<Pressable style={styles.amountWrapper} onPress={focusInput}>
<AmountInput onChange={onChange} value={value} inputRef={amountInputRef} editable={inputEditable} />
<TextInput
keyboardType="numeric"
autoComplete="off"
value={value}
placeholder="0"
onChangeText={onChange}
allowFontScaling
selectionColor={COLORS.TRANSPARENT_BLACK}
style={styles.amountInput}
underlineColorAndroid="transparent"
ref={amountInputRef}
editable={inputEditable}
/>
</Pressable>

<Spacer width={7} />
Expand Down Expand Up @@ -98,42 +115,13 @@ export const AmountCard = ({
<View>
<Spacer height={4} />

<Text style={styles.errorText}>{strings.notEnoughBalance}</Text>
<Text style={styles.errorText}>{isSell ? strings.notEnoughBalance : strings.notEnoughSupply}</Text>
</View>
)}
</View>
)
}

type AmountInputProps = {
value?: string
onChange(value: string): void
inputRef?: React.RefObject<TextInput>
editable: boolean
}
const AmountInput = ({onChange, value, inputRef, editable}: AmountInputProps) => {
// TODO add more formatting if is the case
const onChangeText = (text: string) => {
onChange(text)
}

return (
<TextInput
keyboardType="numeric"
autoComplete="off"
value={value}
placeholder="0"
onChangeText={onChangeText}
allowFontScaling
selectionColor={COLORS.TRANSPARENT_BLACK}
style={styles.amountInput}
underlineColorAndroid="transparent"
ref={inputRef}
editable={editable}
/>
)
}

const messages = defineMessages({
selectToken: {
id: 'swap.swapScreen.selectToken',
Expand All @@ -147,6 +135,10 @@ const messages = defineMessages({
id: 'swap.swapScreen.notEnoughBalance',
defaultMessage: '!!!Not enough balance',
},
notEnoughSupply: {
id: 'swap.swapScreen.notEnoughSupply',
defaultMessage: '!!!Not enough supply in the pool',
},
})

const useStrings = () => {
Expand All @@ -155,6 +147,7 @@ const useStrings = () => {
selectToken: intl.formatMessage(messages.selectToken),
currentBalance: intl.formatMessage(messages.currentBalance),
notEnoughBalance: intl.formatMessage(messages.notEnoughBalance),
notEnoughSupply: intl.formatMessage(messages.notEnoughSupply),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const EditBuyAmount = () => {
}
}, [isBuyTouched, quantity, tokenInfo.decimals])

const hasSupply = !Quantities.isGreaterThan(quantity, createOrder?.selectedPool?.tokenB?.quantity ?? `0`)
const showError = !Quantities.isZero(quantity) && !hasSupply

const recalculateSellValue = (buyQuantity: BalanceQuantity) => {
if (createOrder.type === 'limit' && createOrder.limitPrice !== undefined) {
const buyQuantityDenominated = Quantities.denominated(buyQuantity, tokenInfo.decimals ?? 0)
Expand Down Expand Up @@ -84,6 +87,7 @@ export const EditBuyAmount = () => {
value={inputValue}
amount={{tokenId, quantity: balance}}
wallet={wallet}
hasError={showError}
navigateTo={navigate.selectBuyToken}
touched={isBuyTouched}
inputRef={inputRef}
Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"swap.swapScreen.swapTo": "Swap to",
"swap.swapScreen.currentBalance": "Current balance",
"swap.swapScreen.notEnoughBalance": "Not enough balance",
"swap.swapScreen.notEnoughSupply": "Not enough supply in the pool",
"swap.swapScreen.balance": "Balance",
"swap.swapScreen.selectToken": "Select token",
"swap.swapScreen.marketPrice": "Market price",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,59 @@
"defaultMessage": "!!!Select token",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 138,
"line": 126,
"column": 15,
"index": 4107
"index": 3924
},
"end": {
"line": 141,
"line": 129,
"column": 3,
"index": 4190
"index": 4007
}
},
{
"id": "swap.swapScreen.currentBalance",
"defaultMessage": "!!!Current Balance",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 142,
"line": 130,
"column": 18,
"index": 4210
"index": 4027
},
"end": {
"line": 145,
"line": 133,
"column": 3,
"index": 4299
"index": 4116
}
},
{
"id": "swap.swapScreen.notEnoughBalance",
"defaultMessage": "!!!Not enough balance",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 146,
"line": 134,
"column": 20,
"index": 4321
"index": 4138
},
"end": {
"line": 137,
"column": 3,
"index": 4232
}
},
{
"id": "swap.swapScreen.notEnoughSupply",
"defaultMessage": "!!!Not enough supply in the pool",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 138,
"column": 19,
"index": 4253
},
"end": {
"line": 149,
"line": 141,
"column": 3,
"index": 4415
"index": 4357
}
}
]

0 comments on commit e8e3367

Please sign in to comment.