Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/MapView/MapView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useState} from 'react';
import {View} from 'react-native';
import Map, {MapRef, Marker} from 'react-map-gl';
import mapboxgl from 'mapbox-gl';

import responder from './responder';
import utils from './utils';
Expand Down Expand Up @@ -66,6 +67,7 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(
>
<Map
ref={setRef}
mapLib={mapboxgl}
mapboxAccessToken={accessToken}
initialViewState={{
longitude: initialState.location[0],
Expand Down
20 changes: 13 additions & 7 deletions src/libs/actions/MapboxToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ const hasTokenExpired = () => moment().isAfter(currentToken.expiration);

const clearToken = () => {
console.debug('[MapboxToken] Deleting the token stored in Onyx');

// Use Onyx.set() to delete the key from Onyx, which will trigger a new token to be retrieved from the API.
Onyx.set(ONYXKEYS.MAPBOX_ACCESS_TOKEN, null);
};
Comment thread
pradeepmdk marked this conversation as resolved.

const fetchToken = () => {
API.read('GetMapboxAccessToken');
isCurrentlyFetchingToken = true;
};

const init = () => {
if (connectionIDForToken) {
console.debug('[MapboxToken] init() is already listening to Onyx so returning early');
Expand Down Expand Up @@ -83,9 +87,7 @@ const init = () => {
// If the token is falsy or an empty object, the token needs to be retrieved from the API.
// The API sets a token in Onyx with a 30 minute expiration.
if (_.isEmpty(token)) {
console.debug('[MapboxToken] Token does not exist so fetching one');
API.read('GetMapboxAccessToken');
isCurrentlyFetchingToken = true;
fetchToken();
return;
}

Expand Down Expand Up @@ -126,9 +128,13 @@ const init = () => {
callback: (val) => {
// When the network reconnects, check if the token has expired. If it has, then clearing the token will
// trigger the fetch of a new one
if (network && network.isOffline && val && !val.isOffline && !isCurrentlyFetchingToken && hasTokenExpired()) {
console.debug('[MapboxToken] Token is expired after network came online');
clearToken();
if (network && network.isOffline && val && !val.isOffline) {
if (_.isEmpty(currentToken)) {
fetchToken();
} else if (!isCurrentlyFetchingToken && hasTokenExpired()) {
console.debug('[MapboxToken] Token is expired after network came online');
clearToken();
}
}
network = val;
},
Expand Down