aretherecookies-mobile/js/streams/FoodItemsStream.js
Bart Akeley 9221b7f9b5 quantities round trip api and stream
make POST requests to /quantity to update the quantity of an existing food item - then mixin the new quantity with the existing food items observable
2017-11-26 19:52:16 -06:00

20 lines
978 B
JavaScript

//@flow
import FoodItemRecord, { createFoodItem } from '../records/FoodItemRecord';
import { setById } from '../helpers/ImmutableHelpers';
import { Map } from 'immutable';
import location$ from './LocationStream';
import { getFoodItems, type FoodItemsForLocation } from '../apis/FoodItemsApi';
import Filter$ from './FilterStream';
import FilterRecord from '../records/FilterRecord';
import Quantity$ from './QuantityStream';
import type { QuantityFragment } from '../constants/QuantityConstants';
export default location$
.combineLatest(Filter$)
.mergeMap(([loc, filter]: [Position, FilterRecord]) => getFoodItems({ loc, filter }))
.map(({ fooditems = [] }: FoodItemsForLocation) => {
return fooditems.map(createFoodItem).reduce(setById, new Map());
})
.combineLatest(Quantity$, (foodItems: Map<string, FoodItemRecord>, quantities: Map<string, QuantityFragment>) => {
return foodItems.mergeDeepWith((foodItem, quantity) => foodItem.merge(quantity), quantities);
});