reduce method

  1. @override
Future<CoreState?> reduce()

The reduce method is the action reducer. It may read the action state, the store state, and then return a new state (or null if no state change is necessary).

It may be synchronous (returning AppState or null) or async (returning Future<AppState> or Future<null>).

The StoreConnectors may rebuild only if the reduce method returns a state which is both not null and different from the previous one (comparing by identical, not equals).

Implementation

@override
Future<CoreState?> reduce() async {
  final IGraphQlClient _client = AppWrapperBase.of(context)!.graphQLClient;

  final Map<String, dynamic> _variables = <String, dynamic>{'pin': pin};

  final Response result = await _client.query(resumeWithPinQuery, _variables);

  final Map<String, dynamic> body = _client.toMap(result);

  // Check first for errors
  if (_client.parseError(body) != null || body['data'] == null) {
    await captureException(
      errorVerifyingPIN,
      query: resumeWithPinQuery,
      error: _client.parseError(body),
      response: body,
    );
    showErrorSnackbar(context);
    return null;
  }

  if (body['data']['resumeWithPIN'] as bool == true) {
    final OnboardingPathConfig path = onboardingPath(state: state);

    dispatch(
      BatchUpdateMiscStateAction(pinCode: UNKNOWN, invalidPin: false),
    );

    if (isChangingPin) {
      await triggerNavigationEvent(
          context: context, route: profileChangePinRoute);
    } else {
      await triggerNavigationEvent(context: context, route: path.route);
    }
  } else {
    dispatch(
      BatchUpdateMiscStateAction(pinCode: UNKNOWN, invalidPin: true),
    );
    await HapticFeedback.vibrate();

    // return the state unchanged
    return null;
  }
  return null;
}