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 prisma from "./db";
import { redirect } from "next/navigation";
import { Pathname } from "app/types";
const tableMap = {
"/story": "story",
@ -9,7 +10,7 @@ const tableMap = {
"/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 res = await prisma[table].delete({ where: { 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 { Pub, Response, Story, Sub } from "@prisma/client"
import CreateSubmissionDialog from "./create"
import { Trash2 } from "lucide-react"
export type SubComplete = Sub & {
pub: Pub,

View File

@ -1055,6 +1055,10 @@ video {
align-items: baseline;
}
.justify-start {
justify-content: flex-start;
}
.justify-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,
TableRow,
} from "@/components/ui/table"
import { EyeIcon } from "lucide-react"
import { EyeIcon, Trash2 } from "lucide-react"
import { usePathname } from "next/navigation"
import FormContextMenu from "./contextMenu"
import { deleteRecords } from "app/lib/del"
import { Pathname } from "app/types"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
@ -78,10 +80,10 @@ export function DataTable<TData, TValue>({
columnVisibility,
}
})
const pathname = usePathname()
const pathname: Pathname = usePathname()
const [filterBy, setFilterBy] = useState(table.getAllColumns()[0])
return (<>
<div className="flex justify-between py-4">
<div className="flex justify-between items-center py-4">
<div className="flex gap-2">
<DropdownMenu>
<DropdownMenuTrigger asChild>
@ -116,6 +118,17 @@ export function DataTable<TData, TValue>({
{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>
<DropdownMenuTrigger asChild>