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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// components/assign-domain-dialog.tsx
"use client"
import * as React from "react"
import { User } from "@/db/schema/users"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Separator } from "@/components/ui/separator"
import { Users, Loader2, CheckCircle } from "lucide-react"
import { toast } from "sonner"
import { assignUsersDomain } from "../service"
interface AssignDomainDialogProps {
users: User[]
}
// 도메인 옵션 정의
const domainOptions = [
{
value: "pending",
label: "승인 대기",
description: "신규 사용자 (기본 메뉴만)",
color: "yellow",
icon: "🟡"
},
{
value: "evcp",
label: "전체 시스템",
description: "모든 메뉴 접근 (관리자급)",
color: "blue",
icon: "🔵"
},
{
value: "procurement",
label: "구매관리팀",
description: "구매, 협력업체, 계약 관리",
color: "green",
icon: "🟢"
},
{
value: "sales",
label: "기술영업팀",
description: "기술영업, 견적, 프로젝트 관리",
color: "purple",
icon: "🟣"
},
{
value: "engineering",
label: "설계관리팀",
description: "설계, 기술평가, 문서 관리",
color: "orange",
icon: "🟠"
},
{
value: "partners",
label: "협력업체",
description: "외부 협력업체용 기능",
color: "indigo",
icon: "🟦"
}
]
export function AssignDomainDialog({ users }: AssignDomainDialogProps) {
const [open, setOpen] = React.useState(false)
const [selectedDomain, setSelectedDomain] = React.useState<string>("")
const [isLoading, setIsLoading] = React.useState(false)
// 도메인별 사용자 그룹핑
const usersByDomain = React.useMemo(() => {
const groups: Record<string, User[]> = {}
users.forEach(user => {
const domain = user.domain || "pending"
if (!groups[domain]) {
groups[domain] = []
}
groups[domain].push(user)
})
return groups
}, [users])
const handleAssign = async () => {
if (!selectedDomain) {
toast.error("도메인을 선택해주세요.")
return
}
setIsLoading(true)
try {
const userIds = users.map(user => user.id)
const result = await assignUsersDomain(userIds, selectedDomain as any)
if (result.success) {
toast.success(`${users.length}명의 사용자에게 ${selectedDomain} 도메인이 할당되었습니다.`)
setOpen(false)
setSelectedDomain("")
// 테이블 새로고침을 위해 router.refresh() 또는 revalidation 필요
window.location.reload() // 간단한 방법
} else {
toast.error(result.message || "도메인 할당 중 오류가 발생했습니다.")
}
} catch (error) {
console.error("도메인 할당 오류:", error)
toast.error("도메인 할당 중 오류가 발생했습니다.")
} finally {
setIsLoading(false)
}
}
const selectedDomainInfo = domainOptions.find(option => option.value === selectedDomain)
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm" className="gap-2">
<Users className="size-4" />
도메인 할당 ({users.length}명)
</Button>
</DialogTrigger>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Users className="size-5" />
사용자 도메인 할당
</DialogTitle>
<DialogDescription>
선택된 {users.length}명의 사용자에게 도메인을 할당합니다.
</DialogDescription>
</DialogHeader>
<div className="space-y-6">
{/* 현재 사용자 도메인 분포 */}
<div>
<h4 className="text-sm font-medium mb-3">현재 도메인 분포</h4>
<div className="flex flex-wrap gap-2">
{Object.entries(usersByDomain).map(([domain, domainUsers]) => {
const domainInfo = domainOptions.find(opt => opt.value === domain)
return (
<Badge key={domain} variant="outline" className="gap-1">
<span>{domainInfo?.icon || "⚪"}</span>
{domainInfo?.label || domain} ({domainUsers.length}명)
</Badge>
)
})}
</div>
</div>
<Separator />
{/* 사용자 목록 */}
<div>
<h4 className="text-sm font-medium mb-3">대상 사용자</h4>
<ScrollArea className="h-32 w-full border rounded-md p-3">
<div className="space-y-2">
{users.map((user, index) => (
<div key={user.id} className="flex items-center justify-between text-sm">
<div>
<span className="font-medium">{user.name}</span>
<span className="text-muted-foreground ml-2">({user.email})</span>
</div>
<Badge variant="secondary" className="text-xs">
{domainOptions.find(opt => opt.value === user.domain)?.label || user.domain}
</Badge>
</div>
))}
</div>
</ScrollArea>
</div>
<Separator />
{/* 도메인 선택 */}
<div>
<h4 className="text-sm font-medium mb-3">할당할 도메인</h4>
<Select value={selectedDomain} onValueChange={setSelectedDomain}>
<SelectTrigger>
<SelectValue placeholder="도메인을 선택하세요" />
</SelectTrigger>
<SelectContent>
{domainOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
<div className="flex items-start gap-3 py-1">
<span className="text-lg">{option.icon}</span>
<div className="flex-1">
<div className="font-medium">{option.label}</div>
<div className="text-xs text-muted-foreground">
{option.description}
</div>
</div>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* 선택된 도메인 미리보기 */}
{selectedDomainInfo && (
<div className="bg-gray-50 rounded-lg p-4">
<div className="flex items-center gap-2 mb-2">
<CheckCircle className="size-4 text-green-600" />
<span className="font-medium">선택된 도메인</span>
</div>
<div className="flex items-center gap-3">
<span className="text-2xl">{selectedDomainInfo.icon}</span>
<div>
<div className="font-medium">{selectedDomainInfo.label}</div>
<div className="text-sm text-muted-foreground">
{selectedDomainInfo.description}
</div>
</div>
</div>
</div>
)}
</div>
<DialogFooter className="gap-2">
<Button
variant="outline"
onClick={() => setOpen(false)}
disabled={isLoading}
>
취소
</Button>
<Button
onClick={handleAssign}
disabled={!selectedDomain || isLoading}
>
{isLoading && <Loader2 className="size-4 mr-2 animate-spin" />}
{users.length}명에게 도메인 할당
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
|