41 lines
1.2 KiB
Dart
41 lines
1.2 KiB
Dart
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));
|
|
},
|
|
);
|
|
}
|
|
}
|