summaryrefslogtreecommitdiff
path: root/lib/tech-vendors/contacts-table/contact-table-toolbar-actions.tsx
blob: 84228a54375b4182cb877cbb6b2e35758c86d4c1 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
import { Download, Upload } from "lucide-react"
import { toast } from "sonner"
import ExcelJS from "exceljs"

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import { TechVendorContact } from "@/db/schema/techVendors"
import { AddContactDialog } from "./add-contact-dialog"
import { 
  importTechVendorContacts, 
  generateContactImportTemplate, 
  parseContactImportFile 
} from "@/lib/tech-vendors/service"

interface TechVendorContactsTableToolbarActionsProps {
  table: Table<TechVendorContact>
  vendorId: number
}

export function TechVendorContactsTableToolbarActions({ table, vendorId }: TechVendorContactsTableToolbarActionsProps) {
  // 파일 input을 숨기고, 버튼 클릭 시 참조해 클릭하는 방식
  const fileInputRef = React.useRef<HTMLInputElement>(null)

  // 파일이 선택되었을 때 처리
  async function onFileChange(event: React.ChangeEvent<HTMLInputElement>) {
    const file = event.target.files?.[0]
    if (!file) return

    // 파일 초기화 (동일 파일 재업로드 시에도 onChange가 트리거되도록)
    event.target.value = ""

    try {
      // Excel 파일 파싱
      const contactData = await parseContactImportFile(file)
      
      if (contactData.length === 0) {
        toast.error("유효한 데이터가 없습니다. 템플릿 형식을 확인해주세요.")
        return
      }

      // 서버로 데이터 전송
      const result = await importTechVendorContacts(contactData)

      if (result.successCount > 0) {
        toast.success(`${result.successCount}개 연락처가 성공적으로 추가되었습니다.`)
      }
      
      if (result.failedRows.length > 0) {
        toast.error(`${result.failedRows.length}개 행에서 오류가 발생했습니다.`)
        
        // 에러 데이터를 Excel로 다운로드
        const errorWorkbook = new ExcelJS.Workbook()
        const errorWorksheet = errorWorkbook.addWorksheet("오류내역")
        
        // 헤더 추가
        errorWorksheet.columns = [
          { header: "행번호", key: "row", width: 10 },
          { header: "벤더이메일", key: "vendorEmail", width: 25 },
          { header: "담당자명", key: "contactName", width: 20 },
          { header: "담당자이메일", key: "contactEmail", width: 25 },
          { header: "오류내용", key: "error", width: 80, style: { alignment: { wrapText: true } , font: { color: { argb: "FFFF0000" } } } },
        ]
        
        // 오류 데이터 추가
        result.failedRows.forEach(failedRow => {
          errorWorksheet.addRow({
            row: failedRow.row,
            error: failedRow.error,
            vendorEmail: failedRow.vendorEmail,
            contactName: failedRow.contactName,
            contactEmail: failedRow.contactEmail,
          })
        })
        
        const buffer = await errorWorkbook.xlsx.writeBuffer()
        const blob = new Blob([buffer], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        })
        const url = URL.createObjectURL(blob)
        const link = document.createElement("a")
        link.href = url
        link.download = "contact-import-errors.xlsx"
        link.click()
        URL.revokeObjectURL(url)
      }

    } catch (error) {
      toast.error("파일 업로드 중 오류가 발생했습니다.")
      console.error("Import error:", error)
    }
  }

  function handleImportClick() {
    // 숨겨진 <input type="file" /> 요소를 클릭
    fileInputRef.current?.click()
  }

  async function handleTemplateDownload() {
    try {
      const templateBlob = await generateContactImportTemplate()
      const url = URL.createObjectURL(templateBlob)
      const link = document.createElement("a")
      link.href = url
      link.download = "tech-vendor-contacts-template.xlsx"
      link.click()
      URL.revokeObjectURL(url)
      toast.success("템플릿이 다운로드되었습니다.")
    } catch (error) {
      toast.error("템플릿 다운로드 중 오류가 발생했습니다.")
      console.error("Template download error:", error)
    }
  }

  return (
    <div className="flex items-center gap-2">

      <AddContactDialog vendorId={vendorId}/>

      {/** 템플릿 다운로드 버튼 */}
      <Button variant="outline" size="sm" className="gap-2" onClick={handleTemplateDownload}>
        <Download className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">템플릿</span>
      </Button>

      {/** Import 버튼 (파일 업로드) */}
      <Button variant="outline" size="sm" className="gap-2" onClick={handleImportClick}>
        <Upload className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">Import</span>
      </Button>
      {/*
        실제로는 숨겨진 input과 연결:
        - accept=".xlsx,.xls" 등으로 Excel 파일만 업로드 허용
      */}
      <input
        ref={fileInputRef}
        type="file"
        accept=".xlsx,.xls"
        className="hidden"
        onChange={onFileChange}
      />

      {/** Export 버튼 */}
      <Button
        variant="outline"
        size="sm"
        onClick={() =>
          exportTableToExcel(table, {
            filename: "tech-vendor-contacts",
            excludeColumns: ["select", "actions"],
          })
        }
        className="gap-2"
      >
        <Download className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">Export</span>
      </Button>
    </div>
  )
}