Skip to content

Commit

Permalink
fix sort options in tournament games table (fixes #90)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Nov 13, 2023
1 parent 56873cb commit f6ce7ee
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/components/databases/TournamentCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionIcon, Paper, Stack, Text, useMantineTheme } from "@mantine/core";
import { IconEye } from "@tabler/icons-react";
import { DataTable } from "mantine-datatable";
import { DataTable, DataTableSortStatus } from "mantine-datatable";
import { useEffect, useState } from "react";
import { NormalizedGame, Tournament, getTournamentGames } from "@/utils/db";
import { createTab } from "@/utils/tabs";
Expand Down Expand Up @@ -35,6 +35,22 @@ function PlayerCard({
};
}, [tournament.id, file]);

const [sort, setSort] = useState<DataTableSortStatus>({
columnAccessor: "date",
direction: "asc",
});

const sortedGames = games.sort((a, b) => {
const key = sort.columnAccessor;
if (sort.direction === "asc") {
/// @ts-expect-error we know they're the same type
return a[key] > b[key] ? 1 : -1;
} else {
/// @ts-expect-error we know they're the same type
return a[key] < b[key] ? 1 : -1;
}
});

return (
<Paper shadow="sm" p="sm" withBorder>
<Stack align="center">
Expand All @@ -44,8 +60,10 @@ function PlayerCard({
<DataTable
withBorder
highlightOnHover
records={games}
records={sortedGames}
height={500}
sortStatus={sort}
onSortStatusChange={setSort}
columns={[
{
accessor: "actions",
Expand Down

0 comments on commit f6ce7ee

Please sign in to comment.