mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 04:34:55 -06:00
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
67 lines
1.3 KiB
JavaScript
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,
|
|
};
|
|
}
|
|
});
|