summaryrefslogtreecommitdiff
path: root/components/pq-input/pq-delete-dialog.tsx
blob: 2cb7c1d826a9a327ab40eeee10987bde7e228cd1 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { useToast } from "@/hooks/use-toast"
import { deletePQSubmissionAction } from "@/lib/pq/service"
import { useRouter } from "next/navigation"

interface PQDeleteDialogProps {
  pqSubmissionId: number
  status: string
  children: React.ReactNode
}

export function PQDeleteDialog({
  pqSubmissionId,
  status,
  children
}: PQDeleteDialogProps) {
  const [open, setOpen] = React.useState(false)
  const [isDeleting, setIsDeleting] = React.useState(false)
  const { toast } = useToast()
  const router = useRouter()

  // REQUESTED 상태가 아니면 삭제 버튼 비활성화
  const canDelete = status === "REQUESTED"

  const handleDelete = async () => {
    if (!canDelete) {
      toast({
        title: "삭제 불가",
        description: "요청됨 상태가 아닌 PQ는 삭제할 수 없습니다.",
        variant: "destructive",
      })
      return
    }

    try {
      setIsDeleting(true)
      const result = await deletePQSubmissionAction(pqSubmissionId)

      if (result.success) {
        toast({
          title: "삭제 완료",
          description: "PQ가 성공적으로 삭제되었습니다.",
        })
        setOpen(false)
        router.refresh()
      } else {
        toast({
          title: "삭제 실패",
          description: result.error || "PQ 삭제 중 오류가 발생했습니다.",
          variant: "destructive",
        })
      }
    } catch (error) {
      console.error("PQ 삭제 오류:", error)
      toast({
        title: "삭제 실패",
        description: "PQ 삭제 중 오류가 발생했습니다.",
        variant: "destructive",
      })
    } finally {
      setIsDeleting(false)
    }
  }

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <div>
          {children}
        </div>
      </DialogTrigger>
      <DialogContent className="sm:max-w-[425px]">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            PQ 삭제
          </DialogTitle>
          <DialogDescription>
            다음 PQ를 삭제하시겠습니까? <br />
            협력업체가 입력한 답변이 모두 삭제됩니다. 이 작업은 되돌릴 수 없습니다.
          </DialogDescription>
        </DialogHeader>

          {!canDelete && (
            <div className="rounded-lg bg-amber-50 border border-amber-200 p-3">
              <p className="text-sm text-amber-800">
                요청됨 상태가 아닌 PQ는 삭제할 수 없습니다.
              </p>
            </div>
          )}

        <DialogFooter>
          <Button
            variant="outline"
            onClick={() => setOpen(false)}
            disabled={isDeleting}
          >
            취소
          </Button>
          <Button
            variant="destructive"
            onClick={handleDelete}
            disabled={!canDelete || isDeleting}
          >
            {isDeleting ? "삭제 중..." : "삭제"}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}