Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.2 kB
2
Indexable
Never
// ignore_for_file: unnecessary_nullable_for_final_variable_declarations

import '../../../utils/base_import.dart';
import '../billing/repositories/models/confirm_payment_model.dart';
import '../repositories/services/p2d_services.dart';

part 'p2d_state.dart';
part 'p2d_event.dart';

class P2DBloc extends Bloc<P2DEvent, P2DState> {
  P2DBloc() : super(AxeInitial()) {
    final P2DService service = P2DService();
    on<ConfirmPayment>((ConfirmPayment event, Emitter<P2DState> emit) async {
      try {
        emit(ConfirmPaymentLoading());
        final ConfirmPaymentModel? items =
            await service.confirmPayment(event.body);
        if (items != null) {
          emit(ConfirmPaymentSuccess(message: items));
        } else {
          emit(ConfirmPaymentError(
              message: "Response dari server kosong atau tidak valid"));
        }
      } catch (e) {
        print("Error");
        print('Exception: $e');
        emit(ConfirmPaymentError(
            message: "Terjadi kesalahan, silahkan coba kembali"));
      }
    });

    on<ConfirmPaymentNego>(
        (ConfirmPaymentNego event, Emitter<P2DState> emit) async {
      try {
        emit(ConfirmPaymentLoading());
        print("Calling service.confirmPaymentNego with data: ${event.body}");
        final ConfirmPaymentModel? result =
            await service.confirmPaymentNego(event.body);
        print("Service call completed. Result: ${result?.toString()}");

        if (result != null) {
          print("ConfirmPaymentSuccess result: ${result.toString()}");
          emit(ConfirmPaymentSuccess(message: result));
          print("State emitted: ConfirmPaymentSuccess");
        } else {
          print("Response dari server kosong atau tidak valid");
          emit(ConfirmPaymentError(
              message: "Response dari server kosong atau tidak valid"));
          print("State emitted: ConfirmPaymentError");
        }
      } catch (e) {
        print("Error");
        print('Exception: $e');
        emit(ConfirmPaymentError(
            message: "Terjadi kesalahan, silahkan coba kembali"));
        print("State emitted: ConfirmPaymentError");
      }
    });
  }
}
Leave a Comment