Q58Connect/src/components/clients/delete-client.tsx

89 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useToast } from "@/hooks/use-toast";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
interface DeleteClientButtonProps {
clientId: string;
clientName: string;
}
export function DeleteClientButton({
clientId,
clientName,
}: DeleteClientButtonProps) {
const [isLoading, setIsLoading] = useState(false);
const { toast } = useToast();
const router = useRouter();
async function onDelete() {
setIsLoading(true);
try {
const response = await fetch(`/api/clients/${clientId}`, {
method: "DELETE",
});
if (!response.ok) {
throw new Error("删除失败");
}
router.refresh();
toast({
title: "删除成功",
description: "应用已成功删除",
});
} catch (error) {
toast({
variant: "destructive",
title: "删除失败",
description: error instanceof Error ? error.message : "未知错误",
});
} finally {
setIsLoading(false);
}
}
return (
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive" size="sm" disabled={isLoading}>
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle></AlertDialogTitle>
<AlertDialogDescription>
&quot;{clientName}
&quot;
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isLoading}></AlertDialogCancel>
<AlertDialogAction
onClick={onDelete}
disabled={isLoading}
className="bg-red-600 hover:bg-red-700"
>
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}