From f4d7221efd1a94cf9d7e4aa31b5af31da39ea007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20R=C4=85czy?= Date: Sun, 23 Jul 2023 17:50:25 -0700 Subject: [PATCH] Initial implementation with ACTION_SET_NAVIGATION_DESTINATION action and navigationDestination prop --- src/actions/history.js | 9 +++++-- src/actions/index.js | 20 ++++++++++++++ src/actions/types.js | 3 +++ src/components/Dashboard/index.jsx | 5 ++-- src/components/Navigation/index.jsx | 42 ++++++++++++++++++++++++++++- src/reducers/globalState.js | 3 +++ src/url.js | 17 ++++++++++++ 7 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/actions/history.js b/src/actions/history.js index 590b2f291..859dcf439 100644 --- a/src/actions/history.js +++ b/src/actions/history.js @@ -1,6 +1,6 @@ import { LOCATION_CHANGE } from 'connected-react-router'; -import { getDongleID, getZoom, getPrimeNav, getClipsNav } from '../url'; -import { primeNav, selectDevice, selectRange } from './index'; +import { getDongleID, getZoom, getPrimeNav, getClipsNav, getNavigationNav } from '../url'; +import { primeNav, selectDevice, selectRange, navigateToDestination } from './index'; import { clipsExit, fetchClipsDetails, fetchClipsList } from './clips'; export const onHistoryMiddleware = ({ dispatch, getState }) => (next) => (action) => { @@ -24,6 +24,11 @@ export const onHistoryMiddleware = ({ dispatch, getState }) => (next) => (action dispatch(primeNav(pathPrimeNav)); } + const pathNavigationNav = getNavigationNav(action.payload.location.pathname, action.payload.location.search); + if (pathNavigationNav && pathNavigationNav != state.navigationNav) { + dispatch(navigateToDestination(pathNavigationNav.lat, pathNavigationNav.long)); + } + const pathClipsNav = getClipsNav(action.payload.location.pathname); if (pathClipsNav === null && state.clips) { dispatch(clipsExit()); diff --git a/src/actions/index.js b/src/actions/index.js index 22bc8640b..5fc3aa69f 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -132,6 +132,26 @@ export function primeNav(nav, allowPathChange = true) { }; } +export function navigateToDestination(lat, long) { + return (dispatch, getState) => { + const state = getState(); + if (!state.dongleId) { + return; + } + + let destination = { + latitude: lat, + longitude: long, + }; + if (state.navigationDestination !== destination) { + dispatch({ + type: Types.ACTION_SET_NAVIGATION_DESTINATION, + navigationDestination: destination, + }); + } + }; +} + export function fetchSharedDevice(dongleId) { return async (dispatch) => { try { diff --git a/src/actions/types.js b/src/actions/types.js index 86920c155..c3ee0e32a 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -18,6 +18,9 @@ export const ACTION_PRIME_NAV = 'ACTION_PRIME_NAV'; export const ACTION_PRIME_SUBSCRIPTION = 'ACTION_PRIME_SUBSCRIPTION'; export const ACTION_PRIME_SUBSCRIBE_INFO = 'ACTION_PRIME_SUBSCRIBE_INFO'; +// map +export const ACTION_SET_NAVIGATION_DESTINATION = 'ACTION_SET_NAVIGATION_DESTINATION'; + // playback export const ACTION_SEEK = 'action_seek'; export const ACTION_PAUSE = 'action_pause'; diff --git a/src/components/Dashboard/index.jsx b/src/components/Dashboard/index.jsx index a811c5f85..4530205c3 100644 --- a/src/components/Dashboard/index.jsx +++ b/src/components/Dashboard/index.jsx @@ -18,7 +18,7 @@ const DashboardLoading = () => ( ); -const Dashboard = ({ primeNav, device, dongleId }) => { +const Dashboard = ({ primeNav, device, dongleId, navigationDestination }) => { if (!device || !dongleId) { return null; } @@ -30,7 +30,7 @@ const Dashboard = ({ primeNav, device, dongleId }) => { ? : ( <> - + @@ -44,6 +44,7 @@ const stateToProps = Obstruction({ dongleId: 'dongleId', primeNav: 'primeNav', device: 'device', + navigationDestination: 'navigationDestination', }); export default connect(stateToProps)(Dashboard); diff --git a/src/components/Navigation/index.jsx b/src/components/Navigation/index.jsx index 423fb56ec..1f191672d 100644 --- a/src/components/Navigation/index.jsx +++ b/src/components/Navigation/index.jsx @@ -350,7 +350,7 @@ class Navigation extends Component { } componentDidUpdate(prevProps, prevState) { - const { dongleId, device } = this.props; + const { dongleId, device, navigationDestination } = this.props; const { geoLocateCoords, search, carLastLocation, carNetworkLocation, searchSelect, favoriteLocations } = this.state; if ((carLastLocation && !prevState.carLastLocation) || (carNetworkLocation && !prevState.carNetworkLocation) @@ -359,6 +359,12 @@ class Navigation extends Component { this.flyToMarkers(); } + if (prevProps.navigationDestination !== navigationDestination) { + this.getDeviceLastLocation().then(() => { + this.setDestination(navigationDestination.latitude, navigationDestination.longitude); + }) + } + if (prevProps.dongleId !== dongleId) { this.setState({ ...initialState, @@ -399,6 +405,40 @@ class Navigation extends Component { } } + setDestination(latitude, longitude) { + this.focus(); + + const item = { + favoriteId: null, + favoriteIcon: null, + address: { + label: '', + }, + title: '', + resultType: 'place', + position: { + lat: latitude, + lng: longitude, + }, + }; + this.onSearchSelect(item, 'pin'); + reverseLookup([longitude, latitude], false).then((location) => { + if (!location) { + return; + } + + this.setState((prevState) => ({ + searchSelect: { + ...prevState.searchSelect, + address: { + label: location.details, + }, + title: location.place, + }, + })); + }); + } + updateDevice() { if (Demo.isDemo()) { return; diff --git a/src/reducers/globalState.js b/src/reducers/globalState.js index f22abb66d..0d9108ff2 100644 --- a/src/reducers/globalState.js +++ b/src/reducers/globalState.js @@ -431,6 +431,9 @@ export default function reducer(_state, action) { end: action.end, }; break; + case Types.ACTION_SET_NAVIGATION_DESTINATION: + state.navigationDestination = action.navigationDestination + break; default: return state; } diff --git a/src/url.js b/src/url.js index c8dfea558..cdbddd6fe 100644 --- a/src/url.js +++ b/src/url.js @@ -48,3 +48,20 @@ export function getClipsNav(pathname) { } return null; } + +export function getNavigationNav(pathname, search) { + let parts = pathname.split('/'); + parts = parts.filter((m) => m.length); + + if (parts.length >= 2 && parts[0] !== 'auth' && parts[1] == 'navigate') { + let params = new URLSearchParams(search); + if (params.has('lat') && params.has('long')) { + return { + lat: parseFloat(params.get('lat')), + long: parseFloat(params.get('long')), + }; + } + return {}; + } + return null; +} \ No newline at end of file