import { List, Map, Record, fromJS } from 'immutable'; import { pipe, pathOr, map, head, cond, test } from 'ramda'; import { getURLForPhotoReference } from '../apis/GooglePlacesApi'; 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, opening_hours: { open_now: boolean, periods: Array<{ close: { day: number, time: string, }, open: { day: number, time: string, }, }>, weekday_text: Array, }, distance: number, }; export type Period = Map; export type Place = { id: string, name: string, address: string, latitude: number, longitude: number, distance: number, phoneNumber: string, googleMapsUrl: string, photos: List, thumb: string, hours: List, periods: List>, openNow: boolean, }; const FoodRecordDefaults: Place = { id: '', name: '', placeType: '', address: '', latitude: 0, longitude: 0, distance: 999, 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 = (place: ?GooglePlaceObj) => { if (!place) { return; } const { place_id, name, formatted_address, geometry: { location = {} } = {}, formatted_phone_number, url, opening_hours = {}, distance, } = place; const photos = fromJS(getPhotoUrls(place)); const thumb = getThumb(place); const getPlaceType = cond([ [test(/h.?e.?b/i), () => 'HEB'], [test(/whole.?foods/i), () => 'WholeFoods'], ]); const placeType = getPlaceType(name); return new PlaceRecord({ id: place_id, name: name, placeType, address: formatted_address, latitude: location.lat, longitude: location.lng, distance, phoneNumber: formatted_phone_number, googleMapsUrl: url, photos, thumb, hours: fromJS(opening_hours.weekday_text), periods: fromJS(opening_hours.periods), openNow: opening_hours.open_now, }); }; export default PlaceRecord;