Advertisement
rifki_cs29

ProductListBloc

Mar 4th, 2022
1,784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.42 KB | None | 0 0
  1. import 'package:bloc/bloc.dart';
  2. import 'package:equatable/equatable.dart';
  3. import 'package:module_category/domain/entities/category_product_list.dart';
  4. import 'package:module_category/domain/usecases/get_category_product_list.dart';
  5. import 'package:module_category/domain/usecases/get_category_product_list_sorting.dart';
  6. import 'package:module_core/common/state_enum.dart';
  7.  
  8. part 'category_product_list_event.dart';
  9. part 'category_product_list_state.dart';
  10.  
  11. class CategoryProductListBloc
  12.     extends Bloc<CategoryProductListEvent, CategoryProductListState> {
  13.   final GetCategoryProductList _getCategoryProductList;
  14.   final GetCategoryProductListSorting _getCategoryProductListSorting;
  15.   CategoryProductListBloc(
  16.       this._getCategoryProductList, this._getCategoryProductListSorting)
  17.       : super(CategoryProductListState.initial()) {
  18.     on<FetchCategoryProductList>(
  19.       (event, emit) async {
  20.         final slug = event.slug;
  21.         emit(state.copyWith(state: RequestState.Loading));
  22.         final result = await _getCategoryProductList.execute(slug);
  23.  
  24.         result.fold(
  25.           (failure) => emit(state.copyWith(
  26.               state: RequestState.Error, errorMessage: failure.message)),
  27.           (categoryProductList) {
  28.             emit(state.copyWith(
  29.                 state: RequestState.Loaded, data: categoryProductList));
  30.             if (categoryProductList.response.data.isEmpty) {
  31.               emit(state.copyWith(
  32.                   state: RequestState.Empty,
  33.                   errorMessage: 'Empty Category Product'));
  34.             }
  35.           },
  36.         );
  37.       },
  38.     );
  39.     on<FetchCategoryProductListSorting>(
  40.       (event, emit) async {
  41.         final slug = event.slug;
  42.         final sort = event.sort;
  43.         emit(state.copyWith(state: RequestState.Loading));
  44.         final result = await _getCategoryProductListSorting.execute(slug, sort);
  45.  
  46.         result.fold(
  47.           (failure) => emit(state.copyWith(
  48.               state: RequestState.Error, errorMessage: failure.message)),
  49.           (categoryProductList) {
  50.             emit(state.copyWith(
  51.                 state: RequestState.Loaded, data: categoryProductList));
  52.             if (categoryProductList.response.data.isEmpty) {
  53.               emit(state.copyWith(
  54.                   state: RequestState.Empty,
  55.                   errorMessage: 'Empty Category Product'));
  56.             }
  57.           },
  58.         );
  59.       },
  60.     );
  61.   }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement