Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:bloc/bloc.dart';
- import 'package:equatable/equatable.dart';
- import 'package:module_category/domain/entities/category_product_list.dart';
- import 'package:module_category/domain/usecases/get_category_product_list.dart';
- import 'package:module_category/domain/usecases/get_category_product_list_sorting.dart';
- import 'package:module_core/common/state_enum.dart';
- part 'category_product_list_event.dart';
- part 'category_product_list_state.dart';
- class CategoryProductListBloc
- extends Bloc<CategoryProductListEvent, CategoryProductListState> {
- final GetCategoryProductList _getCategoryProductList;
- final GetCategoryProductListSorting _getCategoryProductListSorting;
- CategoryProductListBloc(
- this._getCategoryProductList, this._getCategoryProductListSorting)
- : super(CategoryProductListState.initial()) {
- on<FetchCategoryProductList>(
- (event, emit) async {
- final slug = event.slug;
- emit(state.copyWith(state: RequestState.Loading));
- final result = await _getCategoryProductList.execute(slug);
- result.fold(
- (failure) => emit(state.copyWith(
- state: RequestState.Error, errorMessage: failure.message)),
- (categoryProductList) {
- emit(state.copyWith(
- state: RequestState.Loaded, data: categoryProductList));
- if (categoryProductList.response.data.isEmpty) {
- emit(state.copyWith(
- state: RequestState.Empty,
- errorMessage: 'Empty Category Product'));
- }
- },
- );
- },
- );
- on<FetchCategoryProductListSorting>(
- (event, emit) async {
- final slug = event.slug;
- final sort = event.sort;
- emit(state.copyWith(state: RequestState.Loading));
- final result = await _getCategoryProductListSorting.execute(slug, sort);
- result.fold(
- (failure) => emit(state.copyWith(
- state: RequestState.Error, errorMessage: failure.message)),
- (categoryProductList) {
- emit(state.copyWith(
- state: RequestState.Loaded, data: categoryProductList));
- if (categoryProductList.response.data.isEmpty) {
- emit(state.copyWith(
- state: RequestState.Empty,
- errorMessage: 'Empty Category Product'));
- }
- },
- );
- },
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement