From 7ff7f673e7911791600c9e850908fe44a4a0c206 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Fri, 11 Aug 2023 16:01:18 +0800 Subject: [PATCH] New feature: Delete bookmark --- app/api/bookmark/route.ts | 11 +++++++++++ components/bookmarkCard.tsx | 23 +++++++++++++++++++++-- types/index.ts | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/api/bookmark/route.ts b/app/api/bookmark/route.ts index a614468..48f5b47 100644 --- a/app/api/bookmark/route.ts +++ b/app/api/bookmark/route.ts @@ -1,6 +1,7 @@ import { bookmarkSchema } from '@/types' import { NextRequest, NextResponse } from 'next/server' import { db, bookmark } from '@/lib/schema' +import { eq } from 'drizzle-orm' export const GET = async () => { const bookmarks = await db.select().from(bookmark) @@ -21,3 +22,13 @@ export const POST = async (request: NextRequest) => { return NextResponse.json(newBookmark.error) } + +export const DELETE = async (request: NextRequest) => { + const body = await request.json() + + const bookmarkId = body.bookmarkId + + await db.delete(bookmark).where(eq(bookmark.id, bookmarkId)) + + return NextResponse.json({ message: 'Bookmark Deleted' }) +} diff --git a/components/bookmarkCard.tsx b/components/bookmarkCard.tsx index 6d8b9f3..145012f 100644 --- a/components/bookmarkCard.tsx +++ b/components/bookmarkCard.tsx @@ -4,16 +4,27 @@ import { Card, CardHeader, CardBody, CardFooter } from '@nextui-org/card' import { Link } from '@nextui-org/link' import { Divider } from '@nextui-org/divider' import { Image } from '@nextui-org/image' +import { Icon } from '@iconify/react' +import { useRouter } from 'next/navigation' import { type Bookmark } from '@/types' export const BookmarkCard = ({ data }: { data: Bookmark }) => { + const router = useRouter() + const deleteBookmark = async (event: React.MouseEvent, id: Number) => { + event.stopPropagation() + await fetch('/api/bookmark', { + method: 'DELETE', + body: JSON.stringify({ bookmarkId: id }), + }) + router.refresh() + } return ( <> open(data.url, '_blank')}> + onPress={() => {}}> nextui logo { src='https://avatars.githubusercontent.com/u/86160567?s=200&v=4' width={40} /> -
+

{data.name}

{data.link}

+
+ deleteBookmark(event, data.id)} + /> +
diff --git a/types/index.ts b/types/index.ts index 6cd7607..5d98270 100644 --- a/types/index.ts +++ b/types/index.ts @@ -6,6 +6,7 @@ export type IconSvgProps = SVGProps & { } export const bookmarkSchema = z.object({ + id: z.number(), name: z.string(), link: z.string(), description: z.string(),