fix
This commit is contained in:
@@ -18,9 +18,10 @@ class TopUpBloc extends Bloc<TopUpEvent, TopUpState> {
|
||||
required this.getCertificatesUsecase,
|
||||
required this.purchaseCertificateUsecase,
|
||||
required this.getUserCards,
|
||||
}) : super(TopUpState(isLoading: true)) {
|
||||
}) : super(const TopUpLoading()) {
|
||||
|
||||
on<LoadTopUpData>((event, emit) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
emit(const TopUpLoading());
|
||||
|
||||
final results = await Future.wait([
|
||||
getCertificatesUsecase(),
|
||||
@@ -33,64 +34,68 @@ class TopUpBloc extends Bloc<TopUpEvent, TopUpState> {
|
||||
switch ((certResult, cardResult)) {
|
||||
case (Success(data: final certs), Success(data: final cards)):
|
||||
emit(
|
||||
state.copyWith(
|
||||
certificates: certs,
|
||||
cards: cards,
|
||||
selectedTariff: certs!.length > 1
|
||||
TopUpLoaded(
|
||||
certificates: certs ?? [],
|
||||
cards: cards ?? [],
|
||||
selectedTariff: certs != null && certs.length > 1
|
||||
? certs[1]
|
||||
: (certs.isNotEmpty ? certs.first : null),
|
||||
|
||||
selectedCard: cards!.isEmpty
|
||||
: (certs != null && certs.isNotEmpty ? certs.first : null),
|
||||
selectedCard: cards == null || cards.isEmpty
|
||||
? null
|
||||
: cards.firstWhere(
|
||||
(c) => c.isMain,
|
||||
orElse: () => cards.first,
|
||||
),
|
||||
isLoading: false,
|
||||
(c) => c.isMain,
|
||||
orElse: () => cards.first,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
case (
|
||||
Success<List<Certificate>>(data: null),
|
||||
Failure<List<PaymentCard>>(),
|
||||
):
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
case (
|
||||
Success<List<Certificate>>(data: [...]),
|
||||
Failure<List<PaymentCard>>(),
|
||||
):
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
case (Failure<List<Certificate>>(), _):
|
||||
// TODO: Handle this case.
|
||||
throw UnimplementedError();
|
||||
default:
|
||||
emit(const TopUpPaymentFailure('Не удалось загрузить данные'));
|
||||
}
|
||||
});
|
||||
on<SelectCertificate>(
|
||||
(event, emit) => emit(state.copyWith(selectedTariff: event.certificate)),
|
||||
);
|
||||
on<SelectCard>(
|
||||
(event, emit) => emit(state.copyWith(selectedCard: event.card)),
|
||||
);
|
||||
on<ToggleAgreement>(
|
||||
(event, emit) => emit(state.copyWith(isAgreed: event.value)),
|
||||
);
|
||||
|
||||
on<SelectCertificate>((event, emit) {
|
||||
if (state is TopUpLoaded) {
|
||||
final currentState = state as TopUpLoaded;
|
||||
emit(currentState.copyWith(selectedTariff: event.certificate));
|
||||
}
|
||||
});
|
||||
|
||||
on<SelectCard>((event, emit) {
|
||||
if (state is TopUpLoaded) {
|
||||
final currentState = state as TopUpLoaded;
|
||||
emit(currentState.copyWith(selectedCard: event.card));
|
||||
}
|
||||
});
|
||||
|
||||
on<ToggleAgreement>((event, emit) {
|
||||
if (state is TopUpLoaded) {
|
||||
final currentState = state as TopUpLoaded;
|
||||
emit(currentState.copyWith(isAgreed: event.value));
|
||||
}
|
||||
});
|
||||
|
||||
on<TopUpButtonPressed>((event, emit) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
if (state is TopUpLoaded) {
|
||||
final currentState = state as TopUpLoaded;
|
||||
|
||||
if (state.isAgreed) {
|
||||
final result = await purchaseCertificateUsecase(
|
||||
certificateId: state.selectedTariff!.id,
|
||||
cardId:state.selectedCard!.id,
|
||||
);
|
||||
if (currentState.isAgreed &&
|
||||
currentState.selectedTariff != null &&
|
||||
currentState.selectedCard != null) {
|
||||
|
||||
if (result is Success) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
emit(currentState.copyWith(isProcessingPayment: true));
|
||||
|
||||
final result = await purchaseCertificateUsecase(
|
||||
certificateId: currentState.selectedTariff!.id,
|
||||
cardId: currentState.selectedCard!.id,
|
||||
);
|
||||
|
||||
if (result is Success) {
|
||||
emit(const TopUpPaymentSuccess());
|
||||
} else {
|
||||
emit(const TopUpPaymentFailure('Ошибка при оплате'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user