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 CheckIcon from '@material-ui/icons/Check';
|
||||||
import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople';
|
import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople';
|
||||||
import { useCallback, useContext, useMemo } from 'react';
|
import { useCallback, useContext, useMemo } from 'react';
|
||||||
import { Location } from '../../lib/estimateoptimalpath';
|
|
||||||
import {
|
import {
|
||||||
useCancelCarpoolRequest,
|
useCancelCarpoolRequest,
|
||||||
useInvitationState,
|
useInvitationState,
|
||||||
useSendCarpoolRequest,
|
useSendCarpoolRequest,
|
||||||
} from '../../state/Notifications/NotificationsHooks';
|
} from '../../state/Notifications/NotificationsHooks';
|
||||||
import { useMe } from '../hooks';
|
import { useMe } from '../hooks';
|
||||||
import { IEvent } from '../types';
|
import { IEvent, IEventSignupComplete } from '../types';
|
||||||
import useOptimalPath from '../useOptimalPath';
|
import useOptimalPath from '../useOptimalPath';
|
||||||
import EventContext from './EventContext';
|
import EventContext from './EventContext';
|
||||||
import { useMySignup } from './EventHooks';
|
import { useMySignup } from './EventHooks';
|
||||||
|
@ -34,7 +33,7 @@ function useMemberLocations(members: IEvent['carpools'][0]['members']) {
|
||||||
longitude: signup.longitude,
|
longitude: signup.longitude,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean) as Location[],
|
.filter(Boolean) as IEventSignupComplete[],
|
||||||
[members, signups]
|
[members, signups]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -60,37 +59,21 @@ function CarpoolRow({
|
||||||
cancelCarpoolRequest(carpool.id);
|
cancelCarpoolRequest(carpool.id);
|
||||||
}, [cancelCarpoolRequest, carpool.id]);
|
}, [cancelCarpoolRequest, carpool.id]);
|
||||||
|
|
||||||
const {
|
const { event } = useContext(EventContext);
|
||||||
event: { latitude, longitude },
|
|
||||||
} = useContext(EventContext);
|
|
||||||
|
|
||||||
const mySignup = useMySignup();
|
const mySignup = useMySignup();
|
||||||
|
|
||||||
const myLocation =
|
|
||||||
mySignup && mySignup.latitude !== null
|
|
||||||
? {
|
|
||||||
latitude: mySignup.latitude,
|
|
||||||
longitude: mySignup.longitude,
|
|
||||||
}
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const memberLocations = useMemberLocations(carpool.members);
|
const memberLocations = useMemberLocations(carpool.members);
|
||||||
|
|
||||||
const pathInCarpool = useOptimalPath(memberLocations, {
|
const pathInCarpool = useOptimalPath(memberLocations, event);
|
||||||
latitude,
|
|
||||||
longitude,
|
|
||||||
});
|
|
||||||
|
|
||||||
const pathNotInCarpool = useOptimalPath(
|
const pathNotInCarpool = useOptimalPath(
|
||||||
myLocation ? [...memberLocations, myLocation] : [],
|
mySignup.latitude !== null ? [...memberLocations, mySignup] : [],
|
||||||
{
|
event
|
||||||
latitude,
|
|
||||||
longitude,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const extraDistance =
|
const extraDistance =
|
||||||
myLocation && pathInCarpool && pathNotInCarpool
|
mySignup.latitude !== null && pathInCarpool && pathNotInCarpool
|
||||||
? pathInCarpool.distance - pathNotInCarpool.distance
|
? pathInCarpool.distance - pathNotInCarpool.distance
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
import { useDebugValue, useMemo } from 'react';
|
import { useDebugValue, useMemo } from 'react';
|
||||||
import estimateOptimalPath, {
|
import estimateOptimalPath, { Path } from '../lib/estimateoptimalpath';
|
||||||
Location,
|
import { ICarpool, IEventSignupComplete } from './types';
|
||||||
Path,
|
|
||||||
} from '../lib/estimateoptimalpath';
|
|
||||||
|
|
||||||
export default function useOptimalPath<M extends Location, D extends Location>(
|
export default function useOptimalPath(
|
||||||
members: M[],
|
members: IEventSignupComplete[],
|
||||||
destination: D
|
destination: ICarpool['event']
|
||||||
) {
|
) {
|
||||||
const path = useMemo(() => {
|
const path = useMemo(() => {
|
||||||
if (members.length === 0) {
|
if (members.length === 0) {
|
||||||
|
@ -15,13 +13,17 @@ export default function useOptimalPath<M extends Location, D extends Location>(
|
||||||
|
|
||||||
// O(n^2)
|
// O(n^2)
|
||||||
const path = members.reduce((prev, driver) => {
|
const path = members.reduce((prev, driver) => {
|
||||||
|
if (!driver.canDrive) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
// O(n)
|
// O(n)
|
||||||
const passengerLocations = members.filter(
|
const passengerLocations = members.filter(
|
||||||
(location) => location !== driver
|
(location) => location !== driver
|
||||||
);
|
);
|
||||||
|
|
||||||
// O(n)
|
// O(n)
|
||||||
const path = estimateOptimalPath<M, D>({
|
const path = estimateOptimalPath({
|
||||||
from: driver,
|
from: driver,
|
||||||
waypoints: passengerLocations,
|
waypoints: passengerLocations,
|
||||||
to: destination,
|
to: destination,
|
||||||
|
@ -36,7 +38,7 @@ export default function useOptimalPath<M extends Location, D extends Location>(
|
||||||
}
|
}
|
||||||
|
|
||||||
return prev;
|
return prev;
|
||||||
}, null! as { path: Path<M, D>; distance: number });
|
}, null! as { path: Path<IEventSignupComplete, ICarpool['event']>; distance: number });
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
}, [destination, members]);
|
}, [destination, members]);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ICarpool, IEventSignupComplete } from '../components/types';
|
||||||
import getDistance from './getdistance';
|
import getDistance from './getdistance';
|
||||||
|
|
||||||
export type Location = {
|
export type Location = {
|
||||||
|
@ -11,13 +12,10 @@ export type Path<M extends Location, D extends Location> = {
|
||||||
to: D;
|
to: D;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function estimateOptimalPath<
|
export default function estimateOptimalPath(
|
||||||
M extends Location,
|
path: Path<IEventSignupComplete, ICarpool['event']>
|
||||||
D extends Location
|
|
||||||
>(
|
|
||||||
path: Path<M, D>
|
|
||||||
): {
|
): {
|
||||||
path: Path<M, D>;
|
path: Path<IEventSignupComplete, ICarpool['event']>;
|
||||||
distance: number;
|
distance: number;
|
||||||
} {
|
} {
|
||||||
const { from, to, waypoints } = path;
|
const { from, to, waypoints } = path;
|
||||||
|
@ -51,7 +49,7 @@ export default function estimateOptimalPath<
|
||||||
path: {
|
path: {
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
waypoints: newWaypoints as M[],
|
waypoints: newWaypoints as IEventSignupComplete[],
|
||||||
},
|
},
|
||||||
distance: getDistance(from, ...sequence, to),
|
distance: getDistance(from, ...sequence, to),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user