Skip to content

Commit

Permalink
chore: refactor onSubmit() callback in JsonSchemaForm (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Aug 15, 2024
1 parent 2d61c65 commit aa683fd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 90 deletions.
150 changes: 64 additions & 86 deletions lib/features/payment/payment_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:tbdex/tbdex.dart';
import 'package:web5/web5.dart';

class PaymentDetailsPage extends HookConsumerWidget {
final PaymentState paymentState;
Expand All @@ -35,11 +36,11 @@ class PaymentDetailsPage extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final quote = useState<AsyncValue<Quote?>>(ref.watch(quoteProvider));

final rfq = useState<AsyncValue<Rfq>?>(null);
final state = useState<PaymentDetailsState>(
paymentState.paymentDetailsState ?? PaymentDetailsState(),
);

final availableMethods =
state.value.filterPaymentMethods(state.value.selectedPaymentType);

Expand Down Expand Up @@ -97,52 +98,43 @@ class PaymentDetailsPage extends HookConsumerWidget {
),
),
)
: _buildPage(context, ref, availableMethods, state, rfq),
: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Header(
title:
paymentState.transactionType == TransactionType.send
? state.value.moneyAddresses != null
? Loc.of(context).checkTheirPaymentDetails
: Loc.of(context).enterTheirPaymentDetails
: Loc.of(context).enterYourPaymentDetails,
subtitle: Loc.of(context).makeSureInfoIsCorrect,
),
if (state.value.hasMultiplePaymentTypes)
_buildPaymentTypeSelector(
context,
state,
),
if (state.value.hasNoPaymentTypes ||
state.value.selectedPaymentType != null)
_buildPaymentMethodSelector(
context,
availableMethods,
state,
),
_buildPaymentForm(
context,
ref,
state,
rfq,
),
],
),
),
),
);
}

Widget _buildPage(
BuildContext context,
WidgetRef ref,
List<PaymentMethod>? availableMethods,
ValueNotifier<PaymentDetailsState> state,
ValueNotifier<AsyncValue<Rfq>?> rfq,
) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Header(
title: paymentState.transactionType == TransactionType.send
? state.value.moneyAddresses != null
? Loc.of(context).checkTheirPaymentDetails
: Loc.of(context).enterTheirPaymentDetails
: Loc.of(context).enterYourPaymentDetails,
subtitle: Loc.of(context).makeSureInfoIsCorrect,
),
if (state.value.hasMultiplePaymentTypes)
_buildPaymentTypeSelector(
context,
state,
),
if (state.value.hasNoPaymentTypes ||
state.value.selectedPaymentType != null)
_buildPaymentMethodSelector(
context,
availableMethods,
state,
),
_buildPaymentForm(
context,
ref,
state,
rfq,
),
],
);
}

Widget _buildPaymentForm(
BuildContext context,
WidgetRef ref,
Expand All @@ -152,14 +144,31 @@ class PaymentDetailsPage extends HookConsumerWidget {
Expanded(
child: JsonSchemaForm(
state: state.value,
onSubmit: (formData) {
onSubmit: (formData) async {
state.value = state.value.copyWith(formData: formData);
_checkCredsAndSendRfq(
context,
ref,
rfq,
state,
);

final presentationDefinition = paymentState
.paymentAmountState?.selectedOffering?.data.requiredClaims;

if (presentationDefinition != null) {
final credentials = await _getRequiredCredentials(
context, ref, presentationDefinition,);

if (credentials == null) {
return;
}

state.value = state.value.copyWith(credentialsJwt: credentials);
}

if (context.mounted) {
await _sendRfq(
context,
ref,
state.value,
rfq,
);
}
},
),
);
Expand Down Expand Up @@ -247,24 +256,6 @@ class PaymentDetailsPage extends HookConsumerWidget {
);
}

Future<void> _checkCredsAndSendRfq(
BuildContext context,
WidgetRef ref,
ValueNotifier<AsyncValue<Rfq>?> rfq,
ValueNotifier<PaymentDetailsState> state,
) async {
final hasRequiredVc = await _hasRequiredVc(context, ref, state);

if (hasRequiredVc && context.mounted) {
await _sendRfq(
context,
ref,
state.value,
rfq,
);
}
}

Future<void> _sendRfq(
BuildContext context,
WidgetRef ref,
Expand Down Expand Up @@ -297,27 +288,18 @@ class PaymentDetailsPage extends HookConsumerWidget {
}
}

Future<bool> _hasRequiredVc(
Future<List<String>?> _getRequiredCredentials(
BuildContext context,
WidgetRef ref,
ValueNotifier<PaymentDetailsState> state,
PresentationDefinition presentationDefinition,
) async {
final presentationDefinition =
paymentState.paymentAmountState?.selectedOffering?.data.requiredClaims;
final credentials =
presentationDefinition?.selectCredentials(ref.read(vcsProvider));

if (credentials == null && presentationDefinition == null) {
return true;
}
presentationDefinition.selectCredentials(ref.read(vcsProvider));

if (credentials != null && credentials.isNotEmpty) {
state.value = state.value.copyWith(credentialsJwt: credentials);
return true;
if (credentials.isNotEmpty) {
return credentials;
}

if (paymentState.paymentAmountState?.pfiDid == null) return false;

final issuedCredential = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ModalFlow(
Expand All @@ -329,10 +311,6 @@ class PaymentDetailsPage extends HookConsumerWidget {
),
);

if (issuedCredential == null) return false;

state.value =
state.value.copyWith(credentialsJwt: [issuedCredential as String]);
return true;
return issuedCredential == null ? null : [issuedCredential as String];
}
}
2 changes: 1 addition & 1 deletion lib/shared/json_schema_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:mask_text_input_formatter/mask_text_input_formatter.dart';

class JsonSchemaForm extends HookWidget {
final PaymentDetailsState state;
final void Function(Map<String, String>) onSubmit;
final Future<void> Function(Map<String, String>) onSubmit;

final _formKey = GlobalKey<FormState>();

Expand Down
6 changes: 3 additions & 3 deletions test/shared/json_schema_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ void main() {

group('JsonSchemaForm', () {
Widget jsonSchemaFormTestWidget({
void Function(Map<String, String>)? onSubmit,
Future<void> Function(Map<String, String>)? onSubmit,
}) =>
WidgetHelpers.testableWidget(
child: JsonSchemaForm(
state: PaymentDetailsState(
selectedPaymentMethod:
PaymentMethod(kind: '', schema: jsonSchemaString),
),
onSubmit: onSubmit ?? (_) {},
onSubmit: onSubmit ?? (_) async {},
),
);
testWidgets('should render form fields based on JSON schema',
Expand Down Expand Up @@ -70,7 +70,7 @@ void main() {

await tester.pumpWidget(
jsonSchemaFormTestWidget(
onSubmit: (formData) => submittedData = formData,
onSubmit: (formData) async => submittedData = formData,
),
);

Expand Down

0 comments on commit aa683fd

Please sign in to comment.