emailCode property

TextFieldState emailCode
latefinal

TextFieldState of a ConfirmationCode for email.

Implementation

late final TextFieldState emailCode = TextFieldState(
  onSubmitted: (s) async {
    s.status.value = RxStatus.loading();
    try {
      final bool authorized = _authService.isAuthorized();

      await _authService.signIn(
        email: UserEmail(email.text),
        code: ConfirmationCode(emailCode.text),
        force: true,
        // removeAfterwards: userId,
      );

      // TODO: This is a hack that should be removed, as whenever the account
      //       is changed, the [HomeView] and its dependencies must be
      //       rebuilt, which may take some unidentifiable amount of time as
      //       of now.
      if (authorized) {
        router.nowhere();
        await Future.delayed(const Duration(milliseconds: 500));
      }

      router.home(signedUp: true);
    } on CreateSessionException catch (e) {
      switch (e.code) {
        case CreateSessionErrorCode.wrongCode:
          s.error.value = e.toMessage();

          ++codeAttempts;
          if (codeAttempts >= 3) {
            codeAttempts = 0;
            _setCodeTimer();
          }
          s.status.value = RxStatus.empty();
          break;

        default:
          s.error.value = 'err_wrong_code'.l10n;
          break;
      }
    } on FormatException catch (_) {
      s.error.value = 'err_wrong_code'.l10n;
      s.status.value = RxStatus.empty();
      ++codeAttempts;
      if (codeAttempts >= 3) {
        codeAttempts = 0;
        _setCodeTimer();
      }
    } catch (_) {
      s.resubmitOnError.value = true;
      s.error.value = 'err_data_transfer'.l10n;
      s.status.value = RxStatus.empty();
      s.unsubmit();
      rethrow;
    }
  },
);