import * as React from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Check, ChevronsUpDown, Loader, AlertTriangle } from "lucide-react" import { cn } from "@/lib/utils" import { toast } from "sonner" import { Alert, AlertDescription } from "@/components/ui/alert" import { createRoleSchema, type CreateRoleSchema } from "../validations" import { createRole, checkRegularEvaluationRoleExists } from "../services" import { Textarea } from "@/components/ui/textarea" import { Company } from "@/db/schema/companies" import { getAllCompanies } from "@/lib/admin-users/service" import { Popover, PopoverTrigger, PopoverContent, } from "@/components/ui/popover" import { Command, CommandInput, CommandList, CommandGroup, CommandItem, CommandEmpty, } from "@/components/ui/command" const domainOptions = [ { value: "partners", label: "협력업체" }, { value: "evcp", label: "삼성중공업" }, ] export function AddRoleDialog() { const [open, setOpen] = React.useState(false) const [isAddPending, startAddTransition] = React.useTransition() const [companies, setCompanies] = React.useState([]) const [regularEvaluationExists, setRegularEvaluationExists] = React.useState(false) const [isCheckingRegularEvaluation, setIsCheckingRegularEvaluation] = React.useState(false) React.useEffect(() => { getAllCompanies().then((res) => { setCompanies(res) }) }, []) // react-hook-form 세팅 const form = useForm({ resolver: zodResolver(createRoleSchema), defaultValues: { name: "", domain: "evcp", description: "", }, }) // name 필드 watch const watchedName = form.watch("name") // "정기평가"가 포함된 이름인지 체크 const isRegularEvaluationRole = watchedName.includes("정기평가") // 정기평가 role 존재 여부 체크 (debounced) React.useEffect(() => { if (!isRegularEvaluationRole) { setRegularEvaluationExists(false) return } const timeoutId = setTimeout(async () => { setIsCheckingRegularEvaluation(true) try { const exists = await checkRegularEvaluationRoleExists() setRegularEvaluationExists(exists) } catch (error) { console.error("정기평가 role 체크 실패:", error) } finally { setIsCheckingRegularEvaluation(false) } }, 500) // 500ms debounce return () => clearTimeout(timeoutId) }, [isRegularEvaluationRole, watchedName]) async function onSubmit(data: CreateRoleSchema) { startAddTransition(async () => { const result = await createRole(data) if (result.error) { toast.error(`에러: ${result.error}`) return } form.reset() setOpen(false) setRegularEvaluationExists(false) toast.success("Role이 성공적으로 추가되었습니다") }) } function handleDialogOpenChange(nextOpen: boolean) { if (!nextOpen) { form.reset() setRegularEvaluationExists(false) } setOpen(nextOpen) } const selectedDomain = form.watch("domain") const canSubmit = !isRegularEvaluationRole || !regularEvaluationExists return ( Create New Role 새 Role 정보를 입력하고 Create 버튼을 누르세요.
{/* 1) Role Name */} ( Role Name {/* 정기평가 관련 경고 메시지 */} {isRegularEvaluationRole && (
{isCheckingRegularEvaluation ? ( 정기평가 role 존재 여부를 확인하고 있습니다... ) : regularEvaluationExists ? ( 경고: "정기평가"가 포함된 role이 이미 존재합니다. 정기평가 role은 시스템에서 하나만 허용됩니다. ) : ( 정기평가 role을 생성할 수 있습니다. )}
)}
)} /> {/* 2) Description */} ( Role Description