Skip to content

Commit

Permalink
fix: distance control
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Amorosi committed May 15, 2024
1 parent 8ffb6c6 commit 1852837
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MapView } from "@aws-amplify/ui-react-geo";
import { NavigationControl } from "react-map-gl";
import { GeofencesControl } from "./components/geofences/GeofencesControl";
import { TrackerControl } from "./components/tracking/TrackerControl";
// import { DistanceControl } from "./components/routing/DistanceControl";
import { DistanceControl } from "./components/routing/DistanceControl";

const coordinates = {
longitude: -115.17077150978058,
Expand All @@ -28,6 +28,7 @@ const App = () => {
<NavigationControl position={"top-left"} />
<TrackerControl />
<GeofencesControl />
<DistanceControl />
</MapView>
);
};
Expand Down
15 changes: 9 additions & 6 deletions frontend/src/components/routing/DistanceControl.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useState, useEffect, useCallback, useRef } from "react";
import { Geo, Hub, Auth, Cache } from "aws-amplify";
import { Geo } from "@aws-amplify/geo";
import { fetchAuthSession } from "@aws-amplify/auth";
import { Hub, Cache } from "aws-amplify/utils";
import {
CalculateRouteCommand,
LocationClient,
Expand All @@ -11,16 +13,16 @@ import { UserPositionLabel } from "./UserPositionLabel";

const checkCredentials = async (cachedCredentials) => {
if (!cachedCredentials || cachedCredentials.expiration === undefined) {
const credentials = await Auth.currentCredentials();
Cache.setItem("temporary_credentials", credentials);
return credentials;
const credentials = await fetchAuthSession();
Cache.setItem("temporary_credentials", credentials.credentials);
return credentials.credentials;
}
// If credentials are expired or about to expire, refresh them
if (
(new Date(cachedCredentials.expiration).getTime() - Date.now()) / 1000 <
60
) {
const credentials = await Auth.currentCredentials();
const credentials = await fetchAuthSession();
Cache.setItem("temporary_credentials", credentials);
return credentials;
}
Expand Down Expand Up @@ -61,7 +63,8 @@ export const DistanceControl = () => {
try {
const res = await locationClientRef.current.send(
new CalculateRouteCommand({
CalculatorName: awsmobile.geo.AmazonLocationService.routeCalculator,
CalculatorName:
awsmobile.geo.amazon_location_service.routeCalculator,
TravelMode: "Walking",
DeparturePosition: [userLocation.lng, userLocation.lat],
DestinationPosition: [data.lng, data.lat],
Expand Down

0 comments on commit 1852837

Please sign in to comment.