You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
988 B
25 lines
988 B
import 'package:redux/redux.dart';
|
|
import 'package:thesis_shop/redux/actions/actions.dart';
|
|
import 'package:thesis_shop/redux/state.dart';
|
|
|
|
final productQuantitiesReducer = combineReducers<Quantities>([
|
|
TypedReducer<Quantities, IncrementProductAction>(_increment),
|
|
TypedReducer<Quantities, DecrementProductAction>(_decrement),
|
|
]);
|
|
|
|
Quantities _changeAmount(Quantities state, ChangeProductAmountAction action) {
|
|
final newState = Map.of(state);
|
|
final currentAmount = newState.putIfAbsent(action.product.title, () => 0);
|
|
if (currentAmount + action.amount < 0) return state;
|
|
if (currentAmount + action.amount == 0) {
|
|
newState.remove(action.product.title);
|
|
return newState;
|
|
}
|
|
return newState..[action.product.title] = currentAmount + action.amount;
|
|
}
|
|
|
|
Quantities _increment(Quantities state, IncrementProductAction action) =>
|
|
_changeAmount(state, action);
|
|
|
|
Quantities _decrement(Quantities state, DecrementProductAction action) =>
|
|
_changeAmount(state, action);
|
|
|