aretherecookies-mobile/js/apis/GooglePlacesApi.js

81 lines
2.4 KiB
JavaScript
Raw Normal View History

// @flow
import { type GooglePlaceObj } from '../records/PlaceRecord';
import { GoogleAPIKey } from '../constants/AppConstants';
import { pipe, filter, path, contains } 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/findplacefromtext/json?key=${GoogleAPIKey}`;
type GooglePlaceDetailsResponse = { error_message: ?string, result: GooglePlaceObj };
type GoogleFindPlaceResponse = { status: string, candidates: Array<GooglePlaceObj> };
const milesToMeters = miles => Math.ceil(miles > 0 ? miles / 0.00062137 : 0);
const isEstablishment = pipe(
path(['types']),
contains('establishment')
);
export const getPlaceDetails = async (placeid: ?string): Promise<GooglePlaceObj> => {
if (!placeid || typeof placeid !== 'string') {
throw new Error('placeid looks wrong');
}
const res = await fetch(`${placesDetailUrl}&placeid=${placeid}`);
const { error_message, result }: GooglePlaceDetailsResponse = await res.json();
if (error_message) {
throw new Error(error_message);
}
return result;
};
export const getURLForPhotoReference = (opts?: { maxheight?: number, maxwidth?: number } = {}) => (
photoReference: string
) => {
if (!photoReference) {
return '';
}
const { maxheight, maxwidth } = opts;
const maxHeight = `&maxheight=${maxheight || 600}`;
const maxWidth = maxwidth ? `&maxwidth=${maxwidth}` : '';
const photoref = `&photoreference=${photoReference}`;
return `${photosUrl}${photoref}${maxHeight}${maxWidth}`;
};
export const findNearbyPlaces = async ({
location,
search,
radius,
}: {
location?: Position,
search?: string,
radius: number,
}): Promise<any> => {
if (!location) {
return null;
}
const {
coords: { latitude, longitude },
} = location;
const input = `input=${encodeURIComponent(search || '*')}&inputtype=textquery`;
const loc = `locationbias=circle:${milesToMeters(radius)}@${latitude},${longitude}`;
2018-07-08 12:30:00 -05:00
const fields = 'fields=id,name,geometry,photos,types,formatted_address,opening_hours,place_id';
const reqUrl = `${placesFromTextUrl}&${input}&${loc}&${fields}`;
const { candidates, status }: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json();
if (status !== 'OK') {
throw new Error('google find places request failed');
}
return filter(isEstablishment, candidates);
};