add ability to specify if you can drive or not

This commit is contained in:
Michael Fatemi 2021-07-25 16:44:06 -04:00
parent 9fd6daa826
commit 785934b863
4 changed files with 57 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { useCallback } from 'react'; import { useCallback, useRef } from 'react';
import { green, lightgrey } from '../../lib/colors'; import { green, lightgrey } from '../../lib/colors';
import getPlaceDetails from '../../lib/getPlaceDetails'; import getPlaceDetails from '../../lib/getPlaceDetails';
import { addOrUpdateEventSignup, removeEventSignup } from '../api'; import { addOrUpdateEventSignup, removeEventSignup } from '../api';
@ -12,31 +12,35 @@ import EventSignups from './EventSignups';
export default function EventInterestForm() { export default function EventInterestForm() {
const event = useMutableEvent(); const event = useMutableEvent();
const me = useMe() || { id: 0, name: '' }; const me = useMe() || { id: 0, name: '' };
const placeIdRef = useRef<string | null>(null);
const canDriveRef = useRef(false);
const updateSignup = useCallback( const updateSignup = useCallback(async () => {
async (placeId: string | null) => { const placeId = placeIdRef.current;
await addOrUpdateEventSignup(event.id, placeId); const canDrive = canDriveRef.current;
if (placeId) { await addOrUpdateEventSignup(event.id, placeIdRef.current, canDrive);
const details = await getPlaceDetails(placeId);
event.signups[me.id] = { if (placeId) {
user: { id: me.id, name: me.name }, const details = await getPlaceDetails(placeId);
placeId,
...details, event.signups[me.id] = {
}; user: { id: me.id, name: me.name },
} else { placeId,
event.signups[me.id] = { ...details,
user: { id: me.id, name: me.name }, canDrive,
placeId: null, };
latitude: null, } else {
longitude: null, event.signups[me.id] = {
formattedAddress: null, user: { id: me.id, name: me.name },
}; placeId: null,
} latitude: null,
}, longitude: null,
[event.id, event.signups, me.id, me.name] formattedAddress: null,
); canDrive,
};
}
}, [event.id, event.signups, me.id, me.name]);
const removeSignup = useCallback(async () => { const removeSignup = useCallback(async () => {
await removeEventSignup(event.id); await removeEventSignup(event.id);
@ -48,10 +52,19 @@ export default function EventInterestForm() {
const interested = !!event.signups[me.id]; const interested = !!event.signups[me.id];
const canDrive = !!event.signups[me.id]?.canDrive;
return ( return (
<> <>
<UIButton <UIButton
onClick={interested ? () => removeSignup() : () => updateSignup(null)} onClick={
interested
? () => removeSignup()
: () => {
placeIdRef.current = null;
updateSignup();
}
}
style={{ style={{
backgroundColor: interested ? green : lightgrey, backgroundColor: interested ? green : lightgrey,
color: interested ? 'white' : 'black', color: interested ? 'white' : 'black',
@ -62,10 +75,24 @@ export default function EventInterestForm() {
</UIButton> </UIButton>
{interested && ( {interested && (
<> <>
<UIButton
onClick={() => {
canDriveRef.current = !canDriveRef.current;
updateSignup();
}}
style={{
backgroundColor: canDrive ? green : lightgrey,
color: canDrive ? 'white' : 'black',
transition: 'color 0.2s, background-color 0.2s',
}}
>
{canDrive ? 'Can drive' : "Can't drive"}
</UIButton>
<UIPlacesAutocomplete <UIPlacesAutocomplete
placeholder="Pickup and dropoff location" placeholder="Pickup and dropoff location"
onSelected={(_address, placeId) => { onSelected={(_address, placeId) => {
updateSignup(placeId); placeIdRef.current = placeId;
updateSignup();
}} }}
style={ style={
event.signups[me.id]?.placeId != null event.signups[me.id]?.placeId != null

View File

@ -70,9 +70,10 @@ export async function getEventSignups(
export async function addOrUpdateEventSignup( export async function addOrUpdateEventSignup(
eventId: number, eventId: number,
placeId: string | null placeId: string | null,
canDrive: boolean
) { ) {
await post(`/events/${eventId}/signup`, { placeId }); await post(`/events/${eventId}/signup`, { placeId, canDrive });
} }
export async function removeEventSignup(eventId: number) { export async function removeEventSignup(eventId: number) {

View File

@ -92,6 +92,7 @@ export type IEventSignupComplete = {
id: number; id: number;
name: string; name: string;
}; };
canDrive: boolean;
placeId: string; placeId: string;
formattedAddress: string; formattedAddress: string;
latitude: number; latitude: number;
@ -103,6 +104,7 @@ export type IEventSignupIncomplete = {
id: number; id: number;
name: string; name: string;
}; };
canDrive: boolean;
placeId: null; placeId: null;
formattedAddress: null; formattedAddress: null;
latitude: null; latitude: null;

View File

@ -47,8 +47,6 @@ export default function estimateOptimalPath<
const newWaypoints = sequence.slice(1, sequence.length - 1); const newWaypoints = sequence.slice(1, sequence.length - 1);
console.log({ sequence, path });
return { return {
path: { path: {
from, from,