From 567a80029ca5a9e29df211273ba675f9ce5b7bba Mon Sep 17 00:00:00 2001 From: Tim Pechersky Date: Thu, 23 Sep 2021 19:18:49 +0200 Subject: [PATCH] added sorting selection by label and date --- frontend/src/components/TokensList.js | 109 ++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/TokensList.js b/frontend/src/components/TokensList.js index 43f5da6c..10c4d7ed 100644 --- a/frontend/src/components/TokensList.js +++ b/frontend/src/components/TokensList.js @@ -9,31 +9,53 @@ import { Tbody, Editable, EditableInput, + Button, EditablePreview, } from "@chakra-ui/react"; -import { DeleteIcon } from "@chakra-ui/icons"; +import { DeleteIcon, TriangleDownIcon } from "@chakra-ui/icons"; import moment from "moment"; import CopyButton from "./CopyButton"; +const SORT_BY_TYPES = { + DATE: 0, + LABEL: 1, +}; +const SORT_DIRECTION_TYPES = { + ASC: true, + DESC: false, +}; const List = ({ data, revoke, isLoading, update }) => { const [stateData, setStateData] = useState(data); const userToken = localStorage.getItem("MOONSTREAM_ACCESS_TOKEN"); - + const [sortBy, setSortBy] = useState({ + column: SORT_BY_TYPES.LABEL, + direction: SORT_DIRECTION_TYPES.ASC, + }); useEffect(() => { if (data?.token?.length > 0) { const sortedTokens = data.token.sort(function (a, b) { var aName = a?.note?.toUpperCase(); var bName = b?.note?.toUpperCase(); - if (a.note || b.note) { - return aName < bName ? -1 : aName < bName ? 1 : 0; + + if ((a.note || b.note) && sortBy.column === SORT_BY_TYPES.LABEL) { + if (!b.note) return -1; + if (sortBy.direction === SORT_DIRECTION_TYPES.ASC) { + return aName < bName ? -1 : aName > bName ? 1 : 0; + } else { + return aName > bName ? -1 : aName < bName ? 1 : 0; + } } else { - return new Date(b.created_at) - new Date(a.created_at); + if (sortBy.direction === SORT_DIRECTION_TYPES.ASC) { + return new Date(b.created_at) - new Date(a.created_at); + } else { + return new Date(a.created_at) - new Date(b.created_at); + } } }); setStateData({ ...data, token: [...sortedTokens] }); } - }, [data]); + }, [data, sortBy]); const cellProps = { px: ["2px", "6px", "inherit"], @@ -50,9 +72,80 @@ const List = ({ data, revoke, isLoading, update }) => { > - Label + + + Token - Date Created + + + + {/*