aretherecookies-mobile/js/apis/FoodItemsApi.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

67 lines
1.3 KiB
JavaScript

// @flow
import { memoize } from 'ramda';
import FilterRecord from '../records/FilterRecord';
import { BASE_URL } from '../constants/AppConstants';
export type FoodItemsFilter = {
radius?: number,
};
export type RawFoodItem = {
id: string,
name: string,
place_id: string,
category: string,
images: string,
thumbimage: string,
latitude: number,
longitude: number,
distance: number,
lastupdated: number,
};
export type FoodItemsForLocation = {
orderby: string,
filter: FoodItemsFilter,
fooditems: Array<RawFoodItem>,
};
export const getFoodItems = memoize(async ({ loc, filter }: { loc: Position, filter: FilterRecord }): Promise<
FoodItemsForLocation
> => {
const { coords: { latitude: lat, longitude: lng } } = loc;
const { orderby, categories, radius } = filter;
try {
return fetch(`http://${BASE_URL}/fooditems`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
lat,
lng,
orderby,
filter: {
...(categories ? { categories } : {}),
radius,
},
}),
})
.then(res => res.json())
.then(json => ({
...json,
loading: false,
error: null,
}));
} catch (error) {
console.error(error); // eslint-disable-line no-console
return {
orderby: 'distance',
filter: {},
fooditems: [],
loading: false,
error: error,
};
}
});