summaryrefslogtreecommitdiff
path: root/lib/tech-vendors/contacts-table/delete-contact-dialog.tsx
blob: 6c8b14d786251bff1d903aa9bd6424a45a14e4fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use client"

import * as React from "react"
import { toast } from "sonner"
import { Trash2 } from "lucide-react"

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

import { deleteTechVendorContact } from "../service"
import type { TechVendorContact } from "@/db/schema/techVendors"

interface DeleteContactDialogProps {
  contact: TechVendorContact
  vendorId: number
  onSuccess?: () => void
}

export function DeleteContactDialog({ contact, vendorId, onSuccess }: DeleteContactDialogProps) {
  const [isDeleting, setIsDeleting] = React.useState(false)

  const handleDelete = async () => {
    setIsDeleting(true)
    try {
      const result = await deleteTechVendorContact(contact.id, vendorId)

      if (result.success) {
        toast.success("담당자가 성공적으로 삭제되었습니다.")
        onSuccess?.()
      } else {
        toast.error(result.error || "담당자 삭제 중 오류가 발생했습니다.")
      }
    } catch (error) {
      console.error("담당자 삭제 오류:", error)
      toast.error("담당자 삭제 중 오류가 발생했습니다.")
    } finally {
      setIsDeleting(false)
    }
  }

  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button variant="ghost" size="sm" className="text-red-600 hover:text-red-700 hover:bg-red-50">
          <Trash2 className="size-4 mr-2" />
          삭제
        </Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>담당자 삭제</AlertDialogTitle>
          <AlertDialogDescription>
            <strong>{contact.contactName}</strong> 담당자를 정말 삭제하시겠습니까?
            <br />
            이 작업은 되돌릴 수 없으며, 관련된 모든 데이터(아이템 매핑 등)가 함께 삭제됩니다.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>취소</AlertDialogCancel>
          <AlertDialogAction
            onClick={handleDelete}
            disabled={isDeleting}
            className="bg-red-600 hover:bg-red-700"
          >
            {isDeleting ? "삭제 중..." : "삭제"}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}