-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
314 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,309 @@ | ||
"use client" | ||
|
||
import { | ||
ColumnDef, | ||
ColumnFiltersState, | ||
SortingState, | ||
VisibilityState, | ||
flexRender, | ||
getCoreRowModel, | ||
getFilteredRowModel, | ||
getPaginationRowModel, | ||
getSortedRowModel, | ||
useReactTable, | ||
} from "@tanstack/react-table" | ||
import { useState } from "react" | ||
import { ArrowUpDown, MoreHorizontal } from "lucide-react" | ||
import { format } from "date-fns" | ||
|
||
import { | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table" | ||
import { Event } from "@/models/event" | ||
import { Button } from "@/components/ui/button" | ||
import { | ||
DropdownMenu, | ||
DropdownMenuCheckboxItem, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuLabel, | ||
DropdownMenuSeparator, | ||
DropdownMenuTrigger, | ||
} from "@/components/ui/dropdown-menu" | ||
|
||
import { Input } from "../ui/input" | ||
import { Checkbox } from "../ui/checkbox" | ||
|
||
const columns: ColumnDef<Event>[] = [ | ||
{ | ||
id: "select", | ||
header: ({ table }) => ( | ||
<Checkbox | ||
checked={ | ||
table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && "indeterminate") | ||
} | ||
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)} | ||
aria-label="Select all" | ||
/> | ||
), | ||
cell: ({ row }) => ( | ||
<Checkbox | ||
checked={row.getIsSelected()} | ||
onCheckedChange={(value) => row.toggleSelected(!!value)} | ||
aria-label="Select row" | ||
/> | ||
), | ||
}, | ||
{ | ||
accessorKey: "title", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
タイトル | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
) | ||
}, | ||
}, | ||
{ | ||
accessorKey: "description", | ||
header: "説明", | ||
}, | ||
{ | ||
accessorKey: "thumbnailUrl", | ||
header: "サムネイル", | ||
}, | ||
{ | ||
accessorKey: "color", | ||
header: "テーマカラー", | ||
}, | ||
{ | ||
accessorKey: "startAt", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
開始日時 | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
) | ||
}, | ||
cell: ({ row }) => { | ||
const date = format(new Date(row.original.startAt.seconds), "yyyy-MM-dd HH:mm:ss") | ||
return <span>{date}</span> | ||
}, | ||
}, | ||
{ | ||
accessorKey: "endAt", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
終了日時 | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
) | ||
}, | ||
}, | ||
{ | ||
accessorKey: "externalLink", | ||
header: "外部リンク", | ||
}, | ||
{ | ||
accessorKey: "isPublished", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
公開 | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
) | ||
}, | ||
}, | ||
{ | ||
accessorKey: "createdBy", | ||
header: "作成者", | ||
}, | ||
{ | ||
accessorKey: "participantIds", | ||
header: "参加者", | ||
}, | ||
{ | ||
accessorKey: "createdAt", | ||
header: "作成日時", | ||
}, | ||
{ | ||
accessorKey: "updatedAt", | ||
header: "更新日時", | ||
}, | ||
{ | ||
id: "actions", | ||
cell: ({ row }) => { | ||
const payment = row.original | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" className="h-8 w-8 p-0"> | ||
<span className="sr-only">Open menu</span> | ||
<MoreHorizontal className="h-4 w-4" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
<DropdownMenuLabel>Actions</DropdownMenuLabel> | ||
<DropdownMenuItem onClick={() => navigator.clipboard.writeText(payment.id)}> | ||
Copy payment ID | ||
</DropdownMenuItem> | ||
<DropdownMenuSeparator /> | ||
<DropdownMenuItem>View customer</DropdownMenuItem> | ||
<DropdownMenuItem>View payment details</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
) | ||
}, | ||
}, | ||
] | ||
|
||
interface EventDataTableProps { | ||
data: Event[] | ||
} | ||
|
||
export function EventDataTable({ data }: EventDataTableProps) { | ||
const [sorting, setSorting] = useState<SortingState>([]) | ||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]) | ||
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({}) | ||
const [rowSelection, setRowSelection] = useState({}) | ||
|
||
const table = useReactTable({ | ||
data, | ||
columns, | ||
getCoreRowModel: getCoreRowModel(), | ||
getPaginationRowModel: getPaginationRowModel(), | ||
onSortingChange: setSorting, | ||
getSortedRowModel: getSortedRowModel(), | ||
onColumnFiltersChange: setColumnFilters, | ||
getFilteredRowModel: getFilteredRowModel(), | ||
onColumnVisibilityChange: setColumnVisibility, | ||
onRowSelectionChange: setRowSelection, | ||
state: { | ||
sorting, | ||
columnFilters, | ||
columnVisibility, | ||
rowSelection, | ||
}, | ||
}) | ||
|
||
return ( | ||
<div> | ||
<div className="flex items-center py-4"> | ||
<Input | ||
placeholder="Filter emails..." | ||
value={(table.getColumn("email")?.getFilterValue() as string) ?? ""} | ||
onChange={(event) => table.getColumn("email")?.setFilterValue(event.target.value)} | ||
className="max-w-sm" | ||
/> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="outline" className="ml-auto"> | ||
Columns | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
{table | ||
.getAllColumns() | ||
.filter((column) => column.getCanHide()) | ||
.map((column) => { | ||
return ( | ||
<DropdownMenuCheckboxItem | ||
key={column.id} | ||
className="capitalize" | ||
checked={column.getIsVisible()} | ||
onCheckedChange={(value) => column.toggleVisibility(!!value)} | ||
> | ||
{column.id} | ||
</DropdownMenuCheckboxItem> | ||
) | ||
})} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
|
||
<div className="flex-1 text-sm text-muted-foreground"> | ||
{table.getFilteredSelectedRowModel().rows.length} of{" "} | ||
{table.getFilteredRowModel().rows.length} row(s) selected. | ||
</div> | ||
|
||
<div className="rounded-md border"> | ||
<Table> | ||
<TableHeader> | ||
{table.getHeaderGroups().map((headerGroup) => ( | ||
<TableRow key={headerGroup.id}> | ||
{headerGroup.headers.map((header) => { | ||
return ( | ||
<TableHead key={header.id}> | ||
{header.isPlaceholder | ||
? null | ||
: flexRender(header.column.columnDef.header, header.getContext())} | ||
</TableHead> | ||
) | ||
})} | ||
</TableRow> | ||
))} | ||
</TableHeader> | ||
<TableBody> | ||
{table.getRowModel().rows?.length ? ( | ||
table.getRowModel().rows.map((row) => ( | ||
<TableRow key={row.id} data-state={row.getIsSelected() && "selected"}> | ||
{row.getVisibleCells().map((cell) => ( | ||
<TableCell key={cell.id}> | ||
{flexRender(cell.column.columnDef.cell, cell.getContext())} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
)) | ||
) : ( | ||
<TableRow> | ||
<TableCell colSpan={columns.length} className="h-24 text-center"> | ||
No results. | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
<div className="flex items-center justify-end space-x-2 py-4"> | ||
<Button | ||
variant="outline" | ||
size="sm" | ||
onClick={() => table.previousPage()} | ||
disabled={!table.getCanPreviousPage()} | ||
> | ||
Previous | ||
</Button> | ||
<Button | ||
variant="outline" | ||
size="sm" | ||
onClick={() => table.nextPage()} | ||
disabled={!table.getCanNextPage()} | ||
> | ||
Next | ||
</Button> | ||
</div> | ||
</div> | ||
) | ||
} |