From 6ba12c1573afe13cff2f0f0fdf0c2a450ae01c69 Mon Sep 17 00:00:00 2001 From: Ethan Lee <125412902+ethan-tbd@users.noreply.github.com> Date: Sun, 19 May 2024 09:07:19 -0700 Subject: [PATCH] fix: transactions status and time rendering (#162) --- lib/features/home/home_page.dart | 3 +- lib/features/home/transaction.dart | 28 ++++++------- .../home/transaction_details_page.dart | 42 +++++++------------ lib/features/payment/review_payment_page.dart | 11 +++-- lib/features/payout/payout.dart | 4 +- lib/features/tbdex/tbdex_service.dart | 2 + .../home/transaction_details_page_test.dart | 12 +++--- 7 files changed, 46 insertions(+), 56 deletions(-) diff --git a/lib/features/home/home_page.dart b/lib/features/home/home_page.dart index bc88cc56..71afd7bb 100644 --- a/lib/features/home/home_page.dart +++ b/lib/features/home/home_page.dart @@ -218,8 +218,7 @@ class HomePage extends HookConsumerWidget { List exchanges, ) => ListView( - reverse: true, - children: exchanges.map((exchange) { + children: exchanges.reversed.map((exchange) { final transaction = Transaction.fromExchange(exchange); return ListTile( diff --git a/lib/features/home/transaction.dart b/lib/features/home/transaction.dart index 195d8e3c..3ca0eb92 100644 --- a/lib/features/home/transaction.dart +++ b/lib/features/home/transaction.dart @@ -7,8 +7,8 @@ class Transaction { final double payoutAmount; final String payinCurrency; final String payoutCurrency; + final String status; final DateTime createdAt; - final TransactionStatus status; final TransactionType type; Transaction({ @@ -16,8 +16,8 @@ class Transaction { required this.payoutAmount, required this.payinCurrency, required this.payoutCurrency, - required this.createdAt, required this.status, + required this.createdAt, required this.type, }); @@ -26,32 +26,39 @@ class Transaction { var payoutAmount = '0'; var payinCurrency = ''; var payoutCurrency = ''; + var status = ''; var latestCreatedAt = DateTime.fromMillisecondsSinceEpoch(0); - var status = TransactionStatus.pending; var type = TransactionType.send; for (final msg in exchange) { switch (msg.metadata.kind) { case MessageKind.rfq: + status = 'Request submitted'; payinAmount = (msg as Rfq).data.payin.amount; break; case MessageKind.quote: + status = 'Quote received'; payinAmount = (msg as Quote).data.payin.amount; payoutAmount = msg.data.payout.amount; payinCurrency = msg.data.payin.currencyCode; payoutCurrency = msg.data.payout.currencyCode; break; case MessageKind.order: - status = TransactionStatus.completed; + status = 'Order submitted'; break; // TODO(ethan-tbd): add additional order statuses case MessageKind.orderstatus: - status = TransactionStatus.completed; + status = (msg as OrderStatus).data.orderStatus; break; case MessageKind.close: - status = TransactionStatus.failed; + status = 'Canceled'; break; } + + final createdAt = DateTime.parse(msg.metadata.createdAt); + if (createdAt.isAfter(latestCreatedAt)) { + latestCreatedAt = createdAt; + } } type = (payinCurrency == 'STORED_BALANCE') @@ -83,15 +90,6 @@ class Transaction { } } -enum TransactionStatus { - failed, - pending, - completed; - - @override - String toString() => name.substring(0, 1).toUpperCase() + name.substring(1); -} - enum TransactionType { deposit, withdraw, diff --git a/lib/features/home/transaction_details_page.dart b/lib/features/home/transaction_details_page.dart index 5917022e..a4e1fef9 100644 --- a/lib/features/home/transaction_details_page.dart +++ b/lib/features/home/transaction_details_page.dart @@ -1,10 +1,10 @@ import 'package:auto_size_text/auto_size_text.dart'; import 'package:didpay/features/home/transaction.dart'; -import 'package:didpay/l10n/app_localizations.dart'; import 'package:didpay/shared/theme/grid.dart'; import 'package:didpay/shared/utils/currency_util.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:intl/intl.dart'; class TransactionDetailsPage extends HookWidget { final Transaction txn; @@ -132,7 +132,7 @@ class TransactionDetailsPage extends HookWidget { ), const SizedBox(height: Grid.xxs), Text( - 'Mar 1 at 10:00 am', + DateFormat("MMM dd 'at' hh:mm a").format(txn.createdAt.toLocal()), style: Theme.of(context).textTheme.titleMedium?.copyWith( color: Theme.of(context).colorScheme.onSurface, ), @@ -151,33 +151,23 @@ class TransactionDetailsPage extends HookWidget { ), splashFactory: NoSplash.splashFactory, ), - child: _getStatusText(context, txn.status), + child: _formatTransactionStatus(context, txn.status), ), ); - Text _getStatusText(BuildContext context, TransactionStatus status) { - switch (status) { - case TransactionStatus.pending: - return Text( - Loc.of(context).pending, - style: Theme.of(context).textTheme.titleMedium?.copyWith( - color: Theme.of(context).colorScheme.onSurface, - ), - ); - case TransactionStatus.failed: - return Text( - Loc.of(context).failed, - style: Theme.of(context).textTheme.titleMedium?.copyWith( - color: Theme.of(context).colorScheme.error, - ), - ); - case TransactionStatus.completed: - return Text( - Loc.of(context).completed, - style: Theme.of(context).textTheme.titleMedium?.copyWith( - color: Theme.of(context).colorScheme.tertiary, - ), - ); + Text _formatTransactionStatus(BuildContext context, String status) { + var formattedStatus = status.replaceAll('_', ' ').toLowerCase(); + + if (formattedStatus.isNotEmpty) { + formattedStatus = + formattedStatus[0].toUpperCase() + formattedStatus.substring(1); } + + return Text( + formattedStatus, + style: Theme.of(context).textTheme.titleMedium?.copyWith( + color: Theme.of(context).colorScheme.tertiary, + ), + ); } } diff --git a/lib/features/payment/review_payment_page.dart b/lib/features/payment/review_payment_page.dart index fc27d33f..caee33e3 100644 --- a/lib/features/payment/review_payment_page.dart +++ b/lib/features/payment/review_payment_page.dart @@ -55,10 +55,13 @@ class ReviewPaymentPage extends HookConsumerWidget { ), error: (error, _) => AsyncErrorWidget( text: error.toString(), - onRetry: () => ref.read(quoteProvider.notifier).startPolling( - exchangeId, - paymentState.pfi, - ), + onRetry: () => submitOrder( + context, + ref, + paymentState, + exchangeId, + sendOrderState, + ), ), ) : getQuoteState.when( diff --git a/lib/features/payout/payout.dart b/lib/features/payout/payout.dart index 4bb2dda0..92333d90 100644 --- a/lib/features/payout/payout.dart +++ b/lib/features/payout/payout.dart @@ -43,9 +43,7 @@ class Payout extends HookWidget { WidgetsBinding.instance.addPostFrameCallback((_) { if (selectedOffering.value == null) return; - payoutAmount.value = transactionType == TransactionType.deposit - ? payinAmount / exchangeRate - : payinAmount * exchangeRate; + payoutAmount.value = payinAmount * exchangeRate; }); return; diff --git a/lib/features/tbdex/tbdex_service.dart b/lib/features/tbdex/tbdex_service.dart index a3d3096a..5a1f8131 100644 --- a/lib/features/tbdex/tbdex_service.dart +++ b/lib/features/tbdex/tbdex_service.dart @@ -78,6 +78,7 @@ class TbdexService { ), ); await rfq.sign(did); + await Future.delayed(const Duration(seconds: 1)); final response = await TbdexHttpClient.createExchange(rfq, replyTo: rfq.metadata.from); @@ -93,6 +94,7 @@ class TbdexService { Future submitOrder(BearerDid did, Pfi pfi, String exchangeId) async { final order = Order.create(pfi.did, did.uri, exchangeId); await order.sign(did); + await Future.delayed(const Duration(seconds: 1)); final response = await TbdexHttpClient.submitOrder(order); diff --git a/test/features/home/transaction_details_page_test.dart b/test/features/home/transaction_details_page_test.dart index c7f1fb65..77191c21 100644 --- a/test/features/home/transaction_details_page_test.dart +++ b/test/features/home/transaction_details_page_test.dart @@ -16,8 +16,8 @@ void main() { payoutAmount: 0, payinCurrency: 'USDC', payoutCurrency: 'USDC', + status: '', createdAt: DateTime.now(), - status: TransactionStatus.completed, type: TransactionType.deposit, ), ), @@ -36,8 +36,8 @@ void main() { payoutAmount: 0, payinCurrency: 'USDC', payoutCurrency: 'USDC', + status: '', createdAt: DateTime.now(), - status: TransactionStatus.completed, type: TransactionType.withdraw, ), ), @@ -56,8 +56,8 @@ void main() { payoutAmount: 123, payinCurrency: 'USDC', payoutCurrency: 'MXN', + status: '', createdAt: DateTime.now(), - status: TransactionStatus.completed, type: TransactionType.deposit, ), ), @@ -79,8 +79,8 @@ void main() { payoutAmount: 123, payinCurrency: 'USDC', payoutCurrency: 'USDC', + status: 'Pending', createdAt: DateTime.now(), - status: TransactionStatus.pending, type: TransactionType.deposit, ), ), @@ -99,8 +99,8 @@ void main() { payoutAmount: 123, payinCurrency: 'USDC', payoutCurrency: 'USDC', + status: 'Completed', createdAt: DateTime.now(), - status: TransactionStatus.completed, type: TransactionType.deposit, ), ), @@ -119,8 +119,8 @@ void main() { payoutAmount: 123, payinCurrency: 'USDC', payoutCurrency: 'USDC', + status: 'Failed', createdAt: DateTime.now(), - status: TransactionStatus.failed, type: TransactionType.deposit, ), ),