Skip to content

Commit

Permalink
feat: filter offerings (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Jun 18, 2024
1 parent 1b44bf7 commit baf0648
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/features/countries/countries_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CountriesPage extends HookConsumerWidget {
MaterialPageRoute(
builder: (context) => PaymentAmountPage(
paymentState: PaymentState(
transactionType: TransactionType.deposit,
transactionType: TransactionType.send,
selectedCountry: country.value,
),
),
Expand Down
5 changes: 4 additions & 1 deletion lib/features/feature_flags/lucid/lucid_offerings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class LucidOfferingsPage extends HookConsumerWidget {
transactionType: TransactionType.send,
selectedOffering: selectedOffering.value,
selectedPfi: selectedPfi.value,
offeringsMap: offeringsMap,
),
),
),
Expand Down Expand Up @@ -133,7 +134,9 @@ class LucidOfferingsPage extends HookConsumerWidget {
try {
await ref
.read(tbdexServiceProvider)
.getOfferings(ref.read(pfisProvider))
.getOfferings(
const PaymentState(transactionType: TransactionType.send),
ref.read(pfisProvider),)
.then((offerings) => state.value = AsyncData(offerings));
} on Exception catch (e) {
state.value = AsyncError(e, StackTrace.current);
Expand Down
17 changes: 9 additions & 8 deletions lib/features/payment/payment_amount_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ class PaymentAmountPage extends HookConsumerWidget {
useEffect(
() {
Future.microtask(
() async =>
selectedOffering.value != null && selectedPfi.value != null
? offerings.value = AsyncData({
selectedPfi.value!: [selectedOffering.value!],
})
: await _getOfferings(ref, offerings),
() async => paymentState.offeringsMap != null
? offerings.value = AsyncData({
selectedPfi.value!: [selectedOffering.value!],
})
: await _getOfferings(ref, currentPaymentState.value, offerings),
);
return null;
},
Expand Down Expand Up @@ -141,7 +140,8 @@ class PaymentAmountPage extends HookConsumerWidget {
AsyncLoadingWidget(text: Loc.of(context).fetchingOfferings),
error: (error, stackTrace) => AsyncErrorWidget(
text: error.toString(),
onRetry: () => _getOfferings(ref, offerings),
onRetry: () =>
_getOfferings(ref, currentPaymentState.value, offerings),
),
),
),
Expand All @@ -150,13 +150,14 @@ class PaymentAmountPage extends HookConsumerWidget {

Future<void> _getOfferings(
WidgetRef ref,
PaymentState paymentState,
ValueNotifier<AsyncValue<Map<Pfi, List<Offering>>>> state,
) async {
state.value = const AsyncLoading();
try {
await ref
.read(tbdexServiceProvider)
.getOfferings(ref.read(pfisProvider))
.getOfferings(paymentState, ref.read(pfisProvider))
.then((offerings) => state.value = AsyncData(offerings));
} on Exception catch (e) {
state.value = AsyncError(e, StackTrace.current);
Expand Down
10 changes: 5 additions & 5 deletions lib/features/payment/payment_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PaymentDetailsPage extends HookConsumerWidget {

useEffect(
() {
selectedPaymentMethod.value = (filteredPaymentMethods?.length ?? 1) <= 1
selectedPaymentMethod.value = (filteredPaymentMethods?.length ?? 0) <= 1
? filteredPaymentMethods?.firstOrNull
: null;
return;
Expand Down Expand Up @@ -104,11 +104,11 @@ class PaymentDetailsPage extends HookConsumerWidget {
selectedPayinMethod: paymentState.transactionType ==
TransactionType.deposit
? selectedPaymentMethod.value as PayinMethod?
: paymentState.payinMethods?.firstOrNull,
: null,
selectedPayoutMethod: paymentState.transactionType ==
TransactionType.withdraw
? selectedPaymentMethod.value as PayoutMethod?
: paymentState.payoutMethods?.firstOrNull,
TransactionType.deposit
? null
: selectedPaymentMethod.value as PayoutMethod?,
),
onPaymentFormSubmit: (paymentState) async {
await _hasRequiredVc(
Expand Down
25 changes: 22 additions & 3 deletions lib/features/tbdex/tbdex_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,33 @@ import 'package:web5/web5.dart';
final tbdexServiceProvider = Provider((_) => TbdexService());

class TbdexService {
Future<Map<Pfi, List<Offering>>> getOfferings(List<Pfi> pfis) async {
Future<Map<Pfi, List<Offering>>> getOfferings(
PaymentState paymentState,
List<Pfi> pfis,
) async {
final offeringsMap = <Pfi, List<Offering>>{};

GetOfferingsFilter? filter;
switch (paymentState.transactionType) {
case TransactionType.deposit:
filter = GetOfferingsFilter(payoutCurrency: 'USDC');
break;
case TransactionType.withdraw:
filter = GetOfferingsFilter(payinCurrency: 'USDC');
break;
case TransactionType.send:
filter = paymentState.selectedCountry != null
? GetOfferingsFilter(payoutCurrency: 'MXN')
: null;
break;
}

for (final pfi in pfis) {
try {
await TbdexHttpClient.listOfferings(pfi.did)
await TbdexHttpClient.listOfferings(pfi.did, filter: filter)
.then((offerings) => offeringsMap[pfi] = offerings);
} on Exception {
} on Exception catch (e) {
if (e is ValidationError) continue;
rethrow;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:didpay/features/feature_flags/feature_flags_notifier.dart';
import 'package:didpay/features/feature_flags/lucid/lucid_offerings_page.dart';
import 'package:didpay/features/payment/payment_state.dart';
import 'package:didpay/features/pfis/pfis_notifier.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
import 'package:didpay/features/transaction/transaction.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
Expand All @@ -26,10 +28,16 @@ void main() async {
mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]);

when(
() => mockTbdexService.getOfferings(pfis),
() => mockTbdexService.getOfferings(any(), pfis),
).thenAnswer((_) async => offerings);
});

setUpAll(
() => registerFallbackValue(
const PaymentState(transactionType: TransactionType.send),
),
);

group('LucidOfferingsPage', () {
Widget lucidOfferingsPageTestWidget() => WidgetHelpers.testableWidget(
child: const LucidOfferingsPage(),
Expand Down
10 changes: 9 additions & 1 deletion test/features/home/home_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import 'package:didpay/features/account/account_balance_notifier.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/home/home_page.dart';
import 'package:didpay/features/payment/payment_amount_page.dart';
import 'package:didpay/features/payment/payment_state.dart';
import 'package:didpay/features/pfis/pfis_notifier.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
import 'package:didpay/features/transaction/transaction.dart';
import 'package:didpay/features/transaction/transaction_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -44,7 +46,7 @@ void main() async {
mockPfisNotifier = MockPfisNotifier(pfis);

when(
() => mockTbdexService.getOfferings(pfis),
() => mockTbdexService.getOfferings(any(), pfis),
).thenAnswer((_) async => offerings);

when(
Expand All @@ -59,6 +61,12 @@ void main() async {
);
});

setUpAll(
() => registerFallbackValue(
const PaymentState(transactionType: TransactionType.deposit),
),
);

testWidgets('should show account balance', (tester) async {
await tester.pumpWidget(homePageTestWidget());

Expand Down
8 changes: 7 additions & 1 deletion test/features/payment/payment_amount_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ void main() async {
mockPfisNotifier = MockPfisNotifier(pfis);

when(
() => mockTbdexService.getOfferings(pfis),
() => mockTbdexService.getOfferings(any(), pfis),
).thenAnswer((_) async => offerings);
});

setUpAll(
() => registerFallbackValue(
const PaymentState(transactionType: TransactionType.deposit),
),
);

group('PaymentAmountPage', () {
Widget paymentAmountPageTestWidget() => WidgetHelpers.testableWidget(
child: const PaymentAmountPage(
Expand Down

0 comments on commit baf0648

Please sign in to comment.