From 2c2d05fd0a2c3c26d765f8a6beb88d907a097c1d Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sun, 11 Feb 2024 14:54:52 +0000
Subject: refactor: Migrating to trpc instead of next's route handers

---
 .../bookmarks/components/BookmarkOptions.tsx       | 40 +++++++++++++---------
 1 file changed, 24 insertions(+), 16 deletions(-)

(limited to 'packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx')

diff --git a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
index 15ce64c7..4496d820 100644
--- a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
+++ b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
@@ -1,7 +1,7 @@
 "use client";
 
 import { useToast } from "@/components/ui/use-toast";
-import APIClient from "@/lib/api";
+import { api } from "@/lib/trpc";
 import { ZBookmark, ZUpdateBookmarksRequest } from "@/lib/types/api/bookmarks";
 import { useRouter } from "next/navigation";
 import { Button } from "@/components/ui/button";
@@ -19,36 +19,37 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
   const linkId = bookmark.id;
 
   const unbookmarkLink = async () => {
-    const [_, error] = await APIClient.deleteBookmark(linkId);
+    try {
+      await api.bookmarks.deleteBookmark.mutate({
+        bookmarkId: linkId,
+      });
 
-    if (error) {
+      toast({
+        description: "The bookmark has been deleted!",
+      });
+    } catch (e) {
       toast({
         variant: "destructive",
         title: "Something went wrong",
         description: "There was a problem with your request.",
       });
-    } else {
-      toast({
-        description: "The bookmark has been deleted!",
-      });
     }
 
     router.refresh();
   };
 
   const updateBookmark = async (req: ZUpdateBookmarksRequest) => {
-    const [_, error] = await APIClient.updateBookmark(linkId, req);
-
-    if (error) {
+    try {
+      await api.bookmarks.updateBookmark.mutate(req);
+      toast({
+        description: "The bookmark has been updated!",
+      });
+    } catch (e) {
       toast({
         variant: "destructive",
         title: "Something went wrong",
         description: "There was a problem with your request.",
       });
-    } else {
-      toast({
-        description: "The bookmark has been updated!",
-      });
     }
 
     router.refresh();
@@ -63,13 +64,20 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
       </DropdownMenuTrigger>
       <DropdownMenuContent className="w-fit">
         <DropdownMenuItem
-          onClick={() => updateBookmark({ favourited: !bookmark.favourited })}
+          onClick={() =>
+            updateBookmark({
+              bookmarkId: linkId,
+              favourited: !bookmark.favourited,
+            })
+          }
         >
           <Star className="mr-2 size-4" />
           <span>{bookmark.favourited ? "Un-favourite" : "Favourite"}</span>
         </DropdownMenuItem>
         <DropdownMenuItem
-          onClick={() => updateBookmark({ archived: !bookmark.archived })}
+          onClick={() =>
+            updateBookmark({ bookmarkId: linkId, archived: !bookmark.archived })
+          }
         >
           <Archive className="mr-2 size-4" />
           <span>{bookmark.archived ? "Un-archive" : "Archive"}</span>
-- 
cgit v1.2.3-70-g09d2

