From 6febe13b3f4ad4eff3f205ece445b3577255bf41 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Fri, 16 Feb 2024 20:33:00 +0000
Subject: Migrating to react query for client side queries

---
 .../app/dashboard/bookmarks/components/AddLink.tsx | 26 +++++++++++++---------
 1 file changed, 15 insertions(+), 11 deletions(-)

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

diff --git a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx
index e8ecec35..34f043e7 100644
--- a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx
+++ b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx
@@ -17,21 +17,19 @@ const formSchema = z.object({
 
 export default function AddLink() {
   const router = useRouter();
+  const bookmarkLinkMutator = api.bookmarks.bookmarkLink.useMutation({
+    onSuccess: () => {
+      router.refresh();
+    },
+    onError: () => {
+      toast({ description: "Something went wrong", variant: "destructive" });
+    },
+  });
 
   const form = useForm<z.infer<typeof formSchema>>({
     resolver: zodResolver(formSchema),
   });
 
-  async function onSubmit(value: z.infer<typeof formSchema>) {
-    try {
-      await api.bookmarks.bookmarkLink.mutate({ url: value.url, type: "link" });
-    } catch (e) {
-      toast({ description: "Something went wrong", variant: "destructive" });
-      return;
-    }
-    router.refresh();
-  }
-
   const onError: SubmitErrorHandler<z.infer<typeof formSchema>> = (errors) => {
     toast({
       description: Object.values(errors)
@@ -43,7 +41,13 @@ export default function AddLink() {
 
   return (
     <Form {...form}>
-      <form onSubmit={form.handleSubmit(onSubmit, onError)}>
+      <form
+        onSubmit={form.handleSubmit(
+          (value) =>
+            bookmarkLinkMutator.mutate({ url: value.url, type: "link" }),
+          onError,
+        )}
+      >
         <div className="container flex w-full items-center space-x-2 py-4">
           <FormField
             control={form.control}
-- 
cgit v1.2.3-70-g09d2

