aman-kassa-flutter/lib/redux/actions/main_actions.dart

72 lines
2.5 KiB
Dart

import 'dart:convert';
import 'package:aman_kassa_flutter/core/entity/Category.dart';
import 'package:aman_kassa_flutter/core/entity/Goods.dart';
import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/DictDao.dart';
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
import 'package:aman_kassa_flutter/core/services/DataService.dart';
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
import 'package:aman_kassa_flutter/redux/state/main_state.dart';
import 'package:meta/meta.dart';
import 'package:redux/redux.dart';
import 'package:redux_thunk/redux_thunk.dart';
import '../store.dart';
@immutable
class SetMainStateAction {
final MainState mainState;
SetMainStateAction(this.mainState);
}
final ApiService _api = locator<ApiService>();
final DataService _dataService = locator<DataService>();
final NavigatorService _navigation = locator<NavigatorService>();
Future<void> backBottomElement(Store<AppState> store) async {
List<DictDao> prevCategories = store.state.mainState.prevCategories;
DictDao last = prevCategories.removeLast();
if(last!=null) {
store.dispatch(SetMainStateAction(MainState(prevCategories: prevCategories)));
store.dispatch(selectBottomElement(last.id));
}
}
ThunkAction<AppState> selectBottomElement(int parentId) {
return (Store<AppState> store) async {
store.dispatch(SetMainStateAction(MainState(bottomSheetLoading: true)));
try {
List<DictDao> prevCategories = store.state.mainState.prevCategories;
if(parentId == 0) {
prevCategories = [];
}
store.state.mainState.bottomSheetElements.forEach((element) {
if(element is Category && element.id == parentId){
prevCategories.add(DictDao(id: element.parentIn, name: element.name));
}
});
List<Category> categories = await _dataService.getCategoriesByParentId(parentId: parentId);
List<Good> goods = await _dataService.getGoodsByCategoryId(categoryId: parentId);
List _bottomSheetElements = [];
_bottomSheetElements.addAll(categories);
_bottomSheetElements.addAll(goods);
store.dispatch(
SetMainStateAction(
MainState(
bottomSheetLoading: false,
bottomSheetElements: _bottomSheetElements,
prevCategories: prevCategories
)
)
);
} catch (e) {
print(e);
store.dispatch(SetMainStateAction(MainState(bottomSheetLoading: false)));
}
};
}