From 504bc50c9e2d03ab11e3a51cbfd2a71252f415f4 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Sun, 25 Jul 2021 18:15:13 -0400 Subject: [PATCH] factor in who's available to drive when calculating optimal path --- src/components/Event/EventCarpools.tsx | 31 ++++++-------------------- src/components/useOptimalPath.ts | 20 +++++++++-------- src/lib/estimateoptimalpath.ts | 12 +++++----- 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/components/Event/EventCarpools.tsx b/src/components/Event/EventCarpools.tsx index 25558ec..09bef14 100644 --- a/src/components/Event/EventCarpools.tsx +++ b/src/components/Event/EventCarpools.tsx @@ -2,14 +2,13 @@ import CancelIcon from '@material-ui/icons/Cancel'; import CheckIcon from '@material-ui/icons/Check'; import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople'; import { useCallback, useContext, useMemo } from 'react'; -import { Location } from '../../lib/estimateoptimalpath'; import { useCancelCarpoolRequest, useInvitationState, useSendCarpoolRequest, } from '../../state/Notifications/NotificationsHooks'; import { useMe } from '../hooks'; -import { IEvent } from '../types'; +import { IEvent, IEventSignupComplete } from '../types'; import useOptimalPath from '../useOptimalPath'; import EventContext from './EventContext'; import { useMySignup } from './EventHooks'; @@ -34,7 +33,7 @@ function useMemberLocations(members: IEvent['carpools'][0]['members']) { longitude: signup.longitude, }; }) - .filter(Boolean) as Location[], + .filter(Boolean) as IEventSignupComplete[], [members, signups] ); } @@ -60,37 +59,21 @@ function CarpoolRow({ cancelCarpoolRequest(carpool.id); }, [cancelCarpoolRequest, carpool.id]); - const { - event: { latitude, longitude }, - } = useContext(EventContext); + const { event } = useContext(EventContext); const mySignup = useMySignup(); - const myLocation = - mySignup && mySignup.latitude !== null - ? { - latitude: mySignup.latitude, - longitude: mySignup.longitude, - } - : null; - const memberLocations = useMemberLocations(carpool.members); - const pathInCarpool = useOptimalPath(memberLocations, { - latitude, - longitude, - }); + const pathInCarpool = useOptimalPath(memberLocations, event); const pathNotInCarpool = useOptimalPath( - myLocation ? [...memberLocations, myLocation] : [], - { - latitude, - longitude, - } + mySignup.latitude !== null ? [...memberLocations, mySignup] : [], + event ); const extraDistance = - myLocation && pathInCarpool && pathNotInCarpool + mySignup.latitude !== null && pathInCarpool && pathNotInCarpool ? pathInCarpool.distance - pathNotInCarpool.distance : null; diff --git a/src/components/useOptimalPath.ts b/src/components/useOptimalPath.ts index aa49bee..b81b7e8 100644 --- a/src/components/useOptimalPath.ts +++ b/src/components/useOptimalPath.ts @@ -1,12 +1,10 @@ import { useDebugValue, useMemo } from 'react'; -import estimateOptimalPath, { - Location, - Path, -} from '../lib/estimateoptimalpath'; +import estimateOptimalPath, { Path } from '../lib/estimateoptimalpath'; +import { ICarpool, IEventSignupComplete } from './types'; -export default function useOptimalPath( - members: M[], - destination: D +export default function useOptimalPath( + members: IEventSignupComplete[], + destination: ICarpool['event'] ) { const path = useMemo(() => { if (members.length === 0) { @@ -15,13 +13,17 @@ export default function useOptimalPath( // O(n^2) const path = members.reduce((prev, driver) => { + if (!driver.canDrive) { + return prev; + } + // O(n) const passengerLocations = members.filter( (location) => location !== driver ); // O(n) - const path = estimateOptimalPath({ + const path = estimateOptimalPath({ from: driver, waypoints: passengerLocations, to: destination, @@ -36,7 +38,7 @@ export default function useOptimalPath( } return prev; - }, null! as { path: Path; distance: number }); + }, null! as { path: Path; distance: number }); return path; }, [destination, members]); diff --git a/src/lib/estimateoptimalpath.ts b/src/lib/estimateoptimalpath.ts index 794cacb..dc8e992 100644 --- a/src/lib/estimateoptimalpath.ts +++ b/src/lib/estimateoptimalpath.ts @@ -1,3 +1,4 @@ +import { ICarpool, IEventSignupComplete } from '../components/types'; import getDistance from './getdistance'; export type Location = { @@ -11,13 +12,10 @@ export type Path = { to: D; }; -export default function estimateOptimalPath< - M extends Location, - D extends Location ->( - path: Path +export default function estimateOptimalPath( + path: Path ): { - path: Path; + path: Path; distance: number; } { const { from, to, waypoints } = path; @@ -51,7 +49,7 @@ export default function estimateOptimalPath< path: { from, to, - waypoints: newWaypoints as M[], + waypoints: newWaypoints as IEventSignupComplete[], }, distance: getDistance(from, ...sequence, to), };