"use client"; import { useState } from "react"; import { Check, ChevronsUpDown, Search, X } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { toast } from "sonner"; import { updateMenuManager } from "../servcie"; interface User { id: number; name: string; email: string; domain: string; } interface ManagerSelectProps { menuPath: string; currentManagerId?: number | null; users: User[]; placeholder: string; otherManagerId?: number | null; // 다른 담당자 ID (중복 선택 방지용) type: "manager1" | "manager2"; } export function ManagerSelect({ menuPath, currentManagerId, users, placeholder, otherManagerId, type }: ManagerSelectProps) { const [open, setOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); // 선택 가능한 사용자 필터링 (다른 담당자로 이미 선택된 사용자 제외) const availableUsers = users.filter(user => user.id !== otherManagerId || user.id === currentManagerId ); // 현재 선택된 사용자 찾기 const selectedUser = availableUsers.find(user => user.id === currentManagerId); const handleSelect = async (userId: number | null) => { setIsLoading(true); setOpen(false); try { // 현재 담당자 정보 구성 const updateData = type === "manager1" ? { manager1Id: userId, manager2Id: otherManagerId } : { manager1Id: otherManagerId, manager2Id: userId }; const result = await updateMenuManager( menuPath, updateData.manager1Id, updateData.manager2Id ); if (result.success) { toast.success(result.message); } else { toast.error(result.message); } } catch (error) { toast.error("담당자 업데이트 중 오류가 발생했습니다."); } finally { setIsLoading(false); } }; const handleClear = () => { handleSelect(null); }; return ( )} ) : ( {placeholder} )} 검색 결과가 없습니다. {/* 담당자 없음 옵션 */} handleSelect(null)} className="flex items-center gap-2" >
담당자 없음 담당자를 지정하지 않습니다
{/* 사용자 목록 */} {availableUsers.map((user) => ( handleSelect(user.id)} className="flex items-center gap-2" disabled={user.id === otherManagerId && user.id !== currentManagerId} >
{user.name} {user.email}
{user.id === otherManagerId && user.id !== currentManagerId && ( 다른 담당자로 선택됨 )}
))}
); }