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/settings/components/AddApiKey.tsx    | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

(limited to 'packages/web/app/dashboard/settings/components/AddApiKey.tsx')

diff --git a/packages/web/app/dashboard/settings/components/AddApiKey.tsx b/packages/web/app/dashboard/settings/components/AddApiKey.tsx
index f4f2894c..c438f4b1 100644
--- a/packages/web/app/dashboard/settings/components/AddApiKey.tsx
+++ b/packages/web/app/dashboard/settings/components/AddApiKey.tsx
@@ -53,20 +53,22 @@ function AddApiKeyForm({ onSuccess }: { onSuccess: (key: string) => void }) {
     name: z.string(),
   });
   const router = useRouter();
+  const mutator = api.apiKeys.create.useMutation({
+    onSuccess: (resp) => {
+      onSuccess(resp.key);
+      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 {
-      const resp = await api.apiKeys.create.mutate({ name: value.name });
-      onSuccess(resp.key);
-    } catch (e) {
-      toast({ description: "Something went wrong", variant: "destructive" });
-      return;
-    }
-    router.refresh();
+    mutator.mutate({ name: value.name });
   }
 
   const onError: SubmitErrorHandler<z.infer<typeof formSchema>> = (errors) => {
-- 
cgit v1.2.3-70-g09d2

