summaryrefslogtreecommitdiff
path: root/lib/basic-contract/template/dispose-documents-dialog.tsx
blob: 1154c246afc2e81063c47064724ebe32e3a364e3 (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 { toast } from "sonner"
import { Trash2, RotateCcw } from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import { BasicContractTemplate } from "@/db/schema"
import { disposeDocuments, restoreDocuments } from "../actions"

interface DisposeDocumentsDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  documents: BasicContractTemplate[]
  showTrigger?: boolean
  onSuccess?: () => void
}

export function DisposeDocumentsDialog({
  open,
  onOpenChange,
  documents,
  showTrigger = true,
  onSuccess,
}: DisposeDocumentsDialogProps) {
  const [isLoading, setIsLoading] = React.useState(false)

  const isDisposeAction = documents.some(doc => doc.status === 'ACTIVE')
  const actionText = isDisposeAction ? '폐기' : '복구'
  const actionIcon = isDisposeAction ? Trash2 : RotateCcw

  const handleAction = async () => {
    if (documents.length === 0) return

    setIsLoading(true)
    try {
      const documentIds = documents.map(doc => doc.id)
      
      if (isDisposeAction) {
        await disposeDocuments(documentIds)
        toast.success(`${documents.length}개의 문서가 폐기되었습니다.`)
      } else {
        await restoreDocuments(documentIds)
        toast.success(`${documents.length}개의 문서가 복구되었습니다.`)
      }

      onSuccess?.()
      onOpenChange(false)
    } catch (error) {
      console.error(`${actionText} 처리 오류:`, error)
      toast.error(`${actionText} 처리 중 오류가 발생했습니다.`)
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      {showTrigger && (
        <Button variant="outline" size="sm">
          {React.createElement(actionIcon, { className: "mr-2 h-4 w-4" })}
          {actionText}하기
        </Button>
      )}
      <DialogContent className="sm:max-w-[425px]">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            {React.createElement(actionIcon, { className: "h-5 w-5" })}
            문서 {actionText}
          </DialogTitle>
          <DialogDescription>
            선택한 {documents.length}개의 문서를 {actionText}하시겠습니까?
            {isDisposeAction 
              ? ' 폐기된 문서는 복구할 수 있습니다.' 
              : ' 복구된 문서는 다시 사용할 수 있습니다.'
            }
          </DialogDescription>
        </DialogHeader>
        
        <div className="py-4">
          <div className="space-y-2">
            {documents.map((doc) => (
              <div key={doc.id} className="flex items-center p-2 bg-muted rounded">
                <div>
                  <p className="font-medium">{doc.templateName}</p>
                  <p className="text-sm text-muted-foreground">
                    {doc.fileName} • v{doc.revision}
                  </p>
                </div>
              </div>
            ))}
          </div>
        </div>

        <DialogFooter>
          <Button
            variant="outline"
            onClick={() => onOpenChange(false)}
            disabled={isLoading}
          >
            취소
          </Button>
          <Button
            onClick={handleAction}
            disabled={isLoading}
            variant={isDisposeAction ? "destructive" : "default"}
          >
            {isLoading ? "처리 중..." : `${actionText}하기`}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}