aretherecookies-mobile/js/apis/GooglePlacesApi.js

118 lines
3.2 KiB
JavaScript
Raw Normal View History

// @flow
2019-09-21 15:45:07 +00:00
import { type GooglePlaceObj } from "../records/PlaceRecord";
import { GoogleAPIKey } from "../constants/AppConstants";
import { memoizeWith, identity } from "ramda";
const placesDetailUrl = `https://maps.googleapis.com/maps/api/place/details/json?key=${GoogleAPIKey}`;
const photosUrl = `https://maps.googleapis.com/maps/api/place/photo?key=${GoogleAPIKey}`;
const placesFromTextUrl = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=${GoogleAPIKey}`;
type GooglePlaceDetailsResponse = { error_message: ?string, result: GooglePlaceObj };
type GoogleFindPlaceResponse = {
status: string,
results: Array<GooglePlaceObj>,
2019-09-21 15:45:07 +00:00
next_page_token?: string
};
const milesToMeters = miles => Math.ceil(miles > 0 ? miles / 0.00062137 : 0);
2018-08-12 09:51:44 -05:00
/**
* always return a promise and swallow exceptions so as not to break the stream
*/
const safeWrapAjax = ajaxFn =>
2019-09-21 15:45:07 +00:00
memoizeWith(identity, (...args) => {
2018-08-12 09:51:44 -05:00
return ajaxFn(...args).catch(error => {
2019-09-21 15:45:07 +00:00
console.log("ERROR: ", error); //eslint-disable-line no-console
2018-08-12 09:51:44 -05:00
return null;
});
});
export const getPlaceDetails = safeWrapAjax(
2019-09-21 15:45:07 +00:00
async ({ distance, placeId }: { distance: number, placeId: ?string }): Promise<GooglePlaceObj> => {
if (!placeId || typeof placeId !== "string") {
throw new Error("placeid looks wrong");
2018-08-12 09:51:44 -05:00
}
2018-08-12 09:51:44 -05:00
const res = await fetch(`${placesDetailUrl}&placeid=${placeId}`);
2018-08-12 09:51:44 -05:00
const { error_message, result }: GooglePlaceDetailsResponse = await res.json();
2018-08-12 09:51:44 -05:00
if (error_message) {
throw new Error(error_message);
}
2018-07-08 19:38:54 -05:00
2018-08-12 09:51:44 -05:00
return { ...result, distance };
}
);
export const getURLForPhotoReference = (opts?: { maxheight?: number, maxwidth?: number } = {}) => (
photoReference: string
) => {
if (!photoReference) {
2019-09-21 15:45:07 +00:00
return "";
}
const { maxheight, maxwidth } = opts;
const maxHeight = `&maxheight=${maxheight || 600}`;
2019-09-21 15:45:07 +00:00
const maxWidth = maxwidth ? `&maxwidth=${maxwidth}` : "";
const photoref = `&photoreference=${photoReference}`;
return `${photosUrl}${photoref}${maxHeight}${maxWidth}`;
};
// const getPageResults = async ({ pageToken }: { pageToken: string }) => {
// const reqUrl = `${placesFromTextUrl}&pageToken=${pageToken}`;
// return (await fetch(reqUrl)).toJson();
// };
2018-08-12 09:51:44 -05:00
export const findNearbyPlaces = safeWrapAjax(
async ({
location,
search,
2019-09-21 15:45:07 +00:00
radius
2018-08-12 09:51:44 -05:00
}: {
location?: Position,
search?: string,
2019-09-21 15:45:07 +00:00
radius: number
2018-08-12 09:51:44 -05:00
}): Promise<any> => {
if (!location) {
return null;
2018-07-15 11:28:38 -05:00
}
2018-08-12 09:51:44 -05:00
const {
2019-09-21 15:45:07 +00:00
coords: { latitude, longitude }
2018-08-12 09:51:44 -05:00
} = location;
2019-09-21 15:45:07 +00:00
const keyword = search ? `keyword=${encodeURIComponent(search)}` : "";
2018-08-12 09:51:44 -05:00
const loc = `location=${latitude},${longitude}`;
const rad = `radius=${milesToMeters(radius)}`;
const reqUrl = `${placesFromTextUrl}&${keyword}&${loc}&${rad}&type=restaurant`;
let error;
let places;
try {
const response: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json();
// if (response.next_page_token) {
// const page = await getPageResults({ pageToken: response.next_page_token });
// return concat(response.results, page.results);
// }
2019-09-21 15:45:07 +00:00
if (response.status !== "OK") {
throw new Error("google find places request failed");
2018-08-12 09:51:44 -05:00
}
places = response.results;
} catch (err) {
2019-09-21 15:45:07 +00:00
console.error(error); // eslint-disable-line no-console
2018-08-12 09:51:44 -05:00
error = err;
}
return {
error,
location,
2019-09-21 15:45:07 +00:00
places
2018-08-12 09:51:44 -05:00
};
}
2018-08-12 09:51:44 -05:00
);