mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-16 00:50:18 -04:00
factor in who's available to drive when calculating optimal path
This commit is contained in:
parent
f443921761
commit
504bc50c9e
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<M extends Location, D extends Location>(
|
||||
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<M extends Location, D extends Location>(
|
|||
|
||||
// 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<M, D>({
|
||||
const path = estimateOptimalPath({
|
||||
from: driver,
|
||||
waypoints: passengerLocations,
|
||||
to: destination,
|
||||
|
@ -36,7 +38,7 @@ export default function useOptimalPath<M extends Location, D extends Location>(
|
|||
}
|
||||
|
||||
return prev;
|
||||
}, null! as { path: Path<M, D>; distance: number });
|
||||
}, null! as { path: Path<IEventSignupComplete, ICarpool['event']>; distance: number });
|
||||
|
||||
return path;
|
||||
}, [destination, members]);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { ICarpool, IEventSignupComplete } from '../components/types';
|
||||
import getDistance from './getdistance';
|
||||
|
||||
export type Location = {
|
||||
|
@ -11,13 +12,10 @@ export type Path<M extends Location, D extends Location> = {
|
|||
to: D;
|
||||
};
|
||||
|
||||
export default function estimateOptimalPath<
|
||||
M extends Location,
|
||||
D extends Location
|
||||
>(
|
||||
path: Path<M, D>
|
||||
export default function estimateOptimalPath(
|
||||
path: Path<IEventSignupComplete, ICarpool['event']>
|
||||
): {
|
||||
path: Path<M, D>;
|
||||
path: Path<IEventSignupComplete, ICarpool['event']>;
|
||||
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),
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user