From 6a9fdac75c3121bfd7d48bd09214a5139b0f77c2 Mon Sep 17 00:00:00 2001 From: Fringg Date: Mon, 9 Mar 2026 05:00:08 +0300 Subject: [PATCH] refactor: extract shared payment status sets to utility module - Move PAID_STATUSES/FAILED_STATUSES from Balance.tsx and TopUpResult.tsx to src/utils/paymentStatus.ts - Eliminates code duplication between the two pages --- src/pages/Balance.tsx | 27 +++------------------------ src/pages/TopUpResult.tsx | 34 +--------------------------------- src/utils/paymentStatus.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 57 deletions(-) create mode 100644 src/utils/paymentStatus.ts diff --git a/src/pages/Balance.tsx b/src/pages/Balance.tsx index d3ab517..a093431 100644 --- a/src/pages/Balance.tsx +++ b/src/pages/Balance.tsx @@ -13,6 +13,7 @@ import type { PaginatedResponse, Transaction } from '../types'; import { Card } from '@/components/data-display/Card'; import { Button } from '@/components/primitives/Button'; import { staggerContainer, staggerItem } from '@/components/motion/transitions'; +import { isPaidStatus, isFailedStatus } from '../utils/paymentStatus'; // Icons const ChevronDownIcon = ({ className = 'h-5 w-5' }: { className?: string }) => ( @@ -64,32 +65,10 @@ export default function Balance() { if (paymentHandledRef.current) return; const paymentStatus = searchParams.get('payment') || searchParams.get('status'); - const paidSet = new Set([ - 'succeeded', - 'success', - 'paid', - 'paid_over', - 'overpaid', - 'completed', - 'confirmed', - 'closed', - ]); - const failedSet = new Set([ - 'fail', - 'failed', - 'error', - 'canceled', - 'cancelled', - 'declined', - 'expired', - 'cancel', - 'system_fail', - 'refund_paid', - ]); const normalised = paymentStatus?.toLowerCase() ?? ''; - const isSuccess = paidSet.has(normalised) || searchParams.get('success') === 'true'; - const isFailed = failedSet.has(normalised); + const isSuccess = isPaidStatus(normalised) || searchParams.get('success') === 'true'; + const isFailed = isFailedStatus(normalised); if (isSuccess) { paymentHandledRef.current = true; diff --git a/src/pages/TopUpResult.tsx b/src/pages/TopUpResult.tsx index ac46a55..783a308 100644 --- a/src/pages/TopUpResult.tsx +++ b/src/pages/TopUpResult.tsx @@ -12,6 +12,7 @@ import { Spinner } from '@/components/ui/Spinner'; import { AnimatedCheckmark } from '@/components/ui/AnimatedCheckmark'; import { AnimatedCrossmark } from '@/components/ui/AnimatedCrossmark'; import { loadTopUpPendingInfo, clearTopUpPendingInfo } from '../utils/topUpStorage'; +import { isPaidStatus, isFailedStatus } from '../utils/paymentStatus'; // ── Constants ──────────────────────────────────────────────── const MAX_POLL_MS = 10 * 60 * 1000; // 10 minutes @@ -177,39 +178,6 @@ function TimeoutState({ onRetry, onGoBack }: { onRetry: () => void; onGoBack: () ); } -// ── Determine paid status from provider-specific status strings ── -const PAID_STATUSES = new Set([ - 'succeeded', - 'success', - 'paid', - 'paid_over', - 'overpaid', - 'completed', - 'confirmed', - 'closed', -]); - -const FAILED_STATUSES = new Set([ - 'fail', - 'failed', - 'error', - 'canceled', - 'cancelled', - 'declined', - 'expired', - 'cancel', - 'system_fail', - 'refund_paid', -]); - -function isPaidStatus(status: string): boolean { - return PAID_STATUSES.has(status.toLowerCase()); -} - -function isFailedStatus(status: string): boolean { - return FAILED_STATUSES.has(status.toLowerCase()); -} - // ── Main Component ─────────────────────────────────────────── export default function TopUpResult() { diff --git a/src/utils/paymentStatus.ts b/src/utils/paymentStatus.ts new file mode 100644 index 0000000..7f360f5 --- /dev/null +++ b/src/utils/paymentStatus.ts @@ -0,0 +1,31 @@ +export const PAID_STATUSES = new Set([ + 'succeeded', + 'success', + 'paid', + 'paid_over', + 'overpaid', + 'completed', + 'confirmed', + 'closed', +]); + +export const FAILED_STATUSES = new Set([ + 'fail', + 'failed', + 'error', + 'canceled', + 'cancelled', + 'declined', + 'expired', + 'cancel', + 'system_fail', + 'refund_paid', +]); + +export function isPaidStatus(status: string): boolean { + return PAID_STATUSES.has(status.toLowerCase()); +} + +export function isFailedStatus(status: string): boolean { + return FAILED_STATUSES.has(status.toLowerCase()); +}