From bbb9279228976227c797ab037b97b3faa9117d07 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sun, 24 May 2020 13:32:49 +1000 Subject: [PATCH] Fix undefined error in token state sort --- src/components/map/Map.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/map/Map.js b/src/components/map/Map.js index a8e550c..3657aea 100644 --- a/src/components/map/Map.js +++ b/src/components/map/Map.js @@ -206,13 +206,25 @@ function Map({ setIsTokenMenuOpen(true); } + // Sort so vehicles render below other tokens + function sortMapTokenStates(a, b) { + const tokenA = tokensById[a.tokenId]; + const tokenB = tokensById[b.tokenId]; + if (tokenA && tokenB) { + return tokenB.isVehicle - tokenA.isVehicle; + } else if (tokenA) { + return 1; + } else if (tokenB) { + return -1; + } else { + return 0; + } + } + const mapTokens = mapState && Object.values(mapState.tokens) - .sort( - (a, b) => - tokensById[b.tokenId].isVehicle - tokensById[a.tokenId].isVehicle - ) // Sort so vehicles render below other tokens + .sort(sortMapTokenStates) .map((tokenState) => (