Compare commits

...

2 Commits

Author SHA1 Message Date
andrzej 4aa7194427 enable row selection 2024-06-27 16:08:14 +02:00
andrzej f163de99c8 add multi delete function 2024-06-27 16:08:03 +02:00
3 changed files with 24 additions and 25 deletions

Binary file not shown.

View File

@ -3,34 +3,30 @@ import { revalidatePath } from "next/cache";
import prisma from "./db";
import { redirect } from "next/navigation";
const tableMap = {
"/story": "story",
"/publication": "pub",
"/submission": "sub"
}
export async function deleteRecord(id: number, pathname: "/story" | "/publication" | "/submission") {
let res = {}
switch (pathname) {
case "/story":
res = await prisma.story.delete({
where: { id }
})
console.log(`deleted: ${res}`)
break;
case "/publication":
res = await prisma.pub.delete({
where: { id }
})
console.log(`deleted: ${res}`)
break;
case "/submission":
res = await prisma.sub.delete({
where: { id }
})
console.log(`deleted: ${res}`)
break;
default:
break;
}
const table = tableMap[pathname]
const res = await prisma[table].delete({ where: { id } })
console.log(`deleted from ${table}: ${res.id}`)
console.log("revalidating: " + pathname)
revalidatePath(pathname)
redirect(pathname)
}
export async function deleteRecords(ids: number[], pathname: "/story" | "/publication" | "/submission") {
const table = tableMap[pathname]
ids.forEach(async (id) => {
const res = await prisma.story.delete({
where: { id }
})
console.log(`deleted from ${table}: ${res.id}`)
})
}

View File

@ -26,7 +26,7 @@ import {
getFilteredRowModel,
getCoreRowModel,
getPaginationRowModel,
useReactTable,
useReactTable
} from "@tanstack/react-table"
import {
@ -63,6 +63,8 @@ export function DataTable<TData, TValue>({
const table = useReactTable({
data,
columns,
enableRowSelection: true,
enableMultiRowSelection: true,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
@ -74,7 +76,7 @@ export function DataTable<TData, TValue>({
sorting,
columnFilters,
columnVisibility,
},
}
})
const pathname = usePathname()
const [filterBy, setFilterBy] = useState(table.getAllColumns()[0])
@ -174,6 +176,7 @@ export function DataTable<TData, TValue>({
key={row.id}
data-state={row.getIsSelected() && "selected"}
tabIndex={0}
onClick={() => row.toggleSelected()}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>