Skip to content

Commit

Permalink
Merge pull request #417 from VK-RED/refactor/ApiKeyAlertDialog
Browse files Browse the repository at this point in the history
refactor: added a reusable ConfirmDialog component and dropped ApiKeyAlertDialog
  • Loading branch information
dahal committed Jul 7, 2024
2 parents eb1385b + 3e2fd9b commit e9b6d53
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import { dayjsExt } from "@/common/dayjs";
import Tldr from "@/components/common/tldr";
import { Card } from "@/components/ui/card";
import { api } from "@/trpc/react";
import { RiMore2Fill } from "@remixicon/react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";

import { ApiKeyAlertDialog } from "@/components/apiKey/apiKey-alert-dialog";
import { ConfirmDialog } from "@/components/common/confirmDialog";
import {
DropdownMenu,
DropdownMenuContent,
Expand All @@ -30,6 +33,28 @@ interface ApiKey {
}

const ApiKeysTable = ({ keys }: { keys: ApiKey[] }) => {
const router = useRouter();

const deleteMutation = api.apiKey.delete.useMutation({
onSuccess: ({ message }) => {
toast.success(message);
},

onError: (error) => {
console.error(error);
toast.error("An error occurred while creating the API key.");
},

onSettled: () => {
router.refresh();
},
});

const dialogTitle = "Are you sure?";
const dialogBody =
"Are you sure you want to delete this key? This action cannot be undone and you will loose the access if this API key is currently being used.";
const dialogTrigger = <div className="text-rose-600">Delete key</div>;

return (
<Card className="mx-auto mt-3 w-[28rem] sm:w-[38rem] md:w-full">
<div className="mx-3">
Expand Down Expand Up @@ -78,7 +103,15 @@ const ApiKeysTable = ({ keys }: { keys: ApiKey[] }) => {
Rotate key
</DropdownMenuItem>

<ApiKeyAlertDialog keyId={key.keyId} key={key.keyId} />
<ConfirmDialog
key={key.keyId}
title={dialogTitle}
body={dialogBody}
trigger={dialogTrigger}
onConfirm={() => {
deleteMutation.mutate({ keyId: key.keyId });
}}
/>
</DropdownMenuContent>
</DropdownMenu>
</div>
Expand Down
73 changes: 0 additions & 73 deletions src/components/apiKey/apiKey-alert-dialog.tsx

This file was deleted.

52 changes: 52 additions & 0 deletions src/components/common/confirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";

import type React from "react";

interface ConfirmDialogProps {
title: string;
body: string;
trigger: React.ReactNode;
onConfirm: () => void;
onCancel?: () => void;
}

export function ConfirmDialog({
title,
body,
trigger,
onConfirm,
onCancel,
}: ConfirmDialogProps) {
return (
<AlertDialog>
<AlertDialogTrigger
className="relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 hover:bg-zinc-100"
asChild={true}
>
{trigger}
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{title}</AlertDialogTitle>
<AlertDialogDescription>{body}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={onCancel}>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={onConfirm}>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}

0 comments on commit e9b6d53

Please sign in to comment.