From 64671bd025e76e0ebf40363718d299bafa94a3e0 Mon Sep 17 00:00:00 2001 From: Marco Escaleira Date: Thu, 2 May 2024 22:20:30 +0100 Subject: [PATCH] feat: allow sorting on quizzes table --- src/pages/Quizzes/index.tsx | 64 +++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/pages/Quizzes/index.tsx b/src/pages/Quizzes/index.tsx index 50d704c..84749cf 100644 --- a/src/pages/Quizzes/index.tsx +++ b/src/pages/Quizzes/index.tsx @@ -1,8 +1,14 @@ import { useMemo } from "react"; import { useQuery } from "@apollo/client"; import { Button, Card, CardBody, CardFooter, CardHeader, Spinner, Typography } from "@material-tailwind/react"; -import { flexRender, getCoreRowModel, SortDirection, useReactTable } from "@tanstack/react-table"; -import { ChevronsUpDown } from "lucide-react"; +import { + flexRender, + getCoreRowModel, + getSortedRowModel, + useReactTable, + getPaginationRowModel, +} from "@tanstack/react-table"; +import { ChevronDown, ChevronsUpDown, ChevronUp } from "lucide-react"; import { GET_QUIZZES } from "@utils/queries/Quizzes.ts"; import { mapQuizList } from "./mapTableData.ts"; import { tableColumns } from "./tableColumns.tsx"; @@ -11,12 +17,13 @@ export function Component() { const { data, loading, error } = useQuery(GET_QUIZZES, { variables: {} }); const quizzes = useMemo(() => mapQuizList(data?.quizList || []), [data?.quizList]); - // const [columnFilters, setColumnFilters] = useState([]); const table = useReactTable({ data: quizzes, columns: tableColumns, getCoreRowModel: getCoreRowModel(), + getSortedRowModel: getSortedRowModel(), + getPaginationRowModel: getPaginationRowModel(), }); if (loading || error) @@ -43,27 +50,36 @@ export function Component() { {table.getHeaderGroups().map(headerGroup => ( - {headerGroup.headers.map(header => ( - -
- - {header.column.columnDef.header?.toString()} - - {header.column.getCanSort() && ( - - )} - { - { - asc: "🔼", - desc: "🔽", - }[header.column.getIsSorted() as SortDirection] - } -
- - ))} + {headerGroup.headers.map(header => { + const chevronProps = { + className: "ml-1 size-5", + onClick: header.column.getToggleSortingHandler(), + }; + let sortChevron = ; + + if (header.column.getIsSorted() === "asc") { + sortChevron = ; + } + if (header.column.getIsSorted() === "desc") { + sortChevron = ; + } + + return ( + +
+ + {header.column.columnDef.header?.toString()} + +
+ {header.column.getCanSort() && sortChevron} +
+
+ + ); + })} ))}