From 76e56a0fa492f079265788d9c1a57eca3623f2c4 Mon Sep 17 00:00:00 2001 From: Calli Date: Mon, 1 Jul 2024 16:03:29 +0300 Subject: [PATCH] Add sorting by all fields in totals --- src/app/components/Summary/Summary.tsx | 62 +++++++++++++++++++++----- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/src/app/components/Summary/Summary.tsx b/src/app/components/Summary/Summary.tsx index e65b435..d986a73 100644 --- a/src/app/components/Summary/Summary.tsx +++ b/src/app/components/Summary/Summary.tsx @@ -41,9 +41,13 @@ const displayValue = (valueInMillions: number): string => ? `${(valueInMillions / 1000).toFixed(2)} B` : `${valueInMillions.toFixed(2)} M`; +type SortBy = "name" | "perHour" | "price"; +type SortDirection = "asc" | "desc"; + export const Summary = ({ characters }: { characters: AccessToken[] }) => { const { piPrices } = useContext(SessionContext); - const [sort, setSort] = useState(true); + const [sortDirection, setSortDirection] = useState("asc"); + const [sortBy, setSortBy] = useState("name"); const exports = characters.flatMap((char) => { return char.planets .filter( @@ -102,8 +106,13 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => { setSort(!sort)} + direction={sortDirection} + onClick={() => { + setSortDirection( + sortDirection === "asc" ? "desc" : "asc", + ); + setSortBy("name"); + }} > Exports @@ -111,16 +120,34 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => { - + { + setSortDirection( + sortDirection === "asc" ? "desc" : "asc", + ); + setSortBy("perHour"); + }} + > u/h - + - + { + setSortDirection( + sortDirection === "asc" ? "desc" : "asc", + ); + setSortBy("price"); + }} + > ISK/M - + @@ -128,9 +155,24 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => { {withProductNameAndPrice .sort((a, b) => { - if (a.materialName === b.materialName) return 0; - if (sort) return a.materialName > b.materialName ? 1 : -1; - if (!sort) return a.materialName > b.materialName ? -1 : 1; + if (sortBy === "name") { + if (sortDirection === "asc") + return a.materialName > b.materialName ? 1 : -1; + if (sortDirection === "desc") + return a.materialName > b.materialName ? -1 : 1; + } + if (sortBy === "perHour") { + if (sortDirection === "asc") + return a.amount > b.amount ? 1 : -1; + if (sortDirection === "desc") + return a.amount > b.amount ? -1 : 1; + } + if (sortBy === "price") { + if (sortDirection === "asc") + return a.price > b.price ? 1 : -1; + if (sortDirection === "desc") + return a.price > b.price ? -1 : 1; + } return 0; }) .map((product) => (