diff --git a/src/components/DataTable.tsx b/src/components/DataTable.tsx new file mode 100644 index 0000000..99b8152 --- /dev/null +++ b/src/components/DataTable.tsx @@ -0,0 +1,113 @@ +import { + flexRender, + getCoreRowModel, + useReactTable, + getPaginationRowModel, + SortingState, + getSortedRowModel, +} from "@tanstack/react-table" + +import { Button } from "./ui/button" +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table" +import { useState } from "react" + +export function DataTable({ columns, data, loading }) { + const [sorting, setSorting] = useState([]) + const table = useReactTable({ + data, + columns, + getCoreRowModel: getCoreRowModel(), + getPaginationRowModel: getPaginationRowModel(), + onSortingChange: setSorting, + getSortedRowModel: getSortedRowModel(), + state: { + sorting, + }, + }) + + const pageCount = table.getPageCount() + const currentPage = table.getState().pagination?.pageIndex + return ( +
+
+ + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => { + return ( + + {header.isPlaceholder + ? null + : flexRender( + header.column.columnDef.header, + header.getContext(), + )} + + ) + })} + + ))} + + + {table.getRowModel().rows?.length ? ( + table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + {flexRender(cell.column.columnDef.cell, cell.getContext())} + + ))} + + )) + ) : loading ? ( + + + Loading... + + + ) : ( + + + No results. + + + )} + +
+
+
+
+ {`${currentPage + 1} of ${pageCount}`} +
+
+ + +
+
+
+ ) +} diff --git a/src/components/RecentEditsDataTable.tsx b/src/components/RecentEditsDataTable.tsx deleted file mode 100644 index 5794add..0000000 --- a/src/components/RecentEditsDataTable.tsx +++ /dev/null @@ -1,104 +0,0 @@ -import { - flexRender, - getCoreRowModel, - useReactTable, - getPaginationRowModel, -} from "@tanstack/react-table" - -import { Button } from "./ui/button" -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table" - -export function RecentEditsDataTable({ columns, data, loading }) { - const table = useReactTable({ - data, - columns, - getCoreRowModel: getCoreRowModel(), - getPaginationRowModel: getPaginationRowModel(), - }) - - const pageCount = table.getPageCount() - const currentPage = table.getState().pagination?.pageIndex - return ( -
-
- - - {table.getHeaderGroups().map((headerGroup) => ( - - {headerGroup.headers.map((header) => { - return ( - - {header.isPlaceholder - ? null - : flexRender( - header.column.columnDef.header, - header.getContext(), - )} - - ) - })} - - ))} - - - {table.getRowModel().rows?.length ? ( - table.getRowModel().rows.map((row) => ( - - {row.getVisibleCells().map((cell) => ( - - {flexRender(cell.column.columnDef.cell, cell.getContext())} - - ))} - - )) - ) : loading ? ( - - - Loading... - - - ) : ( - - - No results. - - - )} - -
-
-
-
- {`${currentPage + 1} of ${pageCount}`} -
-
- - -
-
-
- ) -} diff --git a/src/containers/RecentEdits.tsx b/src/containers/RecentEdits.tsx index 1d68faa..cdfe5e6 100644 --- a/src/containers/RecentEdits.tsx +++ b/src/containers/RecentEdits.tsx @@ -2,63 +2,77 @@ import { RecentEditsDataTable } from "@/components/RecentEditsDataTable" import { useQuery } from "@tanstack/react-query" import api from "../api/eolas-api" import { Link } from "react-router" +import { ArrowUpDown } from "lucide-react" +import { Button } from "@/components/ui/button" +import { DataTable } from "@/components/DataTable" const columns = [ - { - accessorKey: "title", - header: "Title", - cell: ({ cell, row }) => { - return ( - - - {row.original.title} - - - ) - }, - }, - { - accessorKey: "date", - header: "Date", - }, - { - accessorKey: "time", - header: "Time", - }, + { + accessorKey: "title", + header: ({ column }) => { + return ( + + ) + }, + // header: "Title", + cell: ({ cell, row }) => { + return ( + + + {row.original.title} + + + ) + }, + }, + { + accessorKey: "date", + header: "Date", + }, + { + accessorKey: "time", + header: "Time", + }, ] export default function RecentEdits() { - const { data, isLoading } = useQuery({ - queryKey: ["entries_recent"], - queryFn: () => api.get("/entries?limit=20&sort=date").then((res) => res.data), - }) + const { data, isLoading } = useQuery({ + queryKey: ["entries_recent"], + queryFn: () => api.get("/entries?limit=20&sort=date").then((res) => res.data), + }) - console.log(data) - const parsed = data?.data?.map((entry) => { - const [date, time] = entry?.last_modified?.split(" ") - return { - title: entry.title.replace(/_/g, " "), - date: new Date(date).toLocaleString("en-GB", { - day: "numeric", - month: "long", - year: "numeric", - }), - time: time, - } - }) + console.log(data) + const parsed = data?.data?.map((entry) => { + const [date, time] = entry?.last_modified?.split(" ") + return { + title: entry.title.replace(/_/g, " "), + date: new Date(date).toLocaleString("en-GB", { + day: "numeric", + month: "long", + year: "numeric", + }), + time: time, + } + }) - return ( - <> -
-
-

Recent edits

-
-
-
- -
-
-
- - ) + return ( + <> +
+
+

Recent edits

+
+
+
+ +
+
+
+ + ) } diff --git a/src/templates/Tag.tsx b/src/templates/Tag.tsx index 7b7fabd..3351f86 100644 --- a/src/templates/Tag.tsx +++ b/src/templates/Tag.tsx @@ -1,4 +1,3 @@ -import Main from "@/templates/Main" import { useParams } from "react-router" import Page from "./Page" export default function Tag() {