summaryrefslogtreecommitdiff
path: root/lib/shi-signature/signature-list.tsx
blob: 93cd3dbe080b05d1bf9d8920ba59a350db7f4c1f (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
'use client';

import { useState } from 'react';
import { BuyerSignature } from '@/db/schemae';
import { setActiveSignature, deleteSignature } from './buyer-signature';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Trash2, CheckCircle, Circle,Loader2 } from 'lucide-react';
import { toast } from 'sonner';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from '@/components/ui/alert-dialog';

interface SignatureListProps {
  signatures: BuyerSignature[];
}

export function SignatureList({ signatures }: SignatureListProps) {
  const [isUpdating, setIsUpdating] = useState<number | null>(null);

  const handleSetActive = async (id: number) => {
    setIsUpdating(id);
    try {
      const result = await setActiveSignature(id);
      if (result.success) {
        toast.success('활성 서명이 변경되었습니다.');
      } else {
        toast.error(result.error || '변경에 실패했습니다.');
      }
    } catch (error) {
      toast.error('오류가 발생했습니다.');
    } finally {
      setIsUpdating(null);
    }
  };

  const handleDelete = async (id: number) => {
    try {
      const result = await deleteSignature(id);
      if (result.success) {
        toast.success('서명이 삭제되었습니다.');
      } else {
        toast.error(result.error || '삭제에 실패했습니다.');
      }
    } catch (error) {
      toast.error('오류가 발생했습니다.');
    }
  };

  if (signatures.length === 0) {
    return (
      <Card>
        <CardContent className="py-8 text-center text-muted-foreground">
          아직 업로드된 서명이 없습니다.
        </CardContent>
      </Card>
    );
  }

  return (
    <Card>
      <CardHeader>
        <CardTitle>서명 목록</CardTitle>
      </CardHeader>
      <CardContent className="space-y-4">
        {signatures.map((signature) => (
          <div
            key={signature.id}
            className="flex items-center justify-between p-4 border rounded-lg"
          >
            <div className="flex items-center space-x-4">
              <img
                src={signature.imageUrl}
                alt="서명"
                className="h-12 object-contain border rounded p-1"
              />
              <div>
                <div className="flex items-center space-x-2">
                  <span className="font-medium">{signature.name}</span>
                  {signature.isActive && (
                    <Badge variant="default" className="text-xs">
                      활성
                    </Badge>
                  )}
                </div>
                <p className="text-sm text-muted-foreground">
                  {new Date(signature.createdAt).toLocaleDateString()}
                </p>
              </div>
            </div>
            
            <div className="flex items-center space-x-2">
              {!signature.isActive && (
                <Button
                  variant="outline"
                  size="sm"
                  onClick={() => handleSetActive(signature.id)}
                  disabled={isUpdating === signature.id}
                >
                  {isUpdating === signature.id ? (
                    <Loader2 className="h-4 w-4 animate-spin" />
                  ) : (
                    <Circle className="h-4 w-4" />
                  )}
                  <span className="ml-2">활성화</span>
                </Button>
              )}
              
              <AlertDialog>
                <AlertDialogTrigger asChild>
                  <Button
                    variant="outline"
                    size="sm"
                    disabled={signature.isActive}
                  >
                    <Trash2 className="h-4 w-4" />
                  </Button>
                </AlertDialogTrigger>
                <AlertDialogContent>
                  <AlertDialogHeader>
                    <AlertDialogTitle>서명 삭제</AlertDialogTitle>
                    <AlertDialogDescription>
                      이 서명을 삭제하시겠습니까? 이 작업은 취소할 수 없습니다.
                    </AlertDialogDescription>
                  </AlertDialogHeader>
                  <AlertDialogFooter>
                    <AlertDialogCancel>취소</AlertDialogCancel>
                    <AlertDialogAction onClick={() => handleDelete(signature.id)}>
                      삭제
                    </AlertDialogAction>
                  </AlertDialogFooter>
                </AlertDialogContent>
              </AlertDialog>
            </div>
          </div>
        ))}
      </CardContent>
    </Card>
  );
}