import { useState, useCallback, useRef, useContext } from 'react'; import Button from '@material-ui/core/Button'; import Card from '@material-ui/core/Card'; import Textarea from '@material-ui/core/TextareaAutosize'; import Typography from '@material-ui/core/Typography'; import Comment from './Comment'; 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', title: 'TJ Carpool', description: 'Carpool from TJ track to homes', start_time: '4/10/2021 3:00 PM', end_time: '4/10/2021 4:00 PM', capacity: 2, participant_ids: [], comments: [ { author_id: 'myfatemi04', id: '1234', body: "what's the vaccination status of everyone?", }, ], driver_id: 'None', create_time: '1234', update_time: '1234', group_id: 'tj', status: 'pending', direction: 'dropoff', author_id: 'michael', type: 'offer', }; export default function Pool({ pool, triggerUpdate, }: { pool: Carpool.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' >(null); const onComment = useCallback>( (e) => { e.preventDefault(); if (!commentTextareaRef.current) { // Wait for ref to be ready return; } if (commentTextareaRef.current.value.length === 0) { // Noop, no comment to send return; } setCommentStatus('pending'); makeAPIPostCall('/comment', { body: commentTextareaRef.current!.value, }) .then(() => { setCommentStatus(null); commentTextareaRef.current!.value = ''; }) .catch(() => { setCommentStatus('errored'); }); }, [] ); const onRegister = useCallback(() => { 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(() => 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 && ( <> {pool.title} Capacity: {pool.participant_ids?.length} / {pool.capacity} Start Time: {pool.start_time || '3:00 PM'} End Time: {pool.end_time || '3:30 PM'} {pool.description} {user && pool.participant_ids?.includes(user._id) ? undefined : mapField} {user && ( )}