diff --git a/src/contexts/MapDataContext.js b/src/contexts/MapDataContext.js index 7e61338..d9e2092 100644 --- a/src/contexts/MapDataContext.js +++ b/src/contexts/MapDataContext.js @@ -131,15 +131,13 @@ export function MapDataProvider({ children }) { } async function updateMap(id, update) { - // fake-indexeddb throws an error when updating maps - // TODO: find the root cause of the error + // fake-indexeddb throws an error when updating maps in production. + // Catch that error and use put when it fails try { await database.table("maps").update(id, update); } catch (error) { - // Use put when update fails const map = (await getMapFromDB(id)) || {}; await database.table("maps").put({ ...map, id, ...update }); - console.error(error); } setMaps((prevMaps) => { const newMaps = [...prevMaps]; @@ -149,10 +147,6 @@ export function MapDataProvider({ children }) { } return newMaps; }); - - // Return the updated map and ensure it has the update in it - const updatedMap = (await database.table("maps").get(id)) || {}; - return { ...updatedMap, ...update }; } async function updateMaps(ids, update) { diff --git a/src/modals/SelectMapModal.js b/src/modals/SelectMapModal.js index d60c820..8f745f1 100644 --- a/src/modals/SelectMapModal.js +++ b/src/modals/SelectMapModal.js @@ -280,8 +280,8 @@ function SelectMapModal({ if (selectedMapIds.length === 1) { // Update last used for cache invalidation const lastUsed = Date.now(); - const updatedMap = await updateMap(selectedMapIds[0], { lastUsed }); - onMapChange(updatedMap, selectedMapStates[0]); + await updateMap(selectedMapIds[0], { lastUsed }); + onMapChange({ ...selectedMaps[0], lastUsed }, selectedMapStates[0]); } else { onMapChange(null, null); } diff --git a/src/network/NetworkedMapAndTokens.js b/src/network/NetworkedMapAndTokens.js index 47f74f5..1f1bd21 100644 --- a/src/network/NetworkedMapAndTokens.js +++ b/src/network/NetworkedMapAndTokens.js @@ -247,8 +247,8 @@ function NetworkedMapAndTokens({ session }) { if (cachedMap && cachedMap.lastModified >= newMap.lastModified) { // Update last used for cache invalidation const lastUsed = Date.now(); - const updatedMap = await updateMap(cachedMap.id, { lastUsed }); - setCurrentMap(updatedMap); + await updateMap(cachedMap.id, { lastUsed }); + setCurrentMap({ ...cachedMap, lastUsed }); } else { // Save map data but remove last modified so if there is an error // during the map request the cache is invalid. Also add last used