summaryrefslogtreecommitdiff
path: root/lib/bidding-projects/table/projects-table-toolbar-actions.tsx
blob: ee2f8c4e59d80154a87f4509acb572369ef05a1e (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
"use client"

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

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import { BiddingProjects } from "@/db/schema"

interface ItemsTableToolbarActionsProps {
  table: Table<BiddingProjects>
}

export function ProjectTableToolbarActions({ table }: ItemsTableToolbarActionsProps) {
  const [isLoading, setIsLoading] = React.useState(false)

  // 프로젝트 동기화 API 호출 함수
  const syncProjects = async () => {
    try {
      setIsLoading(true)
      
      // API 엔드포인트 호출
      const response = await fetch('/api/cron/bid-projects')
      
      if (!response.ok) {
        const errorData = await response.json()
        throw new Error(errorData.error || 'Failed to sync projects')
      }
      
      const data = await response.json()
      
      // 성공 메시지 표시
      toast.success(
        `Projects synced successfully! ${data.result.items} items processed.`
      )
      
      // 페이지 새로고침으로 테이블 데이터 업데이트
      window.location.reload()
    } catch (error) {
      console.error('Error syncing projects:', error)
      toast.error(
        error instanceof Error 
          ? error.message 
          : 'An error occurred while syncing projects'
      )
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <div className="flex items-center gap-2">
      <Button
        variant="samsung"
        size="sm"
        className="gap-2"
        onClick={syncProjects}
        disabled={isLoading}
      >
        <RefreshCcw 
          className={`size-4 ${isLoading ? 'animate-spin' : ''}`} 
          aria-hidden="true" 
        />
        <span className="hidden sm:inline">
          {isLoading ? 'Syncing...' : 'Get Projects'}
        </span>
      </Button>

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