summaryrefslogtreecommitdiff
path: root/lib/payment-terms/table/payment-terms-table-columns.tsx
blob: 208723f7614492b678a8020e6c9f73d5dbb619bb (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
import { type ColumnDef, type Row } from "@tanstack/react-table";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Ellipsis } from "lucide-react";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { paymentTerms } from "@/db/schema/procurementRFQ";
import { toast } from "sonner";
import { deletePaymentTerm } from "../service";

type PaymentTerm = typeof paymentTerms.$inferSelect;

interface GetColumnsProps {
  setRowAction: (action: { type: string; row: Row<PaymentTerm> }) => void;
  onSuccess: () => void;
}

const handleDelete = async (code: string, onSuccess: () => void) => {
  const result = await deletePaymentTerm(code);
  if (result.success) {
    toast.success("삭제 완료");
    onSuccess();
  } else {
    toast.error(result.error || "삭제 실패");
  }
};

export function getColumns({ setRowAction, onSuccess }: GetColumnsProps): ColumnDef<PaymentTerm>[] {
  return [
    {
      id: "code",
      header: () => <div>코드</div>,
      cell: ({ row }) => <div>{row.original.code}</div>,
      enableSorting: true,
      enableHiding: false,
    },
    {
      id: "description",
      header: () => <div>설명</div>,
      cell: ({ row }) => <div>{row.original.description}</div>,
      enableSorting: true,
      enableHiding: false,
    },
    {
      id: "isActive",
      header: () => <div>상태</div>,
      cell: ({ row }) => (
        <Badge variant={row.original.isActive ? "default" : "secondary"}>
          {row.original.isActive ? "활성" : "비활성"}
        </Badge>
      ),
      enableSorting: true,
      enableHiding: false,
    },
    {
      id: "createdAt",
      header: () => <div>생성일</div>,
      cell: ({ row }) => {
        const value = row.original.createdAt;
        const date = value ? new Date(value) : null;
        return date ? date.toLocaleDateString() : "";
      },
      enableSorting: true,
      enableHiding: false,
    },
    {
      id: "actions",
      cell: ({ row }) => (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-8 p-0 data-[state=open]:bg-muted"
            >
              <Ellipsis className="size-4" aria-hidden="true" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-40">
            <DropdownMenuItem
              onSelect={() => setRowAction({ type: "edit", row })}
            >
              수정
            </DropdownMenuItem>
            <DropdownMenuSeparator />
            <DropdownMenuItem
              onSelect={() => handleDelete(row.original.code, onSuccess)}
              className="text-destructive"
            >
              삭제
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      ),
    },
  ];
}