diff --git a/src/api/google.ts b/src/api/google.ts index 0ca502e..60578aa 100644 --- a/src/api/google.ts +++ b/src/api/google.ts @@ -1,16 +1,19 @@ -export const GOOGLE_MAPS_API_KEY = 'AIzaSyDUnWIrt-H4RuP2YFLpVPz4oAjBhpOOoyI'; +export type PlaceDetails = { + formattedAddress: string; + latitude: number; + longitude: number; +}; -export async function searchForPlaces(query: string) { - const url = new URL( - 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json' - ); - url.searchParams.set('key', GOOGLE_MAPS_API_KEY); - url.searchParams.set('input', query); - url.searchParams.set('inputtype', 'textquery'); - url.searchParams.set('fields', 'place_id,name,formatted_address'); - console.log(url.toString()); - let res = await fetch(url.toString(), { mode: 'no-cors' }); - let json = await res.text(); +export async function getPlaceDetails( + placeId: string +): Promise { + if (placeId == null) { + console.warn('placeId was null'); + return null; + } + + const result = await fetch('http://localhost:5000/api/place/' + placeId); + const json = await result.json(); return json; } diff --git a/src/components/NewUI/Event.tsx b/src/components/NewUI/Event.tsx index c260fb2..b6804cc 100644 --- a/src/components/NewUI/Event.tsx +++ b/src/components/NewUI/Event.tsx @@ -4,6 +4,7 @@ import UIButton from './UIButton'; import UIPlacesAutocomplete from './UIPlacesAutocomplete'; import UISecondaryBox from './UISecondaryBox'; import UISecondaryHeader from './UISecondaryHeader'; +import usePlace from './usePlace'; import useThrottle from './useThrottle'; import useToggle from './useToggle'; @@ -205,41 +206,46 @@ const dummyPeopleData: IPerson[] = [ longitude: 10.12, }, ]; -function People({ event }: { event: IEvent }) { +function People({ event, placeId }: { event: IEvent; placeId: string }) { const PADDING = '1rem'; // eslint-disable-next-line const [people, setPeople] = useState(dummyPeopleData); + const placeDetails = usePlace(placeId); const myLatitude = 10; const myLongitude = 10; - const locationLatitude = 12; - const locationLongitude = 12; + return (

People

{people.map(({ name, latitude, longitude, id }) => { - const meToThem = latlongdist( - latitude, - longitude, - myLatitude, - myLongitude, - R_miles - ); - const themToLocation = latlongdist( - latitude, - longitude, - locationLatitude, - locationLongitude, - R_miles - ); - const totalWithThem = meToThem + themToLocation; - const totalWithoutThem = latlongdist( - myLatitude, - myLongitude, - locationLatitude, - locationLongitude, - R_miles - ); - const extraDistance = totalWithThem - totalWithoutThem; + let extraDistance = null; + if (placeDetails != null) { + const locationLatitude = placeDetails.latitude; + const locationLongitude = placeDetails.longitude; + const meToThem = latlongdist( + latitude, + longitude, + myLatitude, + myLongitude, + R_miles + ); + const themToLocation = latlongdist( + latitude, + longitude, + locationLatitude, + locationLongitude, + R_miles + ); + const totalWithThem = meToThem + themToLocation; + const totalWithoutThem = latlongdist( + myLatitude, + myLongitude, + locationLatitude, + locationLongitude, + R_miles + ); + extraDistance = totalWithThem - totalWithoutThem; + } return (
- {name}: +{extraDistance.toFixed(1)} miles + {name} + {extraDistance ? `: +${extraDistance.toFixed(1)} miles` : ''}
(null!); + const [placeId, setPlaceId] = useState(null!); const [interested, toggleInterested] = useToggle(false); const toggleInterestedThrottled = useThrottle(toggleInterested, 500); @@ -302,11 +309,9 @@ export default function Event({ event }: { event: IEvent }) { { - setLocationPlaceId(placeID); + setPlaceId(placeID); }} - style={ - locationPlaceId != null ? { border: '2px solid ' + green } : {} - } + style={placeId != null ? { border: '2px solid ' + green } : {}} /> {false && (
)} - + )} diff --git a/src/components/NewUI/usePlace.ts b/src/components/NewUI/usePlace.ts new file mode 100644 index 0000000..ffbc0fb --- /dev/null +++ b/src/components/NewUI/usePlace.ts @@ -0,0 +1,24 @@ +import { useState, useEffect, useCallback } from 'react'; +import { getPlaceDetails, PlaceDetails } from '../../api/google'; +import useThrottle from './useThrottle'; + +export default function usePlace(placeId: string) { + const [placeDetails, setPlaceDetails] = useState(null); + + const updatePlaceDetails = useCallback(() => { + if (placeId == null) { + setPlaceDetails(null); + } else { + getPlaceDetails(placeId).then(setPlaceDetails); + } + }, [placeId]); + + const updatePlaceDetailsThrottled = useThrottle(updatePlaceDetails, 500); + + useEffect(updatePlaceDetailsThrottled, [ + placeId, + updatePlaceDetailsThrottled, + ]); + + return placeDetails; +} diff --git a/src/components/NewUI/useThrottle.ts b/src/components/NewUI/useThrottle.ts index c399f74..a108d38 100644 --- a/src/components/NewUI/useThrottle.ts +++ b/src/components/NewUI/useThrottle.ts @@ -1,11 +1,13 @@ import { useMemo } from 'react'; -function throttle any, T>( +type Void = (...args: any) => void; + +export function throttle( this: T, - callback: F, + callback: Void, limit: number, thisArg?: T -): F { +): (...args: Parameters) => void { let waiting = false; let pendingArgs: null | any[] = null; const scope = thisArg ?? this;