2017-08-27 14:29:37 -05:00
|
|
|
// @flow
|
2017-10-22 12:38:05 -05:00
|
|
|
import { memoize } from 'ramda';
|
2017-11-11 20:15:19 -06:00
|
|
|
import FilterRecord from '../records/FilterRecord';
|
2017-08-27 14:29:37 -05:00
|
|
|
|
2017-10-29 20:18:33 -05:00
|
|
|
const BASE_URL = 'aretherecookies.herokuapp.com';
|
2017-11-11 20:15:19 -06:00
|
|
|
// const BASE_URL = '192.168.1.6:3000';
|
2017-10-29 20:18:33 -05:00
|
|
|
|
2017-10-22 12:38:05 -05:00
|
|
|
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,
|
|
|
|
|
};
|
2017-08-27 14:29:37 -05:00
|
|
|
|
2017-10-22 12:38:05 -05:00
|
|
|
export type FoodItemsForLocation = {
|
|
|
|
|
orderby: string,
|
|
|
|
|
filter: FoodItemsFilter,
|
|
|
|
|
fooditems: Array<RawFoodItem>,
|
2017-08-27 14:29:37 -05:00
|
|
|
};
|
2017-10-22 12:38:05 -05:00
|
|
|
|
2017-11-11 20:15:19 -06:00
|
|
|
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,
|
2017-10-22 12:38:05 -05:00
|
|
|
},
|
2017-11-11 20:15:19 -06:00
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(json => ({
|
|
|
|
|
...json,
|
2017-10-22 12:38:05 -05:00
|
|
|
loading: false,
|
2017-11-11 20:15:19 -06:00
|
|
|
error: null,
|
|
|
|
|
}));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
|
return {
|
|
|
|
|
orderby: 'distance',
|
|
|
|
|
filter: {},
|
|
|
|
|
fooditems: [],
|
|
|
|
|
loading: false,
|
|
|
|
|
error: error,
|
|
|
|
|
};
|
2017-10-22 12:38:05 -05:00
|
|
|
}
|
2017-11-11 20:15:19 -06:00
|
|
|
});
|