From 10f90dc68dec42e9a64e081cc0dce6a484447290 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Tue, 29 Jul 2025 11:48:59 +0000 Subject: (대표님, 박서영, 최겸) document-list-only, gtc, vendorDocu, docu-list-rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../status/create-gtc-document-dialog.tsx | 98 +++++++++++++++++----- 1 file changed, 75 insertions(+), 23 deletions(-) (limited to 'lib/gtc-contract/status/create-gtc-document-dialog.tsx') diff --git a/lib/gtc-contract/status/create-gtc-document-dialog.tsx b/lib/gtc-contract/status/create-gtc-document-dialog.tsx index 003e4d51..174bb8dd 100644 --- a/lib/gtc-contract/status/create-gtc-document-dialog.tsx +++ b/lib/gtc-contract/status/create-gtc-document-dialog.tsx @@ -41,28 +41,45 @@ import { cn } from "@/lib/utils" import { toast } from "sonner" import { createGtcDocumentSchema, type CreateGtcDocumentSchema } from "@/lib/gtc-contract/validations" -import { createGtcDocument, getProjectsForSelect } from "@/lib/gtc-contract/service" -import { type Project } from "@/db/schema/projects" +import { createGtcDocument, getAvailableProjectsForGtc, hasStandardGtcDocument } from "@/lib/gtc-contract/service" +import { type ProjectForFilter } from "@/lib/gtc-contract/service" import { useSession } from "next-auth/react" import { Input } from "@/components/ui/input" -import { useRouter } from "next/navigation"; +import { useRouter } from "next/navigation" export function CreateGtcDocumentDialog() { const [open, setOpen] = React.useState(false) - const [projects, setProjects] = React.useState([]) + const [projects, setProjects] = React.useState([]) const [isCreatePending, startCreateTransition] = React.useTransition() + const [defaultType, setDefaultType] = React.useState<"standard" | "project">("standard") const { data: session } = useSession() - const router = useRouter(); + const router = useRouter() const currentUserId = React.useMemo(() => { return session?.user?.id ? Number(session.user.id) : null; - }, [session]); - + }, [session]) React.useEffect(() => { if (open) { - getProjectsForSelect().then((res) => { - setProjects(res) + // 표준 GTC 존재 여부와 사용 가능한 프로젝트 동시 조회 + Promise.all([ + hasStandardGtcDocument(), + getAvailableProjectsForGtc() + ]).then(([hasStandard, availableProjects]) => { + const initialType = hasStandard ? "project" : "standard" + setDefaultType(initialType) + setProjects(availableProjects) + + // 폼 기본값 설정 (setTimeout으로 다음 틱에 실행) + setTimeout(() => { + form.reset({ + type: initialType, + projectId: null, + title: "", + revision: 0, + editReason: "", + }) + }, 0) }) } }, [open]) @@ -78,11 +95,21 @@ export function CreateGtcDocumentDialog() { }, }) + const resetForm = React.useCallback((type: "standard" | "project") => { + form.reset({ + type, + projectId: null, + title: "", + revision: 0, + editReason: "", + }) + }, [form]) + + const watchedType = form.watch("type") async function onSubmit(data: CreateGtcDocumentSchema) { startCreateTransition(async () => { - if (!currentUserId) { toast.error("로그인이 필요합니다") return @@ -99,9 +126,9 @@ export function CreateGtcDocumentDialog() { return } - form.reset() + resetForm(defaultType) setOpen(false) - router.refresh(); + router.refresh() toast.success("GTC 문서가 생성되었습니다.") } catch (error) { @@ -112,7 +139,7 @@ export function CreateGtcDocumentDialog() { function handleDialogOpenChange(nextOpen: boolean) { if (!nextOpen) { - form.reset() + resetForm(defaultType) } setOpen(nextOpen) } @@ -122,15 +149,15 @@ export function CreateGtcDocumentDialog() { - Create New GTC Document + 새 GTC 만들기 - 새 GTC 문서 정보를 입력하고 Create 버튼을 누르세요. + 새 GTC 문서 정보를 입력하고 생성 버튼을 누르세요. @@ -159,11 +186,26 @@ export function CreateGtcDocumentDialog() { - 표준 - 프로젝트 + {/* 기본값에 따라 순서 조정 */} + {defaultType === "project" ? ( + <> + 프로젝트 + 표준 + + ) : ( + <> + 표준 + 프로젝트 + + )} + {defaultType === "project" && ( + + 표준 GTC 문서가 이미 존재합니다. 새 문서는 프로젝트 타입을 권장합니다. + + )} )} @@ -198,7 +240,9 @@ export function CreateGtcDocumentDialog() { > {selectedProject ? `${selectedProject.name} (${selectedProject.code})` - : "프로젝트를 선택하세요..."} + : projects.length === 0 + ? "사용 가능한 프로젝트가 없습니다" + : "프로젝트를 선택하세요..."} @@ -210,7 +254,12 @@ export function CreateGtcDocumentDialog() { className="h-9" /> - 프로젝트를 찾을 수 없습니다. + + {projects.length === 0 + ? "모든 프로젝트에 이미 GTC 문서가 있습니다." + : "프로젝트를 찾을 수 없습니다." + } + {projects.map((project) => { const label = `${project.name} (${project.code})` @@ -241,6 +290,9 @@ export function CreateGtcDocumentDialog() { + + {projects.length === 0 && "이미 GTC 문서가 있는 프로젝트는 표시되지 않습니다."} + ) @@ -256,7 +308,7 @@ export function CreateGtcDocumentDialog() { GTC 제목 (선택사항) @@ -295,7 +347,7 @@ export function CreateGtcDocumentDialog() { onClick={() => setOpen(false)} disabled={isCreatePending} > - Cancel + 취소 -- cgit v1.2.3