AppFlowy/app_flowy/lib/user/application/sign_up_bloc.dart

130 lines
3.8 KiB
Dart
Raw Normal View History

2021-07-21 15:43:05 +08:00
import 'package:app_flowy/user/domain/i_auth.dart';
2021-07-13 13:14:49 +08:00
import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart';
2021-07-13 13:14:49 +08:00
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
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> {
2021-07-13 13:14:49 +08:00
final IAuth authImpl;
2021-09-05 22:52:20 +08:00
SignUpBloc(this.authImpl) : super(SignUpState.initial());
2021-07-13 13:14:49 +08:00
@override
2021-09-05 22:52:20 +08:00
Stream<SignUpState> mapEventToState(
SignUpEvent event,
2021-07-13 13:14:49 +08:00
) async* {
2021-09-06 16:18:34 +08:00
yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
yield* _performActionOnSignUp();
}, emailChanged: (EmailChanged value) async* {
yield state.copyWith(email: value.email, successOrFail: none());
}, passwordChanged: (PasswordChanged value) async* {
yield state.copyWith(password: value.password, successOrFail: none());
}, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
yield state.copyWith(
repeatedPassword: value.password, successOrFail: none());
});
2021-07-13 13:14:49 +08:00
}
2021-09-06 16:18:34 +08:00
Stream<SignUpState> _performActionOnSignUp() async* {
yield state.copyWith(
isSubmitting: true,
);
final password = state.password;
final repeatedPassword = state.repeatedPassword;
if (password == null) {
yield state.copyWith(
isSubmitting: false,
passwordError: some("Password can't be empty"),
);
return;
}
if (repeatedPassword == null) {
yield state.copyWith(
isSubmitting: false,
repeatPasswordError: some("Repeat password can't be empty"),
);
return;
}
2021-07-13 13:14:49 +08:00
2021-09-06 16:18:34 +08:00
if (password != repeatedPassword) {
yield state.copyWith(
isSubmitting: false,
repeatPasswordError:
some("Repeat password is not the same as password"),
);
return;
}
yield state.copyWith(
passwordError: none(),
repeatPasswordError: none(),
);
final result =
await authImpl.signUp(state.email, state.password, state.email);
2021-07-13 13:14:49 +08:00
yield result.fold(
2021-09-05 22:52:20 +08:00
(userProfile) => state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: false,
successOrFail: some(left(userProfile)),
emailError: none(),
passwordError: none(),
repeatPasswordError: none(),
),
(error) => stateFromCode(error),
2021-07-13 13:14:49 +08:00
);
}
2021-09-05 22:52:20 +08:00
SignUpState stateFromCode(UserError error) {
switch (error.code) {
case ErrorCode.EmailFormatInvalid:
return state.copyWith(
isSubmitting: false,
emailError: some(error.msg),
passwordError: none());
case ErrorCode.PasswordFormatInvalid:
return state.copyWith(
isSubmitting: false,
passwordError: some(error.msg),
emailError: none());
default:
return state.copyWith(
isSubmitting: false, successOrFail: some(right(error)));
}
}
2021-07-13 13:14:49 +08:00
}
@freezed
class SignUpEvent with _$SignUpEvent {
2021-09-05 22:52:20 +08:00
const factory SignUpEvent.signUpWithUserEmailAndPassword() =
SignUpWithUserEmailAndPassword;
const factory SignUpEvent.emailChanged(String email) = EmailChanged;
const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
2021-09-06 16:18:34 +08:00
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 Option<String> passwordError,
2021-09-06 16:18:34 +08:00
required Option<String> repeatPasswordError,
required Option<String> emailError,
2021-09-04 16:53:58 +08:00
required Option<Either<UserProfile, UserError>> successOrFail,
2021-09-05 22:52:20 +08:00
}) = _SignUpState;
2021-09-05 22:52:20 +08:00
factory SignUpState.initial() => SignUpState(
isSubmitting: false,
passwordError: none(),
2021-09-06 16:18:34 +08:00
repeatPasswordError: none(),
emailError: none(),
successOrFail: none(),
);
}