From c0e2bdc01366f4a8878ffb373527d786e163a19b Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sun, 31 Mar 2024 16:23:25 +0100
Subject: refactor: Extract the action confirming dialog into its own component

---
 .../components/dashboard/settings/DeleteApiKey.tsx | 70 ++++++++--------------
 1 file changed, 26 insertions(+), 44 deletions(-)

(limited to 'apps/web/components/dashboard/settings/DeleteApiKey.tsx')

diff --git a/apps/web/components/dashboard/settings/DeleteApiKey.tsx b/apps/web/components/dashboard/settings/DeleteApiKey.tsx
index 091f352c..cbbe8320 100644
--- a/apps/web/components/dashboard/settings/DeleteApiKey.tsx
+++ b/apps/web/components/dashboard/settings/DeleteApiKey.tsx
@@ -1,19 +1,9 @@
 "use client";
 
-import { useState } from "react";
 import { useRouter } from "next/navigation";
 import { ActionButton } from "@/components/ui/action-button";
+import ActionConfirmingDialog from "@/components/ui/action-confirming-dialog";
 import { Button } from "@/components/ui/button";
-import {
-  Dialog,
-  DialogClose,
-  DialogContent,
-  DialogDescription,
-  DialogFooter,
-  DialogHeader,
-  DialogTitle,
-  DialogTrigger,
-} from "@/components/ui/dialog";
 import { toast } from "@/components/ui/use-toast";
 import { api } from "@/lib/trpc";
 import { Trash } from "lucide-react";
@@ -25,49 +15,41 @@ export default function DeleteApiKey({
   name: string;
   id: string;
 }) {
-  const [isDialogOpen, setDialogOpen] = useState(false);
   const router = useRouter();
   const mutator = api.apiKeys.revoke.useMutation({
     onSuccess: () => {
       toast({
         description: "Key was successfully deleted",
       });
-      setDialogOpen(false);
       router.refresh();
     },
   });
 
   return (
-    <Dialog open={isDialogOpen} onOpenChange={setDialogOpen}>
-      <DialogTrigger asChild>
-        <Button variant="destructive">
-          <Trash className="size-5" />
-        </Button>
-      </DialogTrigger>
-      <DialogContent>
-        <DialogHeader>
-          <DialogTitle>Delete API Key</DialogTitle>
-          <DialogDescription>
-            Are you sure you want to delete the API key &quot;{name}&quot;? Any
-            service using this API key will lose access.
-          </DialogDescription>
-        </DialogHeader>
-        <DialogFooter className="sm:justify-end">
-          <DialogClose asChild>
-            <Button type="button" variant="secondary">
-              Close
-            </Button>
-          </DialogClose>
-          <ActionButton
-            type="button"
-            variant="destructive"
-            loading={mutator.isPending}
-            onClick={() => mutator.mutate({ id })}
-          >
-            Delete
-          </ActionButton>
-        </DialogFooter>
-      </DialogContent>
-    </Dialog>
+    <ActionConfirmingDialog
+      title={"Delete API Key"}
+      description={
+        <p>
+          Are you sure you want to delete the API key &quot;{name}&quot;? Any
+          service using this API key will lose access.
+        </p>
+      }
+      actionButton={(setDialogOpen) => (
+        <ActionButton
+          type="button"
+          variant="destructive"
+          loading={mutator.isPending}
+          onClick={() =>
+            mutator.mutate({ id }, { onSuccess: () => setDialogOpen(false) })
+          }
+        >
+          Delete
+        </ActionButton>
+      )}
+    >
+      <Button variant="destructive">
+        <Trash className="size-5" />
+      </Button>
+    </ActionConfirmingDialog>
   );
 }
-- 
cgit v1.2.3-70-g09d2

