182 lines
4.9 KiB
Dart
Raw Normal View History

import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/user/application/auth/auth_service.dart';
import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
show UserProfilePB;
import 'package:appflowy_result/appflowy_result.dart';
import 'package:easy_localization/easy_localization.dart';
2021-07-13 13:14:49 +08:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
2021-07-13 13:14:49 +08:00
2021-09-05 22:52:20 +08:00
part 'sign_up_bloc.freezed.dart';
2021-07-13 13:14:49 +08:00
2021-09-05 22:52:20 +08:00
class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
SignUpBloc(this.authService) : super(SignUpState.initial()) {
_dispatch();
}
final AuthService authService;
void _dispatch() {
on<SignUpEvent>(
(event, emit) async {
await event.map(
signUpWithUserEmailAndPassword: (e) async {
await _performActionOnSignUp(emit);
},
emailChanged: (_EmailChanged value) async {
emit(
state.copyWith(
email: value.email,
emailError: null,
successOrFail: null,
),
);
},
passwordChanged: (_PasswordChanged value) async {
emit(
state.copyWith(
password: value.password,
passwordError: null,
successOrFail: null,
),
);
},
repeatPasswordChanged: (_RepeatPasswordChanged value) async {
emit(
state.copyWith(
repeatedPassword: value.password,
repeatPasswordError: null,
successOrFail: null,
),
);
},
);
},
);
2021-07-13 13:14:49 +08:00
}
2022-01-04 22:44:03 +08:00
Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async {
emit(
state.copyWith(
isSubmitting: true,
successOrFail: null,
),
);
2021-09-06 16:18:34 +08:00
final password = state.password;
final repeatedPassword = state.repeatedPassword;
if (password == null) {
emit(
state.copyWith(
isSubmitting: false,
passwordError: LocaleKeys.signUp_emptyPasswordError.tr(),
),
);
2021-09-06 16:18:34 +08:00
return;
}
if (repeatedPassword == null) {
emit(
state.copyWith(
isSubmitting: false,
repeatPasswordError: LocaleKeys.signUp_repeatPasswordEmptyError.tr(),
),
);
2021-09-06 16:18:34 +08:00
return;
}
2021-07-13 13:14:49 +08:00
2021-09-06 16:18:34 +08:00
if (password != repeatedPassword) {
emit(
state.copyWith(
isSubmitting: false,
repeatPasswordError: LocaleKeys.signUp_unmatchedPasswordError.tr(),
),
);
2021-09-06 16:18:34 +08:00
return;
}
emit(
state.copyWith(
passwordError: null,
repeatPasswordError: null,
),
);
2021-09-06 16:18:34 +08:00
final result = await authService.signUp(
name: state.email ?? '',
password: state.password ?? '',
email: state.email ?? '',
2022-01-31 08:15:49 +08:00
);
emit(
result.fold(
(profile) => state.copyWith(
isSubmitting: false,
successOrFail: FlowyResult.success(profile),
emailError: null,
passwordError: null,
repeatPasswordError: null,
),
(error) => stateFromCode(error),
2021-09-06 16:18:34 +08:00
),
);
2021-07-13 13:14:49 +08:00
}
2021-12-14 18:04:51 +08:00
SignUpState stateFromCode(FlowyError error) {
switch (error.code) {
case ErrorCode.EmailFormatInvalid:
return state.copyWith(
2021-10-09 16:43:56 +08:00
isSubmitting: false,
emailError: error.msg,
passwordError: null,
successOrFail: null,
2021-10-09 16:43:56 +08:00
);
case ErrorCode.PasswordFormatInvalid:
return state.copyWith(
2021-10-09 16:43:56 +08:00
isSubmitting: false,
passwordError: error.msg,
emailError: null,
successOrFail: null,
2021-10-09 16:43:56 +08:00
);
default:
return state.copyWith(
isSubmitting: false,
successOrFail: FlowyResult.failure(error),
);
}
}
2021-07-13 13:14:49 +08:00
}
@freezed
class SignUpEvent with _$SignUpEvent {
const factory SignUpEvent.signUpWithUserEmailAndPassword() =
SignUpWithUserEmailAndPassword;
2022-03-25 15:02:43 +08:00
const factory SignUpEvent.emailChanged(String email) = _EmailChanged;
const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged;
const factory SignUpEvent.repeatPasswordChanged(String password) =
_RepeatPasswordChanged;
}
@freezed
class SignUpState with _$SignUpState {
2021-09-05 22:52:20 +08:00
const factory SignUpState({
String? email,
String? password,
2021-09-06 16:18:34 +08:00
String? repeatedPassword,
required bool isSubmitting,
required String? passwordError,
required String? repeatPasswordError,
required String? emailError,
required FlowyResult<UserProfilePB, FlowyError>? successOrFail,
2021-09-05 22:52:20 +08:00
}) = _SignUpState;
factory SignUpState.initial() => const SignUpState(
isSubmitting: false,
passwordError: null,
repeatPasswordError: null,
emailError: null,
successOrFail: null,
);
}