Skip to content

Commit

Permalink
fix: event model date to timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-3in committed Apr 19, 2024
1 parent 1e41c59 commit 3430374
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/CreateEventForm/CreateEventForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getDownloadURL, ref, uploadBytes } from "firebase/storage"
import { zodResolver } from "@hookform/resolvers/zod"
import { useCallback } from "react"
import { CalendarIcon } from "lucide-react"
import { addDoc, collection } from "firebase/firestore"
import { addDoc, collection, serverTimestamp } from "firebase/firestore"
import { toast } from "sonner"
import { v4 as uuidv4 } from "uuid"
import { useRouter } from "next/navigation"
Expand Down Expand Up @@ -71,6 +71,8 @@ export const CreateEventForm = () => {

const eventsCollection = collection(devDb, "events")

const serverTimeStamp = serverTimestamp()

const promise = addDoc(eventsCollection, {
title: data.title,
description: data.description ?? null,
Expand All @@ -81,6 +83,8 @@ export const CreateEventForm = () => {
externalLink: data.externalLink ?? null,
isPublished: data.isPublished,
// createdBy: null, // TODO:
createdAt: serverTimeStamp,
updatedAt: serverTimeStamp,
})

toast.promise(promise, {
Expand Down
309 changes: 309 additions & 0 deletions src/components/EventDataTable/EventDataTable.tsx
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"

Check failure on line 14 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Cannot find module '@tanstack/react-table' or its corresponding type declarations.
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"

Check failure on line 26 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Cannot find module '@/components/ui/table' or its corresponding type declarations.
import { Event } from "@/models/event"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

Check failure on line 37 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Cannot find module '@/components/ui/dropdown-menu' or its corresponding type declarations.

import { Input } from "../ui/input"
import { Checkbox } from "../ui/checkbox"

Check failure on line 40 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Cannot find module '../ui/checkbox' or its corresponding type declarations.

const columns: ColumnDef<Event>[] = [
{
id: "select",
header: ({ table }) => (

Check failure on line 45 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Binding element 'table' implicitly has an 'any' type.
<Checkbox
checked={
table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}

Check failure on line 50 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Parameter 'value' implicitly has an 'any' type.
aria-label="Select all"
/>
),
cell: ({ row }) => (

Check failure on line 54 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Binding element 'row' implicitly has an 'any' type.
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}

Check failure on line 57 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Parameter 'value' implicitly has an 'any' type.
aria-label="Select row"
/>
),
},
{
accessorKey: "title",
header: ({ column }) => {

Check failure on line 64 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Binding element 'column' implicitly has an 'any' type.
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 }) => {

Check failure on line 90 in src/components/EventDataTable/EventDataTable.tsx

View workflow job for this annotation

GitHub Actions / build (18)

Binding element 'column' implicitly has an 'any' type.
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>
)
}

0 comments on commit 3430374

Please sign in to comment.