From b26a89f8cab274746c80f11b000c34e73d96c375 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 27 Aug 2020 17:56:59 +1000 Subject: [PATCH] Added token sorted based off of last modified --- src/components/map/Map.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/components/map/Map.js b/src/components/map/Map.js index a6ce762..4fdd0cf 100644 --- a/src/components/map/Map.js +++ b/src/components/map/Map.js @@ -188,11 +188,29 @@ function Map({ } // Sort so vehicles render below other tokens - function sortMapTokenStates(a, b) { + function sortMapTokenStates(a, b, draggingTokenOptions) { const tokenA = tokensById[a.tokenId]; const tokenB = tokensById[b.tokenId]; if (tokenA && tokenB) { - return tokenB.isVehicle - tokenA.isVehicle; + // If one token is a vehicle and one isn't sort vehicles below + if (tokenB.isVehicle !== tokenA.isVehicle) { + return tokenB.isVehicle - tokenA.isVehicle; + } else if ( + draggingTokenOptions && + draggingTokenOptions.tokenState.id === a.id + ) { + // If dragging token a move above + return 1; + } else if ( + draggingTokenOptions && + draggingTokenOptions.tokenState.id === b.id + ) { + // If dragging token b move above + return -1; + } else { + // Else sort so last modified is on top + return a.lastModified - b.lastModified; + } } else if (tokenA) { return 1; } else if (tokenB) { @@ -205,7 +223,7 @@ function Map({ const mapTokens = mapState && ( {Object.values(mapState.tokens) - .sort(sortMapTokenStates) + .sort((a, b) => sortMapTokenStates(a, b, draggingTokenOptions)) .map((tokenState) => (