Skip to content

Commit 982fcac

Browse files
author
Jon Tzeng
committed
Add form field-level validation to Purchase scene
1 parent fb6aca7 commit 982fcac

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/components/scenes/GiftCardPurchaseScene.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
231231
const [amountText, setAmountText] = React.useState<string>(
232232
hasFixedDenominations ? String(sortedDenominations[0]) : ''
233233
)
234+
const [amountInputError, setAmountInputError] = React.useState<
235+
string | undefined
236+
>()
234237

235238
// Update selection when denominations become available (e.g., after brand fetch)
236239
React.useEffect(() => {
@@ -247,6 +250,7 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
247250
setMinimumWarning(null)
248251
setProductUnavailable(false)
249252
setError(null)
253+
setAmountInputError(undefined)
250254

251255
// Only allow numbers and decimal point
252256
const cleaned = text.replace(/[^0-9.]/g, '')
@@ -262,12 +266,38 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
262266
}
263267
})
264268

269+
// Validate amount on blur for variable range cards
270+
const handleAmountBlur = useHandler(() => {
271+
if (!hasVariableRange || amountText === '') return
272+
273+
const parsed = parseFloat(amountText)
274+
if (isNaN(parsed)) return
275+
276+
const fiatSymbol = getFiatSymbol(brand.currency)
277+
if (parsed < minVal) {
278+
setAmountInputError(
279+
sprintf(
280+
lstrings.card_amount_min_error_message_1s,
281+
`${fiatSymbol}${minVal}`
282+
)
283+
)
284+
} else if (parsed > maxVal) {
285+
setAmountInputError(
286+
sprintf(
287+
lstrings.card_amount_max_error_message_1s,
288+
`${fiatSymbol}${maxVal}`
289+
)
290+
)
291+
}
292+
})
293+
265294
// Handle MAX button press
266295
const handleMaxPress = useHandler(() => {
267296
if (hasVariableRange) {
268297
setMinimumWarning(null)
269298
setProductUnavailable(false)
270299
setError(null)
300+
setAmountInputError(undefined)
271301
setAmountText(String(maxVal))
272302
setSelectedAmount(maxVal)
273303
}
@@ -732,10 +762,13 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
732762
<FilledTextInput
733763
value={amountText}
734764
onChangeText={handleAmountChange}
765+
onBlur={handleAmountBlur}
735766
keyboardType="decimal-pad"
767+
returnKeyType="done"
736768
placeholder={`${minVal} ${brand.currency} - ${maxVal} ${brand.currency}`}
737769
clearIcon
738770
textsizeRem={1.5}
771+
error={amountInputError}
739772
/>
740773
<EdgeTouchableOpacity
741774
style={styles.maxButton}

src/locales/en_US.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,8 @@ const strings = {
23782378
buy_new_card_button: `Buy New Card`,
23792379
card_amount_max_error_message_s: `Maximum card purchase amount is $%s`,
23802380
card_amount_min_error_message_s: `Minimum card purchase amount is $%s`,
2381+
card_amount_max_error_message_1s: 'Maximum card purchase amount is %1$s',
2382+
card_amount_min_error_message_1s: 'Minimum card purchase amount is %1$s',
23812383
delete_card_confirmation_title: 'Delete Card?',
23822384
getting_payment_invoice_message: 'Getting payment invoice',
23832385
learn_more_button: `Learn More`,

src/locales/strings/enUS.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,8 @@
18571857
"buy_new_card_button": "Buy New Card",
18581858
"card_amount_max_error_message_s": "Maximum card purchase amount is $%s",
18591859
"card_amount_min_error_message_s": "Minimum card purchase amount is $%s",
1860+
"card_amount_max_error_message_1s": "Maximum card purchase amount is %1$s",
1861+
"card_amount_min_error_message_1s": "Minimum card purchase amount is %1$s",
18601862
"delete_card_confirmation_title": "Delete Card?",
18611863
"getting_payment_invoice_message": "Getting payment invoice",
18621864
"learn_more_button": "Learn More",

0 commit comments

Comments
 (0)