reduce method
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 StoreConnector
s 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 MiscState? miscState = state.miscState;
final bool hasAcceptedTerms = miscState!.acceptedTerms!;
if (phoneNumber.isEmpty) {
ScaffoldMessenger.of(context)
.showSnackBar(snackbar(content: signupPhoneNo));
return state;
}
dispatch(BatchUpdateMiscStateAction(phoneNumber: phoneNumber));
if (!hasAcceptedTerms) {
ScaffoldMessenger.of(context)
.showSnackBar(snackbar(content: allowPhoneComm));
return state;
}
final String endpoint =
AppWrapperBase.of(context)!.customContext?.verifyPhoneEndpoint ??
EndpointContext.verifyPhoneEndpoint(
AppWrapperBase.of(context)!.appContexts);
final IGraphQlClient client = AppWrapperBase.of(context)!.graphQLClient;
final Response response = await SimpleCall.callRestAPI(
endpoint: endpoint,
method: AppRequestTypes.POST.name,
graphClient: client,
variables: <String, String>{'phonenumber': phoneNumber},
raw: true,
) as Response;
final ProcessedResponse processedResponse =
processResponse(response, context);
if (processedResponse.ok) {
final CanRegisterOutput canRegister = CanRegisterOutput(
canRegister: true,
response: OTPResponse.fromJson(
json.decode(response.body) as Map<String, dynamic>));
if (canRegister.canRegister) {
dispatch(
BatchUpdateMiscStateAction(
phoneNumber: phoneNumber,
otpCode: canRegister.response!.otp,
title: verifyPhone,
message: verifyDesc(
formatPhoneNumber(phoneNumber: phoneNumber, countryCode: '')),
),
);
}
return state;
} else {
showAlertSnackBar(context: context, message: processedResponse.message);
return state;
}
}