Skip to content

Commit

Permalink
feat: save OrderInstructions state (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Aug 8, 2024
1 parent 56f845d commit 94fa758
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 44 deletions.
18 changes: 18 additions & 0 deletions lib/features/payment/payment_confirmation_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/confirmation_message.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

class PaymentConfirmationPage extends HookWidget {
const PaymentConfirmationPage({super.key});

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(),
body: SafeArea(
child: ConfirmationMessage(
message: Loc.of(context).orderConfirmed,
),
),
);
}
30 changes: 6 additions & 24 deletions lib/features/payment/payment_fetch_instructions_widget.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:didpay/features/payment/payment_link_webview_page.dart';
import 'package:didpay/features/payment/payment_state.dart';
import 'package:didpay/features/tbdex/tbdex_order_instructions_notifier.dart';
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/confirmation_message.dart';
import 'package:didpay/shared/error_message.dart';
import 'package:didpay/shared/loading_message.dart';
import 'package:flutter/material.dart';
Expand All @@ -14,12 +12,15 @@ class PaymentFetchInstructionsWidget extends HookWidget {
final PaymentState paymentState;
final ValueNotifier<AsyncValue<OrderInstructions?>> instructions;
final ValueNotifier<AsyncValue<Order>?> order;
final Future<void> Function(BuildContext, OrderInstructions)
onInstructionsFetched;
final WidgetRef ref;

const PaymentFetchInstructionsWidget({
required this.paymentState,
required this.instructions,
required this.order,
required this.onInstructionsFetched,
required this.ref,
super.key,
});
Expand All @@ -44,7 +45,7 @@ class PaymentFetchInstructionsWidget extends HookWidget {
return instructions.value.when(
data: (q) => Container(),
loading: () => LoadingMessage(
message: Loc.of(context).confirmingYourOrder,
message: Loc.of(context).fetchingPaymentInstructions,
),
error: (error, _) => ErrorMessage(
message: error.toString(),
Expand All @@ -70,29 +71,10 @@ class PaymentFetchInstructionsWidget extends HookWidget {
if (context.mounted && fetchedInstructions != null) {
instructionsNotifier.stopPolling();

final paymentLink = fetchedInstructions.data.payin.link;

if (paymentLink == null) {
await Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => ConfirmationMessage(
message: Loc.of(context).orderConfirmed,
),
),
(route) => false,
);
} else {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PaymentLinkWebviewPage(
paymentLink: fetchedInstructions.data.payin.link ?? '',
),
),
);
}
await onInstructionsFetched(context, fetchedInstructions);

if (context.mounted) {
instructions.value = const AsyncData(null);
instructions.value = AsyncData(fetchedInstructions);
order.value = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/features/payment/payment_fetch_quote_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PaymentFetchQuoteWidget extends HookWidget {
return quote.value.when(
data: (q) => Container(),
loading: () => LoadingMessage(
message: Loc.of(context).gettingYourQuote,
message: Loc.of(context).fetchingYourQuote,
),
error: (error, _) => ErrorMessage(
message: error.toString(),
Expand Down
7 changes: 2 additions & 5 deletions lib/features/payment/payment_link_webview_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:didpay/l10n/app_localizations.dart';
import 'package:didpay/shared/confirmation_message.dart';
import 'package:didpay/features/payment/payment_confirmation_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Expand Down Expand Up @@ -42,9 +41,7 @@ class PaymentLinkWebviewPage extends HookConsumerWidget {
if (context.mounted) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => ConfirmationMessage(
message: Loc.of(context).orderConfirmed,
),
builder: (context) => const PaymentConfirmationPage(),
),
(route) => false,
);
Expand Down
41 changes: 37 additions & 4 deletions lib/features/payment/payment_review_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:decimal/decimal.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/payment/payment_confirmation_page.dart';
import 'package:didpay/features/payment/payment_fee_details.dart';
import 'package:didpay/features/payment/payment_fetch_instructions_widget.dart';
import 'package:didpay/features/payment/payment_link_webview_page.dart';
import 'package:didpay/features/payment/payment_state.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
import 'package:didpay/features/transaction/transaction.dart';
Expand Down Expand Up @@ -59,10 +61,11 @@ class PaymentReviewPage extends HookConsumerWidget {
paymentState: paymentState,
instructions: orderInstructions,
order: order,
onInstructionsFetched: _handleFetchedInstructions,
ref: ref,
),
loading: () => LoadingMessage(
message: Loc.of(context).confirmingYourOrder,
message: Loc.of(context).sendingYourOrder,
),
error: (error, _) => ErrorMessage(
message: error.toString(),
Expand Down Expand Up @@ -94,16 +97,22 @@ class PaymentReviewPage extends HookConsumerWidget {
const SizedBox(height: Grid.sm),
_buildAmounts(context, paymentState.quote?.data),
_buildFeeDetails(
context, paymentState.quote?.data,),
context,
paymentState.quote?.data,
),
_buildPaymentDetails(context),
],
),
),
),
),
NextButton(
onPressed: () =>
_submitOrder(context, ref, paymentState, order),
onPressed: () => orderInstructions.value.hasValue
? _handleFetchedInstructions(
context,
orderInstructions.value.asData!.value,
)
: _submitOrder(context, ref, paymentState, order),
title:
'${Loc.of(context).pay} ${PaymentFeeDetails.calculateTotalAmount(paymentState.quote?.data)} ${paymentState.quote?.data.payin.currencyCode}',
),
Expand Down Expand Up @@ -253,4 +262,28 @@ class PaymentReviewPage extends HookConsumerWidget {
state.value = AsyncError(e, StackTrace.current);
}
}

Future<void> _handleFetchedInstructions(
BuildContext context,
OrderInstructions? fetchedInstructions,
) async {
final paymentLink = fetchedInstructions?.data.payin.link;

if (paymentLink == null) {
await Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => const PaymentConfirmationPage(),
),
(route) => false,
);
} else {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PaymentLinkWebviewPage(
paymentLink: paymentLink,
),
),
);
}
}
}
5 changes: 3 additions & 2 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@
"pfiAdded": "PFI added!",
"addingPfi": "Adding PFI...",
"mustAddPfiBeforeSending": "Must add a PFI before sending funds!",
"gettingYourQuote": "Getting your quote...",
"fetchingYourQuote": "Fetching your quote...",
"fetchingPaymentInstructions": "Fetching payment instructions...",
"sendingYourRequest": "Sending your request...",
"confirmingYourOrder": "Confirming your order...",
"sendingYourOrder": "Sending your order...",
"orderConfirmed": "Order confirmed!",
"removePfi": "Remove PFI",
"cancel": "Cancel",
Expand Down
18 changes: 12 additions & 6 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -679,23 +679,29 @@ abstract class Loc {
/// **'Must add a PFI before sending funds!'**
String get mustAddPfiBeforeSending;

/// No description provided for @gettingYourQuote.
/// No description provided for @fetchingYourQuote.
///
/// In en, this message translates to:
/// **'Getting your quote...'**
String get gettingYourQuote;
/// **'Fetching your quote...'**
String get fetchingYourQuote;

/// No description provided for @fetchingPaymentInstructions.
///
/// In en, this message translates to:
/// **'Fetching payment instructions...'**
String get fetchingPaymentInstructions;

/// No description provided for @sendingYourRequest.
///
/// In en, this message translates to:
/// **'Sending your request...'**
String get sendingYourRequest;

/// No description provided for @confirmingYourOrder.
/// No description provided for @sendingYourOrder.
///
/// In en, this message translates to:
/// **'Confirming your order...'**
String get confirmingYourOrder;
/// **'Sending your order...'**
String get sendingYourOrder;

/// No description provided for @orderConfirmed.
///
Expand Down
7 changes: 5 additions & 2 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,16 @@ class LocEn extends Loc {
String get mustAddPfiBeforeSending => 'Must add a PFI before sending funds!';

@override
String get gettingYourQuote => 'Getting your quote...';
String get fetchingYourQuote => 'Fetching your quote...';

@override
String get fetchingPaymentInstructions => 'Fetching payment instructions...';

@override
String get sendingYourRequest => 'Sending your request...';

@override
String get confirmingYourOrder => 'Confirming your order...';
String get sendingYourOrder => 'Sending your order...';

@override
String get orderConfirmed => 'Order confirmed!';
Expand Down

0 comments on commit 94fa758

Please sign in to comment.