Files
enaklo-flutter/lib/application/voucher/voucher_loader/voucher_loader_bloc.dart
T

41 lines
1.2 KiB
Dart
Raw Normal View History

2026-10-10 02:33:16 +07:00
import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import '../../../domain/wallet/wallet.dart';
part 'voucher_loader_event.dart';
part 'voucher_loader_state.dart';
part 'voucher_loader_bloc.freezed.dart';
/// Katalog voucher yang bisa ditukar (ยง9.1).
@injectable
class VoucherLoaderBloc extends Bloc<VoucherLoaderEvent, VoucherLoaderState> {
final IVoucherRepository _repository;
VoucherLoaderBloc(this._repository) : super(VoucherLoaderState.initial()) {
on<VoucherLoaderEvent>(_onVoucherLoaderEvent);
}
Future<void> _onVoucherLoaderEvent(
VoucherLoaderEvent event,
Emitter<VoucherLoaderState> emit,
) {
return event.map(
fetched: (e) async {
emit(state.copyWith(isFetching: true, failureOption: none()));
final result = await _repository.getVouchers();
final data = result.fold(
(f) => state.copyWith(failureOption: optionOf(f)),
(vouchers) => state.copyWith(vouchers: vouchers, hasLoaded: true),
);
emit(data.copyWith(isFetching: false));
},
);
}
}