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 {
  /// Call endpoint and get back profile and custom token
  final String createAccountEndpoint =
      AppWrapperBase.of(context)!.customContext?.createUserByPhoneEndpoint ??
          EndpointContext.createUserByPhoneEndpoint(
              AppWrapperBase.of(this.context)!.appContexts);

  final IGraphQlClient client = AppWrapperBase.of(context)!.graphQLClient;

  final Map<String, String> variables = <String, String>{
    'phoneNumber': state.miscState!.phoneNumber!,
    'otp': state.miscState!.otpCode!,
    'flavour': Flavour.PRO.name,
    'pin': state.miscState!.pinCode!,
  };

  final ProcessedResponse processedResponse = processResponse(
      await SimpleCall.callRestAPI(
        endpoint: createAccountEndpoint,
        method: AppRequestTypes.POST.name,
        variables: variables,
        graphClient: client,
        raw: true,
      ) as Response,
      context);

  if (processedResponse.ok) {
    final Response response = processedResponse.response;

    // decode the response
    final Map<String, dynamic> responseMap =
        json.decode(response.body) as Map<String, dynamic>;

    final UserResponse responseAsObject = UserResponse.fromJson(responseMap);

    final UserState? newUserState = store.state.userState?.copyWith.call(
      userProfile: responseAsObject.profile,
      customerProfile: responseAsObject.customerProfile,
      communicationSettings: responseAsObject.communicationSettings,
      auth: responseAsObject.auth,
      isSignedIn: true,
      inActivitySetInTime: DateTime.now().toIso8601String(),
      signedInTime: DateTime.now().toIso8601String(),
    );

    final CoreState newState = state.copyWith(userState: newUserState);

    await StoreProvider.dispatch<CoreState>(
      context,
      NavigationAction(
          drawerSelectedIndex:
              responseAsObject.navigation?.drawerSelectedIndex,
          primaryActions: responseAsObject.navigation?.primaryActions,
          secondaryActions: responseAsObject.navigation?.secondaryActions),
    );

    await saveDeviceToken(client: client, fcm: SILFCM());

    final OnboardingPathConfig path = onboardingPath(state: state);

    triggerEvent(signupEvent, context);

    dispatch(NavigateAction<CoreState>.pushNamedAndRemoveAll(path.route));

    return newState;
  } else {
    await captureException(
      errorPhoneSignup,
      error: processedResponse.message,
      response: processedResponse.response.body,
    );
    throw UserException(processedResponse.message);
  }
}