- {carpool.name}
+ {carpool.name} {extraDistance !== null && '+ ' + extraDistance}
@@ -132,7 +192,7 @@ export default function Carpools() {
const tentativeInviteNames = useMemo(() => {
if (!signups) return [];
const names = tentativeInvites.map((id) => {
- const signup = signups.find((s) => s.user.id === id);
+ const signup = signups[id];
return signup?.user.name;
});
const nonNull = names.filter((n) => n != null);
diff --git a/src/components/Event/EventContext.tsx b/src/components/Event/EventContext.tsx
index 1da0402..c946df3 100644
--- a/src/components/Event/EventContext.tsx
+++ b/src/components/Event/EventContext.tsx
@@ -8,7 +8,7 @@ const EventContext = createContext({
},
event: null! as IEvent,
default: true,
- signups: null as IEventSignup[] | null,
+ signups: {} as Record
,
addTentativeInvite: (id: number) => {
console.error('not implemented: addTentativeInvite');
},
@@ -20,6 +20,7 @@ const EventContext = createContext({
setHasCarpool: (has: boolean) => {
console.error('not implemented: setHasCarpool');
},
+ myPlaceId: null as string | null,
});
export default EventContext;
diff --git a/src/components/Event/EventSignups.tsx b/src/components/Event/EventSignups.tsx
index f847ada..a7ec3b6 100644
--- a/src/components/Event/EventSignups.tsx
+++ b/src/components/Event/EventSignups.tsx
@@ -110,13 +110,11 @@ function EventSignup({
}
export default function EventSignups({
- signups,
myPlaceId,
}: {
- signups: IEventSignup[];
myPlaceId: string | null;
}) {
- const { event } = useContext(EventContext);
+ const { event, signups } = useContext(EventContext);
const carpools = event.carpools;
const myPlaceDetails = usePlace(myPlaceId);
@@ -125,7 +123,9 @@ export default function EventSignups({
const members = carpools.map((c) => c.members);
const allMembers = members.reduce((a, b) => a.concat(b), []);
const allMembersIds = allMembers.map((m) => m.id);
- return signups.filter((s) => !allMembersIds.includes(s.user.id));
+ return Object.keys(signups)
+ .filter((id) => !allMembersIds.includes(+id))
+ .map((id) => signups[id]);
}, [signups, carpools]);
return (
diff --git a/src/lib/estimateoptimalpath.ts b/src/lib/estimateoptimalpath.ts
index b7b7e9d..c3f4ec1 100644
--- a/src/lib/estimateoptimalpath.ts
+++ b/src/lib/estimateoptimalpath.ts
@@ -1,4 +1,4 @@
-import latlongdist from './latlongdist';
+import getDistance from './getdistance';
export type Location = {
latitude: number;
@@ -11,21 +11,6 @@ export type Path = {
waypoints: Location[];
};
-function getDistance(...locations: Location[]): number {
- let distance = 0;
- for (let i = 0; i < locations.length - 1; i++) {
- const from = locations[i];
- const to = locations[i + 1];
- distance += latlongdist(
- from.latitude,
- from.longitude,
- to.latitude,
- to.longitude
- );
- }
- return distance;
-}
-
export default function estimateOptimalPath(path: Path): {
path: Path;
distance: number;
diff --git a/src/lib/furthestpoint.ts b/src/lib/furthestpoint.ts
new file mode 100644
index 0000000..e97ab8b
--- /dev/null
+++ b/src/lib/furthestpoint.ts
@@ -0,0 +1,20 @@
+import { Location } from './estimateoptimalpath';
+import getDistance from './getdistance';
+
+export default function furthestPoint(
+ locations: Location[],
+ destination: Location
+) {
+ let maxDistance = 0;
+ let maxLocation = { latitude: 0, longitude: 0 };
+ for (let i = 0; i < locations.length; i++) {
+ let distance = getDistance(locations[i], destination);
+
+ if (distance > maxDistance) {
+ maxDistance = distance;
+ maxLocation = locations[i];
+ }
+ }
+
+ return { maxDistance, maxLocation };
+}
diff --git a/src/lib/getPlaceDetails.ts b/src/lib/getPlaceDetails.ts
index 9bb630a..d6275e2 100644
--- a/src/lib/getPlaceDetails.ts
+++ b/src/lib/getPlaceDetails.ts
@@ -8,18 +8,26 @@ export type PlaceDetails = {
longitude: number;
};
+const cache = new Map();
+
export default async function getPlaceDetails(placeId: string) {
+ if (cache.has(placeId)) {
+ return cache.get(placeId)!;
+ }
+
return new Promise((resolve, reject) => {
places.getDetails(
{ placeId, fields: ['name', 'formatted_address', 'geometry'] },
(result, status) => {
if (result || status === 'OK') {
- resolve({
+ const place = {
name: result.name,
formattedAddress: result.formatted_address!,
latitude: result.geometry!.location.lat(),
longitude: result.geometry!.location.lng(),
- });
+ };
+ cache.set(placeId, place);
+ resolve(place);
} else {
reject(new Error('Unexpected Places status ' + status));
}
diff --git a/src/lib/getdistance.ts b/src/lib/getdistance.ts
new file mode 100644
index 0000000..d051038
--- /dev/null
+++ b/src/lib/getdistance.ts
@@ -0,0 +1,17 @@
+import { Location } from './estimateoptimalpath';
+import latlongdist from './latlongdist';
+
+export default function getDistance(...locations: Location[]): number {
+ let distance = 0;
+ for (let i = 0; i < locations.length - 1; i++) {
+ const from = locations[i];
+ const to = locations[i + 1];
+ distance += latlongdist(
+ from.latitude,
+ from.longitude,
+ to.latitude,
+ to.longitude
+ );
+ }
+ return distance;
+}