Skip to content

Commit 6380d95

Browse files
committed
Add more error handling in GiftCardPurchaseScene
1 parent 70d417c commit 6380d95

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/components/cards/ErrorCard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface Props {
2525
* If the error is an I18nError, it will display the localized message.
2626
* Otherwise, it will display a localized generic error message with a report
2727
* error button for unexpected errors.
28+
*
29+
* TODO: Add a warning variant
2830
*/
2931
export const ErrorCard: React.FC<Props> = props => {
3032
const { error } = props

src/components/scenes/GiftCardPurchaseScene.tsx

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { DropdownInputButton } from '../buttons/DropdownInputButton'
3636
import { KavButtons } from '../buttons/KavButtons'
3737
import { AlertCardUi4 } from '../cards/AlertCard'
3838
import { EdgeCard } from '../cards/EdgeCard'
39+
import { ErrorCard } from '../cards/ErrorCard'
3940
import { EdgeAnim } from '../common/EdgeAnim'
4041
import { EdgeTouchableOpacity } from '../common/EdgeTouchableOpacity'
4142
import { SceneWrapper } from '../common/SceneWrapper'
@@ -118,6 +119,13 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
118119
footer: string
119120
} | null>(null)
120121

122+
// Warning state for product unavailable
123+
const [productUnavailable, setProductUnavailable] =
124+
React.useState<boolean>(false)
125+
126+
// Error state for unexpected errors
127+
const [error, setError] = React.useState<unknown>(null)
128+
121129
// Fetch allowed tokens from Phaze API
122130
const { data: tokenQueryResult } = useQuery({
123131
queryKey: ['phazeTokens', account?.id, isReady],
@@ -195,8 +203,10 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
195203

196204
// Handle amount text change for variable range
197205
const handleAmountChange = useHandler((text: string) => {
198-
// Clear minimum warning when user modifies amount
206+
// Clear warnings/errors when user modifies amount
199207
setMinimumWarning(null)
208+
setProductUnavailable(false)
209+
setError(null)
200210

201211
// Only allow numbers and decimal point
202212
const cleaned = text.replace(/[^0-9.]/g, '')
@@ -216,6 +226,8 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
216226
const handleMaxPress = useHandler(() => {
217227
if (hasVariableRange) {
218228
setMinimumWarning(null)
229+
setProductUnavailable(false)
230+
setError(null)
219231
setAmountText(String(maxVal))
220232
setSelectedAmount(maxVal)
221233
}
@@ -259,8 +271,10 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
259271
)
260272

261273
if (result != null) {
262-
// Clear minimum warning when user modifies amount
274+
// Clear warnings/errors when user modifies amount
263275
setMinimumWarning(null)
276+
setProductUnavailable(false)
277+
setError(null)
264278
setSelectedAmount(result.amount)
265279
setAmountText(String(result.amount))
266280
}
@@ -496,8 +510,20 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
496510
} catch (err: unknown) {
497511
debugLog('phaze', 'Order creation error:', err)
498512

499-
// Check for minimum amount error from API
513+
// Clear previous warnings/errors
514+
setMinimumWarning(null)
515+
setProductUnavailable(false)
516+
setError(null)
517+
500518
const errorMessage = err instanceof Error ? err.message : ''
519+
520+
// Check for product unavailable error
521+
if (errorMessage.includes('Product is unavailable')) {
522+
setProductUnavailable(true)
523+
return
524+
}
525+
526+
// Check for minimum amount error from API
501527
const minimumMatch = /Minimum cart cost should be above: ([\d.]+)/.exec(
502528
errorMessage
503529
)
@@ -515,7 +541,8 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
515541
)
516542
})
517543
} else {
518-
showError(err)
544+
// Show ErrorCard for other errors
545+
setError(err)
519546
}
520547
} finally {
521548
setIsCreatingOrder(false)
@@ -632,6 +659,8 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
632659
style={styles.maxButton}
633660
onPress={() => {
634661
setMinimumWarning(null)
662+
setProductUnavailable(false)
663+
setError(null)
635664
const maxDenom =
636665
sortedDenominations[sortedDenominations.length - 1]
637666
setSelectedAmount(maxDenom)
@@ -668,14 +697,22 @@ export const GiftCardPurchaseScene: React.FC<Props> = props => {
668697
)}
669698
</EdgeAnim>
670699

671-
{/* Minimum Amount Warning */}
672-
{minimumWarning != null ? (
700+
{/* Warnings/Errors - product unavailable takes precedence */}
701+
{productUnavailable ? (
702+
<AlertCardUi4
703+
type="warning"
704+
title={lstrings.gift_card_product_unavailable_title}
705+
body={lstrings.gift_card_product_unavailable_warning}
706+
/>
707+
) : minimumWarning != null ? (
673708
<AlertCardUi4
674709
type="warning"
675710
title={lstrings.gift_card_minimum_warning_title}
676711
header={minimumWarning.header}
677712
footer={minimumWarning.footer}
678713
/>
714+
) : error != null ? (
715+
<ErrorCard error={error} />
679716
) : null}
680717

681718
{/* Product Description Card */}

src/locales/en_US.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,9 @@ const strings = {
19131913
gift_card_pending_toast:
19141914
'Your gift card is being delivered. Please wait for a few minutes for it to arrive.',
19151915
gift_card_quote_expired_toast: 'Your quote has expired. Please try again.',
1916+
gift_card_product_unavailable_title: 'Temporarily Unavailable',
1917+
gift_card_product_unavailable_warning:
1918+
'Card is temporarily unavailable. Please select another card from this brand or try again later.',
19161919

19171920
// #endregion
19181921

src/locales/strings/enUS.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,8 @@
14841484
"gift_card_pending": "Pending Delivery, Please Wait...",
14851485
"gift_card_pending_toast": "Your gift card is being delivered. Please wait for a few minutes for it to arrive.",
14861486
"gift_card_quote_expired_toast": "Your quote has expired. Please try again.",
1487+
"gift_card_product_unavailable_title": "Temporarily Unavailable",
1488+
"gift_card_product_unavailable_warning": "Card is temporarily unavailable. Please select another card from this brand or try again later.",
14871489
"countdown_hours": ">%sh",
14881490
"countdown_minutes_seconds": "%sm %ss",
14891491
"countdown_seconds": "%ss",

0 commit comments

Comments
 (0)