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

113 lines
4.1 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/models/ProductDao.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> addCustomProductToKassaItems(String name, int count, double price) {
return (Store<AppState> store) async {
List<ProductDao> items = store.state.mainState.kassaItems;
items.add(new ProductDao(name: name, count: count, price: price));
store.dispatch(SetMainStateAction(MainState(kassaItems: items)));
};
}
ThunkAction<AppState> addProductToKassaItems(Good good) {
return (Store<AppState> store) async {
List<ProductDao> items = store.state.mainState.kassaItems;
int index = items.indexWhere((element) => element.good?.id == good.id);
if (index > -1) {
store.dispatch(counterProductFromKassaItems(index, 1));
} else {
items.add(new ProductDao(
name: good.name, good: good, count: 1, price: good.price));
store.dispatch(SetMainStateAction(MainState(kassaItems: items)));
}
};
}
ThunkAction<AppState> removeProductFromKassaItems(int index) {
return (Store<AppState> store) async {
List<ProductDao> items = List.from(store.state.mainState.kassaItems);
items.removeAt(index);
store.dispatch(SetMainStateAction(MainState(kassaItems: items)));
};
}
ThunkAction<AppState> counterProductFromKassaItems(int index, int counter) {
return (Store<AppState> store) async {
List<ProductDao> items = store.state.mainState.kassaItems;
if (items.elementAt(index).count == 1 && counter < 0) {
//if count to zero need delete element
store.dispatch(removeProductFromKassaItems(index));
} else {
items.elementAt(index).count += counter;
store.dispatch(SetMainStateAction(MainState(kassaItems: items)));
}
};
}
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)));
}
};
}