From 7d6b8e59c76686753e03dee64b94c5f958114090 Mon Sep 17 00:00:00 2001 From: broody Date: Sun, 17 Sep 2023 22:37:20 -0700 Subject: [PATCH] percentage toggle --- web/src/pages/[gameId]/travel.tsx | 116 +++++++++++++++++++----------- 1 file changed, 75 insertions(+), 41 deletions(-) diff --git a/web/src/pages/[gameId]/travel.tsx b/web/src/pages/[gameId]/travel.tsx index df3a505a2..a6aa1db63 100644 --- a/web/src/pages/[gameId]/travel.tsx +++ b/web/src/pages/[gameId]/travel.tsx @@ -13,6 +13,7 @@ import { Grid, GridItem, Spacer, + useDisclosure, } from "@chakra-ui/react"; import { useRouter } from "next/router"; import React, { useCallback, useEffect, useMemo, useState } from "react"; @@ -39,6 +40,8 @@ export default function Travel() { const [targetId, setTargetId] = useState(""); const [currentLocationId, setCurrentLocationId] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); + const { isOpen: isPercentage, onToggle: onTogglePercentage } = + useDisclosure(); const { toast } = useToast(); const { account } = useDojo(); @@ -66,11 +69,20 @@ export default function Travel() { ); const targetMarkets = sortDrugMarkets(locationPrices.get(targetId)); - return targetMarkets.map((drug, index) => ({ - id: drug.id, - price: drug.price, - targetDiff: drug.price - currentMarkets[index].price, - })); + return targetMarkets.map((drug, index) => { + const diff = drug.price - currentMarkets[index].price; + const percentage = + (Math.abs(drug.price - currentMarkets[index].price) / + currentMarkets[index].price) * + 100; + + return { + id: drug.id, + price: drug.price, + diff, + percentage, + }; + }); } return []; @@ -160,42 +172,64 @@ export default function Travel() { {getLocationById(targetId)?.name} Prices - - - - {targetMarkets.map((drug, index) => { - return ( - - - {getDrugById(drug.id)?.icon({ - boxSize: "24px", - })} - ${drug.price.toFixed(0)} - {drug.targetDiff !== 0 && ( - = 0 ? "neon.200" : "red"} - > - ({formatCash(drug.targetDiff)}) - - )} - - - ); - })} - - + + + + + {targetMarkets.map((drug, index) => { + return ( + + + {getDrugById(drug.id)?.icon({ + boxSize: "24px", + })} + ${drug.price.toFixed(0)} + {drug.diff !== 0 && ( + = 0 ? "neon.200" : "red"} + > + ( + {isPercentage + ? `${drug.percentage.toFixed(0)}%` + : formatCash(drug.diff)} + ) + + )} + + + ); + })} + + + + { + onTogglePercentage(); + }} + fontSize="20px" + userSelect="none" + > + {isPercentage ? "%" : "#"} + + +