sign_up_bloc.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'package:appflowy/user/application/auth_service.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
  6. show UserProfilePB;
  7. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  8. import 'package:freezed_annotation/freezed_annotation.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. import 'package:appflowy/generated/locale_keys.g.dart';
  11. part 'sign_up_bloc.freezed.dart';
  12. class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
  13. final AuthService authService;
  14. SignUpBloc(this.authService) : super(SignUpState.initial()) {
  15. on<SignUpEvent>((event, emit) async {
  16. await event.map(
  17. signUpWithUserEmailAndPassword: (e) async {
  18. await _performActionOnSignUp(emit);
  19. },
  20. emailChanged: (_EmailChanged value) async {
  21. emit(
  22. state.copyWith(
  23. email: value.email,
  24. emailError: none(),
  25. successOrFail: none(),
  26. ),
  27. );
  28. },
  29. passwordChanged: (_PasswordChanged value) async {
  30. emit(
  31. state.copyWith(
  32. password: value.password,
  33. passwordError: none(),
  34. successOrFail: none(),
  35. ),
  36. );
  37. },
  38. repeatPasswordChanged: (_RepeatPasswordChanged value) async {
  39. emit(
  40. state.copyWith(
  41. repeatedPassword: value.password,
  42. repeatPasswordError: none(),
  43. successOrFail: none(),
  44. ),
  45. );
  46. },
  47. );
  48. });
  49. }
  50. Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async {
  51. emit(
  52. state.copyWith(
  53. isSubmitting: true,
  54. successOrFail: none(),
  55. ),
  56. );
  57. final password = state.password;
  58. final repeatedPassword = state.repeatedPassword;
  59. if (password == null) {
  60. emit(
  61. state.copyWith(
  62. isSubmitting: false,
  63. passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
  64. ),
  65. );
  66. return;
  67. }
  68. if (repeatedPassword == null) {
  69. emit(
  70. state.copyWith(
  71. isSubmitting: false,
  72. repeatPasswordError:
  73. some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
  74. ),
  75. );
  76. return;
  77. }
  78. if (password != repeatedPassword) {
  79. emit(
  80. state.copyWith(
  81. isSubmitting: false,
  82. repeatPasswordError:
  83. some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
  84. ),
  85. );
  86. return;
  87. }
  88. emit(
  89. state.copyWith(
  90. passwordError: none(),
  91. repeatPasswordError: none(),
  92. ),
  93. );
  94. final result = await authService.signUp(
  95. name: state.email,
  96. password: state.password,
  97. email: state.email,
  98. );
  99. emit(
  100. result.fold(
  101. (profile) => state.copyWith(
  102. isSubmitting: false,
  103. successOrFail: some(left(profile)),
  104. emailError: none(),
  105. passwordError: none(),
  106. repeatPasswordError: none(),
  107. ),
  108. (error) => stateFromCode(error),
  109. ),
  110. );
  111. }
  112. SignUpState stateFromCode(FlowyError error) {
  113. switch (ErrorCode.valueOf(error.code)!) {
  114. case ErrorCode.EmailFormatInvalid:
  115. return state.copyWith(
  116. isSubmitting: false,
  117. emailError: some(error.msg),
  118. passwordError: none(),
  119. successOrFail: none(),
  120. );
  121. case ErrorCode.PasswordFormatInvalid:
  122. return state.copyWith(
  123. isSubmitting: false,
  124. passwordError: some(error.msg),
  125. emailError: none(),
  126. successOrFail: none(),
  127. );
  128. default:
  129. return state.copyWith(
  130. isSubmitting: false,
  131. successOrFail: some(right(error)),
  132. );
  133. }
  134. }
  135. }
  136. @freezed
  137. class SignUpEvent with _$SignUpEvent {
  138. const factory SignUpEvent.signUpWithUserEmailAndPassword() =
  139. SignUpWithUserEmailAndPassword;
  140. const factory SignUpEvent.emailChanged(String email) = _EmailChanged;
  141. const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged;
  142. const factory SignUpEvent.repeatPasswordChanged(String password) =
  143. _RepeatPasswordChanged;
  144. }
  145. @freezed
  146. class SignUpState with _$SignUpState {
  147. const factory SignUpState({
  148. String? email,
  149. String? password,
  150. String? repeatedPassword,
  151. required bool isSubmitting,
  152. required Option<String> passwordError,
  153. required Option<String> repeatPasswordError,
  154. required Option<String> emailError,
  155. required Option<Either<UserProfilePB, FlowyError>> successOrFail,
  156. }) = _SignUpState;
  157. factory SignUpState.initial() => SignUpState(
  158. isSubmitting: false,
  159. passwordError: none(),
  160. repeatPasswordError: none(),
  161. emailError: none(),
  162. successOrFail: none(),
  163. );
  164. }