2022-01-31 08:15:49 +08:00
|
|
|
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart';
|
2021-07-13 13:14:49 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
2021-12-07 23:01:23 +05:30
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-12-19 21:29:33 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode;
|
2021-12-14 18:04:51 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
2021-07-13 13:14:49 +08:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2021-12-07 23:01:23 +05:30
|
|
|
import 'package:app_flowy/generated/locale_keys.g.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> {
|
2022-01-31 08:15:49 +08:00
|
|
|
final AuthRepository autoRepo;
|
|
|
|
SignUpBloc(this.autoRepo) : super(SignUpState.initial()) {
|
2022-01-04 22:44:03 +08:00
|
|
|
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: none(), successOrFail: none()));
|
|
|
|
}, passwordChanged: (PasswordChanged value) async {
|
|
|
|
emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none()));
|
|
|
|
}, repeatPasswordChanged: (RepeatPasswordChanged value) async {
|
|
|
|
emit(state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none()));
|
|
|
|
});
|
2021-09-06 16:18:34 +08:00
|
|
|
});
|
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(
|
2021-09-06 16:18:34 +08:00
|
|
|
isSubmitting: true,
|
2021-10-09 16:43:56 +08:00
|
|
|
successOrFail: none(),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
2021-09-06 16:18:34 +08:00
|
|
|
|
|
|
|
final password = state.password;
|
|
|
|
final repeatedPassword = state.repeatedPassword;
|
|
|
|
if (password == null) {
|
2022-01-04 22:44:03 +08:00
|
|
|
emit(state.copyWith(
|
2021-09-06 16:18:34 +08:00
|
|
|
isSubmitting: false,
|
2021-12-07 23:01:23 +05:30
|
|
|
passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
2021-09-06 16:18:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatedPassword == null) {
|
2022-01-04 22:44:03 +08:00
|
|
|
emit(state.copyWith(
|
2021-09-06 16:18:34 +08:00
|
|
|
isSubmitting: false,
|
2021-12-07 23:01:23 +05:30
|
|
|
repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
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) {
|
2022-01-04 22:44:03 +08:00
|
|
|
emit(state.copyWith(
|
2021-09-06 16:18:34 +08:00
|
|
|
isSubmitting: false,
|
2021-12-07 23:01:23 +05:30
|
|
|
repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
2021-09-06 16:18:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:44:03 +08:00
|
|
|
emit(state.copyWith(
|
2021-09-06 16:18:34 +08:00
|
|
|
passwordError: none(),
|
|
|
|
repeatPasswordError: none(),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
2021-09-06 16:18:34 +08:00
|
|
|
|
2022-01-31 08:15:49 +08:00
|
|
|
final result = await autoRepo.signUp(
|
|
|
|
name: state.email,
|
|
|
|
password: state.password,
|
|
|
|
email: state.email,
|
|
|
|
);
|
2022-01-04 22:44:03 +08:00
|
|
|
emit(result.fold(
|
2021-11-08 23:15:29 +08:00
|
|
|
(profile) => state.copyWith(
|
2021-09-06 16:18:34 +08:00
|
|
|
isSubmitting: false,
|
2021-11-08 23:15:29 +08:00
|
|
|
successOrFail: some(left(profile)),
|
2021-09-06 16:18:34 +08:00
|
|
|
emailError: none(),
|
|
|
|
passwordError: none(),
|
|
|
|
repeatPasswordError: none(),
|
|
|
|
),
|
2021-07-25 18:04:16 +08:00
|
|
|
(error) => stateFromCode(error),
|
2022-01-04 22:44:03 +08:00
|
|
|
));
|
2021-07-13 13:14:49 +08:00
|
|
|
}
|
2021-07-25 18:04:16 +08:00
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
SignUpState stateFromCode(FlowyError error) {
|
2021-11-08 10:25:10 +08:00
|
|
|
switch (ErrorCode.valueOf(error.code)!) {
|
2021-08-31 11:32:51 +08:00
|
|
|
case ErrorCode.EmailFormatInvalid:
|
2021-07-25 18:04:16 +08:00
|
|
|
return state.copyWith(
|
2021-10-09 16:43:56 +08:00
|
|
|
isSubmitting: false,
|
|
|
|
emailError: some(error.msg),
|
|
|
|
passwordError: none(),
|
|
|
|
successOrFail: none(),
|
|
|
|
);
|
2021-08-31 11:32:51 +08:00
|
|
|
case ErrorCode.PasswordFormatInvalid:
|
2021-07-25 18:04:16 +08:00
|
|
|
return state.copyWith(
|
2021-10-09 16:43:56 +08:00
|
|
|
isSubmitting: false,
|
|
|
|
passwordError: some(error.msg),
|
|
|
|
emailError: none(),
|
|
|
|
successOrFail: none(),
|
|
|
|
);
|
2021-07-25 18:04:16 +08:00
|
|
|
default:
|
2021-11-08 10:25:10 +08:00
|
|
|
return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
|
2021-07-25 18:04:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 13:14:49 +08:00
|
|
|
}
|
2021-07-24 15:05:47 +08:00
|
|
|
|
|
|
|
@freezed
|
2021-10-09 10:09:31 +08:00
|
|
|
class SignUpEvent with _$SignUpEvent {
|
2021-11-08 10:25:10 +08:00
|
|
|
const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
|
2021-09-05 22:52:20 +08:00
|
|
|
const factory SignUpEvent.emailChanged(String email) = EmailChanged;
|
|
|
|
const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
|
2021-11-08 10:25:10 +08:00
|
|
|
const factory SignUpEvent.repeatPasswordChanged(String password) = RepeatPasswordChanged;
|
2021-07-24 15:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2021-10-09 10:09:31 +08:00
|
|
|
class SignUpState with _$SignUpState {
|
2021-09-05 22:52:20 +08:00
|
|
|
const factory SignUpState({
|
2021-07-24 15:05:47 +08:00
|
|
|
String? email,
|
|
|
|
String? password,
|
2021-09-06 16:18:34 +08:00
|
|
|
String? repeatedPassword,
|
2021-07-24 15:05:47 +08:00
|
|
|
required bool isSubmitting,
|
2021-07-25 18:04:16 +08:00
|
|
|
required Option<String> passwordError,
|
2021-09-06 16:18:34 +08:00
|
|
|
required Option<String> repeatPasswordError,
|
2021-07-25 18:04:16 +08:00
|
|
|
required Option<String> emailError,
|
2021-12-14 18:04:51 +08:00
|
|
|
required Option<Either<UserProfile, FlowyError>> successOrFail,
|
2021-09-05 22:52:20 +08:00
|
|
|
}) = _SignUpState;
|
2021-07-24 15:05:47 +08:00
|
|
|
|
2021-09-05 22:52:20 +08:00
|
|
|
factory SignUpState.initial() => SignUpState(
|
2021-07-24 15:05:47 +08:00
|
|
|
isSubmitting: false,
|
2021-07-25 18:04:16 +08:00
|
|
|
passwordError: none(),
|
2021-09-06 16:18:34 +08:00
|
|
|
repeatPasswordError: none(),
|
2021-07-25 18:04:16 +08:00
|
|
|
emailError: none(),
|
|
|
|
successOrFail: none(),
|
2021-07-24 15:05:47 +08:00
|
|
|
);
|
|
|
|
}
|