Add delete

This commit is contained in:
Owen Schwartz 2024-10-19 22:39:46 -04:00
parent 1c9d90bca4
commit 25b44ca3ba
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
3 changed files with 37 additions and 3 deletions

View file

@ -12,6 +12,7 @@ import { Button } from "@app/components/ui/button";
import { ArrowUpDown, MoreHorizontal } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import api from "@app/api";
export type ResourceRow = {
id: string;
@ -61,8 +62,20 @@ export const columns: ColumnDef<ResourceRow>[] = [
{
id: "actions",
cell: ({ row }) => {
const router = useRouter();
const resourceRow = row.original;
const deleteResource = (resourceId: string) => {
api.delete(`/resource/${resourceId}`)
.catch((e) => {
console.error("Error deleting resource", e);
})
.then(() => {
router.refresh();
});
}
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
@ -79,6 +92,9 @@ export const columns: ColumnDef<ResourceRow>[] = [
View settings
</Link>
</DropdownMenuItem>
<DropdownMenuItem>
<button onClick={() => deleteResource(resourceRow.id)} className="text-red-600 hover:text-red-800 hover:underline cursor-pointer">Delete</button>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);

View file

@ -12,9 +12,11 @@ import { Button } from "@app/components/ui/button";
import { ArrowUpDown, MoreHorizontal } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import api from "@app/api";
export type SiteRow = {
id: string;
id: number;
nice: string;
name: string;
mbIn: number;
mbOut: number;
@ -39,7 +41,7 @@ export const columns: ColumnDef<SiteRow>[] = [
},
},
{
accessorKey: "id",
accessorKey: "nice",
header: ({ column }) => {
return (
<Button
@ -65,8 +67,20 @@ export const columns: ColumnDef<SiteRow>[] = [
{
id: "actions",
cell: ({ row }) => {
const router = useRouter();
const siteRow = row.original;
const deleteSite = (siteId: number) => {
api.delete(`/site/${siteId}`)
.catch((e) => {
console.error("Error deleting site", e);
})
.then(() => {
router.refresh();
});
}
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
@ -83,6 +97,9 @@ export const columns: ColumnDef<SiteRow>[] = [
View settings
</Link>
</DropdownMenuItem>
<DropdownMenuItem>
<button onClick={() => deleteSite(siteRow.id)} className="text-red-600 hover:text-red-800 hover:underline cursor-pointer">Delete</button>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);

View file

@ -23,7 +23,8 @@ export default async function Page({ params }: SitesPageProps) {
const siteRows: SiteRow[] = sites.map((site) => {
return {
name: site.name,
id: site.niceId.toString(),
id: site.siteId,
nice: site.niceId.toString(),
mbIn: site.megabytesIn || 0,
mbOut: site.megabytesOut || 0,
orgId: params.orgId,