summaryrefslogtreecommitdiff
path: root/lib/information/table/delete-information-dialog.tsx
blob: e36d948d5afb885dbec700b68fa6794b7135c2cc (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
123
124
125
"use client"

import * as React from "react"
import { useRouter } from "next/navigation"
import { toast } from "sonner"
import { Loader, Trash2 } from "lucide-react"

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { deleteInformation } from "@/lib/information/service"
import type { PageInformation } from "@/db/schema/information"

interface DeleteInformationDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  information?: PageInformation
  onClose: () => void
}

export function DeleteInformationDialog({
  open,
  onOpenChange,
  information,
  onClose,
}: DeleteInformationDialogProps) {
  const router = useRouter()
  const [isLoading, setIsLoading] = React.useState(false)

  const handleDelete = async () => {
    if (!information) return

    setIsLoading(true)
    try {
      const result = await deleteInformation(information.id)

      if (result.success) {
        toast.success(result.message)
        onClose()
        router.refresh()
      } else {
        toast.error(result.message)
      }
    } catch (error) {
      toast.error("인포메이션 삭제에 실패했습니다.")
      console.error(error)
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle className="flex items-center gap-2">
            <Trash2 className="h-5 w-5 text-destructive" />
            인포메이션 삭제
          </AlertDialogTitle>
          <AlertDialogDescription>
            다음 인포메이션을 삭제하시겠습니까? 이 작업은 되돌릴 수 없습니다.
          </AlertDialogDescription>
        </AlertDialogHeader>

        {information && (
          <div className="bg-muted rounded-lg p-4 my-4">
            <div className="space-y-2">
              <div>
                <span className="font-medium text-sm">페이지 코드:</span>
                <span className="ml-2 font-mono text-sm">{information.pageCode}</span>
              </div>
              <div>
                <span className="font-medium text-sm">페이지명:</span>
                <span className="ml-2 text-sm">{information.pageName}</span>
              </div>
              <div>
                <span className="font-medium text-sm">제목:</span>
                <span className="ml-2 text-sm">{information.title}</span>
              </div>
              {information.noticeTitle && (
                <div>
                  <span className="font-medium text-sm">공지사항 제목:</span>
                  <span className="ml-2 text-sm">{information.noticeTitle}</span>
                </div>
              )}
              {information.noticeContent && (
                <div>
                  <span className="font-medium text-sm">공지사항 내용:</span>
                  <span className="ml-2 text-sm text-orange-600">{information.noticeContent}</span>
                </div>
              )}
              {information.attachmentFileName && (
                <div>
                  <span className="font-medium text-sm">첨부파일:</span>
                  <span className="ml-2 text-sm">{information.attachmentFileName}</span>
                </div>
              )}
            </div>
          </div>
        )}

        <AlertDialogFooter>
          <AlertDialogCancel onClick={onClose} disabled={isLoading}>
            취소
          </AlertDialogCancel>
          <AlertDialogAction
            onClick={handleDelete}
            disabled={isLoading}
            className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
          >
            {isLoading && <Loader className="mr-2 h-4 w-4 animate-spin" />}
            삭제
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}