diff --git a/src/components/Pool.tsx b/src/components/Pool.tsx index dbe7367..465758a 100644 --- a/src/components/Pool.tsx +++ b/src/components/Pool.tsx @@ -8,6 +8,11 @@ import { makeAPIPostCall } from '../api/utils'; import AuthenticationContext from './AuthenticationContext'; import PoolMap from './PoolMap'; +import PlacesAutocomplete, { + geocodeByAddress, + getLatLng, +} from 'react-places-autocomplete'; + // eslint-disable-next-line const SAMPLE_POOL = { id: '123', @@ -42,7 +47,14 @@ export default function Pool({ triggerUpdate: Function; }) { const { user } = useContext(AuthenticationContext); + const [address, setAddress] = useState(''); + const handleChange = (address: string) => { + setAddress(address); + }; + const handleSelect = (address: string) => { + setAddress(address); + }; const commentTextareaRef = useRef(null); const [commentStatus, setCommentStatus] = useState< null | 'pending' | 'errored' @@ -79,13 +91,78 @@ export default function Pool({ ); const onRegister = useCallback(() => { - makeAPIPostCall(`/pools/${pool._id}/join`).then(() => triggerUpdate()); - }, [pool._id, triggerUpdate]); + makeAPIPostCall(`/pools/${pool._id}/join`) + .then(() => geocodeByAddress(address)) + .then((results) => getLatLng(results[0])) + .then(({ lat, lng }) => + makeAPIPostCall(`/addresses`, { + pool: pool._id, + location: address, + lat: lat, + lng: lng, + }) + ) + .then(() => triggerUpdate()); + }, [pool._id, address, triggerUpdate]); const onUnregister = useCallback(() => { - makeAPIPostCall(`/pools/${pool._id}/leave`).then(() => triggerUpdate()); + makeAPIPostCall(`/pools/${pool._id}/leave`) + .then(() => + makeAPIPostCall(`/addresses/remove`, { + pool: pool._id, + }) + ) + .then(() => triggerUpdate()); }, [pool._id, triggerUpdate]); + const mapField = ( +
+ + {({ getInputProps, suggestions, getSuggestionItemProps, loading }) => ( +
+ + +
+ {loading &&
Loading...
} + {suggestions.map((suggestion) => { + const className = suggestion.active + ? 'suggestion-item--active' + : 'suggestion-item'; + // inline style for demonstration purpose + const style = suggestion.active + ? { backgroundColor: '#fafafa', cursor: 'pointer' } + : { backgroundColor: '#ffffff', cursor: 'pointer' }; + return ( +
+ {suggestion.description} +
+ ); + })} +
+
+ )} +
+
+ ); + return ( {pool && ( @@ -103,6 +180,9 @@ export default function Pool({ End Time: {pool.end_time || '3:30 PM'} {pool.description} + {user && pool.participant_ids?.includes(user._id) + ? undefined + : mapField} {user && (