useEndpointResend function

Future<void> useEndpointResend({
  1. required BuildContext context,
  2. required dynamic appWrapperContext,
  3. required String phoneNumber,
  4. required int step,
  5. required Function retrySendOtpEndpoint,
  6. required Function toggleResend,
  7. required Function showErr,
  8. required Function resetTimer,
  9. required OnOTPReceivedNavigateCallback onOTPCallback,
  10. Client? httpClient,
})

useEndpointResend send a resend otp request via REST endpoint. This function should only be used in the context of SILResendPhoneCode widget. It's extracted here for testability

Implementation

Future<void> useEndpointResend(
    {required BuildContext context,
    required dynamic appWrapperContext,
    required String phoneNumber,
    required int step,
    required Function retrySendOtpEndpoint,
    required Function toggleResend,
    required Function showErr,
    required Function resetTimer,
    required OnOTPReceivedNavigateCallback onOTPCallback,
    Client? httpClient}) async {
  final Client client = httpClient ?? Client();

  toggleResend();
  showErr(val: false);
  try {
    final Response response = await client.post(
      Uri.parse(retrySendOtpEndpoint(appWrapperContext).toString()),
      body: json.encode(<String, dynamic>{
        'phoneNumber': phoneNumber,
        'retryStep': step,
      }),
      headers: requestHeaders,
    );

    // reset the timer
    resetTimer();

    // return the new otp
    // Navigator.pop(context, json.decode(response.body)['otp']  );
    final Map<String, dynamic> body =
        json.decode(response.body) as Map<String, dynamic>;

    onOTPCallback(body['otp'] as String);
    toggleResend();
  } catch (e) {
    toggleResend();
    showErr(val: true);
  }
}