From 712c876115474b9ec087d40e91aa9dda209f2642 Mon Sep 17 00:00:00 2001 From: Paul Adamsen <117673433+pauladamseniii@users.noreply.github.com> Date: Fri, 25 Aug 2023 11:53:23 -0400 Subject: [PATCH] CEC-4955 - Digital twin to show vehicle location instead of vehicle paths (#426) * CEC-4955 - Digital twin show vehicle location * fix code smell --- src/components/Cars/Status/DigitalTwinTab.jsx | 4 +- .../DigitalTwinTab.test.jsx.snap | 12 +++- .../Contexts/__mocks__/VehicleContext.jsx | 7 ++- src/components/VehicleMap/index.jsx | 57 +++++++++++++------ src/components/VehiclePathsMap/index.jsx | 13 +++-- src/services/__mocks__/vehiclesAPI.js | 6 +- 6 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/components/Cars/Status/DigitalTwinTab.jsx b/src/components/Cars/Status/DigitalTwinTab.jsx index b7254d6..287426f 100644 --- a/src/components/Cars/Status/DigitalTwinTab.jsx +++ b/src/components/Cars/Status/DigitalTwinTab.jsx @@ -10,7 +10,7 @@ import { VehicleProvider } from "../../Contexts/VehicleContext"; import DigitalTwin from "../../DigitalTwin"; -import VehiclePathsMap from "../../VehiclePathsMap"; +import VehicleMap from "../../VehicleMap"; import useStyles from "../../useStyles"; const REQUEST_INTERVAL = 10000; @@ -60,7 +60,7 @@ const Main = (props) => {
- +
)} diff --git a/src/components/Cars/Status/__snapshots__/DigitalTwinTab.test.jsx.snap b/src/components/Cars/Status/__snapshots__/DigitalTwinTab.test.jsx.snap index c7e54ae..5e87a73 100644 --- a/src/components/Cars/Status/__snapshots__/DigitalTwinTab.test.jsx.snap +++ b/src/components/Cars/Status/__snapshots__/DigitalTwinTab.test.jsx.snap @@ -293,7 +293,17 @@ exports[`DigitalTwinTab Render 1`] = ` />
+ > + Marker +
diff --git a/src/components/Contexts/__mocks__/VehicleContext.jsx b/src/components/Contexts/__mocks__/VehicleContext.jsx index 4387c84..850921f 100644 --- a/src/components/Contexts/__mocks__/VehicleContext.jsx +++ b/src/components/Contexts/__mocks__/VehicleContext.jsx @@ -146,9 +146,10 @@ export const useVehicleContext = () => ({ }), getLocations: jest .fn() - .mockResolvedValue([ - { altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" }, - ]), + .mockResolvedValue({ + // tests only pass without mocking the data here + // data: [{ altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" }] + }), getLocationsVehiclePaths: jest .fn() .mockResolvedValue({ diff --git a/src/components/VehicleMap/index.jsx b/src/components/VehicleMap/index.jsx index 8a56236..f5d9427 100644 --- a/src/components/VehicleMap/index.jsx +++ b/src/components/VehicleMap/index.jsx @@ -15,7 +15,7 @@ import { VehiclePopUp } from "./popup"; import { useLocalStorage } from "../useLocalStorage"; import zoomLocations from './zoomLocations.json'; -const Component = () => { +const Component = (props) => { const classes = useStyles(); const { token: { @@ -47,20 +47,41 @@ const Component = () => { }, [token]); const retrieveAndStoreLocations = (accessToken) => { - return getLocations(accessToken) - .then((result) => { - if (result.data != null && ValidateLocationData(result.data) !== false) { - const points = result.data.map((point) => [ - point.latitude, - point.longitude, - point.vin, - ]); - setMarkers(points); - return points; - } - return []; - }) - .catch((error) => logger.warn(error.stack)); + if (props.vin) { + // if a vin is specified, get its location from vehicle state + return getState(accessToken, props.vin) + .then(async (stateResult) => { + if (stateResult.data?.location) { + if (ValidateLocationData(stateResult.data.location) !== false) { + const points = [[ + stateResult.data.location.latitude, + stateResult.data.location.longitude, + props.vin, + ]]; + setMarkers(points); + return points; + } + } + return []; + }) + .catch((error) => logger.warn(error.stack)); + } else { + // if no vin is specified, use getLocations + return getLocations(accessToken) + .then(async (result) => { + if (result.data != null && ValidateLocationData(result.data) !== false) { + const points = result.data.map((point) => [ + point.latitude, + point.longitude, + point.vin, + ]); + setMarkers(points); + return points; + } + return []; + }) + .catch((error) => logger.warn(error.stack)); + } }; const centerAroundMarkers = (points) => { @@ -154,7 +175,7 @@ const Component = () => { return ( <> - + {!props.vin && } { return null; }; -const VehicleMap = () => ( +const VehicleMap = (props) => ( - + ); diff --git a/src/components/VehiclePathsMap/index.jsx b/src/components/VehiclePathsMap/index.jsx index 18929fd..1dc7212 100644 --- a/src/components/VehiclePathsMap/index.jsx +++ b/src/components/VehiclePathsMap/index.jsx @@ -64,13 +64,14 @@ const ComponentVehiclePathsMap = (props) => { } } } else { - await getState(token, vinLocations[0]).then((stateResult) => { - if (stateResult.data && stateResult.data.location) { - if (ValidateLocationData(stateResult.data.location) !== false) { - path[1].push([stateResult.data.location.latitude, stateResult.data.location.longitude]); + await getState(accessToken, vinLocations[0]) + .then(async (stateResult) => { + if (stateResult.data?.location) { + if (ValidateLocationData(stateResult.data.location) !== false) { + path[1].push([stateResult.data.location.latitude, stateResult.data.location.longitude]); + } } - } - }); + }); } points.push(path); } diff --git a/src/services/__mocks__/vehiclesAPI.js b/src/services/__mocks__/vehiclesAPI.js index 4dc8469..c5d6181 100644 --- a/src/services/__mocks__/vehiclesAPI.js +++ b/src/services/__mocks__/vehiclesAPI.js @@ -134,9 +134,9 @@ const vehiclesAPI = { }, getLocations: jest .fn() - .mockResolvedValue([ - { altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" }, - ]), + .mockResolvedValue( + { data: [{ altitude: 5, longitude: 10, latitude: 15, vin: "TESTVIN123" }] }, + ), getLocationsVehiclePaths: async () => { return { '3FAFP13P31R199430': [[16.891136999999986, 26.832352999999955], [56.891136999999986, 66.832352999999955], [26.891136999999986, 36.832352999999955]],