From 3745443c2b27afb833be7bcc2f3b4f486a42a571 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 7 Feb 2024 18:12:53 +0000 Subject: [feature] Add support for deleting links --- web/app/api/v1/links/[linkId]/route.ts | 32 +++++++++++++++++++++++++++ web/app/bookmarks/components/LinkCard.tsx | 36 +++++++++++++++++++++++++------ web/app/layout.tsx | 6 +++++- 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 web/app/api/v1/links/[linkId]/route.ts (limited to 'web/app') diff --git a/web/app/api/v1/links/[linkId]/route.ts b/web/app/api/v1/links/[linkId]/route.ts new file mode 100644 index 00000000..39449d6d --- /dev/null +++ b/web/app/api/v1/links/[linkId]/route.ts @@ -0,0 +1,32 @@ +import { authOptions } from "@/lib/auth"; +import { unbookmarkLink } from "@/lib/services/links"; +import { Prisma } from "@remember/db"; + +import { getServerSession } from "next-auth"; +import { NextRequest } from "next/server"; + +export async function DELETE( + _request: NextRequest, + { params }: { params: { linkId: string } }, +) { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + try { + await unbookmarkLink(params.linkId, session.user.id); + } catch (e: unknown) { + if ( + e instanceof Prisma.PrismaClientKnownRequestError && + e.code === "P2025" // RecordNotFound + ) { + return new Response(null, { status: 404 }); + } else { + throw e; + } + } + + return new Response(null, { status: 201 }); +} diff --git a/web/app/bookmarks/components/LinkCard.tsx b/web/app/bookmarks/components/LinkCard.tsx index 75973f7e..907acd19 100644 --- a/web/app/bookmarks/components/LinkCard.tsx +++ b/web/app/bookmarks/components/LinkCard.tsx @@ -13,12 +13,34 @@ import { ImageCardFooter, ImageCardTitle, } from "@/components/ui/imageCard"; +import { useToast } from "@/components/ui/use-toast"; +import APIClient from "@/lib/api"; import { ZBookmarkedLink } from "@/lib/types/api/links"; import { MoreHorizontal, Trash2 } from "lucide-react"; import Link from "next/link"; +import { useRouter } from "next/navigation"; -export function LinkOptions() { - // TODO: Implement deletion +export function LinkOptions({ linkId }: { linkId: string }) { + const { toast } = useToast(); + const router = useRouter(); + + const unbookmarkLink = async () => { + let [_, error] = await APIClient.unbookmarkLink(linkId); + + if (error) { + toast({ + variant: "destructive", + title: "Something went wrong", + description: "There was a problem with your request.", + }); + } else { + toast({ + description: "The link has been deleted!", + }); + } + + router.refresh(); + }; return ( @@ -27,10 +49,10 @@ export function LinkOptions() { - - - Delete - + + + Delete + ); @@ -59,7 +81,7 @@ export default function LinkCard({ link }: { link: ZBookmarkedLink }) { {parsedUrl.host} - + diff --git a/web/app/layout.tsx b/web/app/layout.tsx index 30d918df..a6543b1c 100644 --- a/web/app/layout.tsx +++ b/web/app/layout.tsx @@ -2,6 +2,7 @@ import type { Metadata } from "next"; import { Inter } from "next/font/google"; import "./globals.css"; import React from "react"; +import { Toaster } from "@/components/ui/toaster"; const inter = Inter({ subsets: ["latin"] }); @@ -17,7 +18,10 @@ export default function RootLayout({ }>) { return ( - {children} + + {children} + + ); } -- cgit v1.2.3-70-g09d2