partially implement multi-delete button

This commit is contained in:
andrzej 2024-06-27 16:49:56 +02:00
parent 4aa7194427
commit 1fca1a2b81
6 changed files with 24 additions and 4 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import prisma from "./db"; import prisma from "./db";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { Pathname } from "app/types";
const tableMap = { const tableMap = {
"/story": "story", "/story": "story",
@ -9,7 +10,7 @@ const tableMap = {
"/submission": "sub" "/submission": "sub"
} }
export async function deleteRecord(id: number, pathname: "/story" | "/publication" | "/submission") { export async function deleteRecord(id: number, pathname: Pathname) {
const table = tableMap[pathname] const table = tableMap[pathname]
const res = await prisma[table].delete({ where: { id } }) const res = await prisma[table].delete({ where: { id } })
console.log(`deleted from ${table}: ${res.id}`) console.log(`deleted from ${table}: ${res.id}`)

View File

@ -3,6 +3,7 @@ import { DataTable } from "app/ui/tables/data-table"
import { columns } from "./columns" import { columns } from "./columns"
import { Pub, Response, Story, Sub } from "@prisma/client" import { Pub, Response, Story, Sub } from "@prisma/client"
import CreateSubmissionDialog from "./create" import CreateSubmissionDialog from "./create"
import { Trash2 } from "lucide-react"
export type SubComplete = Sub & { export type SubComplete = Sub & {
pub: Pub, pub: Pub,

View File

@ -1055,6 +1055,10 @@ video {
align-items: baseline; align-items: baseline;
} }
.justify-start {
justify-content: flex-start;
}
.justify-end { .justify-end {
justify-content: flex-end; justify-content: flex-end;
} }

1
src/app/types.ts Normal file
View File

@ -0,0 +1 @@
export type Pathname = "/story" | "/publication" | "/submission"

View File

@ -37,9 +37,11 @@ import {
TableHeader, TableHeader,
TableRow, TableRow,
} from "@/components/ui/table" } from "@/components/ui/table"
import { EyeIcon } from "lucide-react" import { EyeIcon, Trash2 } from "lucide-react"
import { usePathname } from "next/navigation" import { usePathname } from "next/navigation"
import FormContextMenu from "./contextMenu" import FormContextMenu from "./contextMenu"
import { deleteRecords } from "app/lib/del"
import { Pathname } from "app/types"
interface DataTableProps<TData, TValue> { interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[] columns: ColumnDef<TData, TValue>[]
@ -78,10 +80,10 @@ export function DataTable<TData, TValue>({
columnVisibility, columnVisibility,
} }
}) })
const pathname = usePathname() const pathname: Pathname = usePathname()
const [filterBy, setFilterBy] = useState(table.getAllColumns()[0]) const [filterBy, setFilterBy] = useState(table.getAllColumns()[0])
return (<> return (<>
<div className="flex justify-between py-4"> <div className="flex justify-between items-center py-4">
<div className="flex gap-2"> <div className="flex gap-2">
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
@ -116,6 +118,17 @@ export function DataTable<TData, TValue>({
{children} {children}
<Button variant="destructive" disabled={!table.getIsSomeRowsSelected()}
onClick={() => {
const selectedRows = table.getState().rowSelection
const rowIds = Object.keys(selectedRows)
const recordIds = rowIds.map(id => Number(table.getRow(id).original.id))
console.table(recordIds)
deleteRecords(recordIds, pathname)
}}
>
<Trash2 />
</Button>
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>