aretherecookies-mobile/js/records/PlaceRecord.js
2017-07-23 19:58:10 -05:00

121 lines
2.4 KiB
JavaScript

// @flow
import { List, Map, Record, fromJS } from 'immutable';
import { pipe, pathOr, map, head, memoizeWith } from 'ramda';
import { getURLForPhotoReference } from '../apis/PlaceDetailsApi';
export type GooglePlaceObj = {
place_id: string,
name: string,
formatted_address: string,
geometry: {
location: {
lat: number,
lng: number,
},
viewport: {
northeast: {
lat: number,
lng: number,
},
southwest: {
lat: number,
lng: number,
},
},
},
formatted_phone_number: string,
url: string,
photos: Array<string>,
opening_hours: {
open_now: boolean,
periods: Array<{
close: {
day: number,
time: string,
},
open: {
day: number,
time: string,
},
}>,
weekday_text: Array<string>,
},
};
export type Period = Map<string, any>;
export type Place = {
id: string,
name: string,
address: string,
latitude: number,
longitude: number,
phoneNumber: string,
googleMapsUrl: string,
photos: List<string>,
thumb: string,
hours: List<string>,
periods: List<Map<string, Period>>,
openNow: boolean,
};
const FoodRecordDefaults: Place = {
id: '',
name: '',
address: '',
latitude: 0,
longitude: 0,
phoneNumber: '',
googleMapsUrl: '',
photos: new List(),
thumb: '',
hours: new List(),
periods: new List(),
openNow: false,
};
const PlaceRecord = Record(FoodRecordDefaults, 'PlaceRecord');
const getPhotos = pathOr([], ['photos']);
const getPhotoRef = photo => photo.photo_reference || '';
const getThumb = pipe(getPhotos, head, getPhotoRef, getURLForPhotoReference({ maxheight: 200 }));
const getPhotoUrls = pipe(getPhotos, map(getPhotoRef), map(getURLForPhotoReference({ maxheight: 600 })));
export const buildPlaceRecord = memoizeWith(
(place: GooglePlaceObj) => place.place_id,
(place: GooglePlaceObj) => {
const {
place_id,
name,
formatted_address,
geometry: { location = {} } = {},
formatted_phone_number,
url,
opening_hours = {},
} = place;
const photos = fromJS(getPhotoUrls(place));
const thumb = getThumb(place);
return new PlaceRecord({
id: place_id,
name: name,
address: formatted_address,
latitude: location.lat,
longitude: location.lng,
phoneNumber: formatted_phone_number,
googleMapsUrl: url,
photos,
thumb,
hours: fromJS(opening_hours.weekday_text),
periods: fromJS(opening_hours.periods),
open: !!opening_hours.open_now,
openNow: opening_hours.open_now,
});
}
);
export default PlaceRecord;