summaryrefslogtreecommitdiff
path: root/components/docu-list-rule/docu-list-rule-client.tsx
blob: 7e6c2bb1767cd89d4110981b1b6fa33446b33be8 (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
"use client"
import * as React from "react"
import { useRouter, useParams } from "next/navigation"

import { getProjectLists } from "@/lib/projects/service"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"

interface DocuListRuleClientProps {
    children: React.ReactNode
}

export default function DocuListRuleClient({
    children,
}: DocuListRuleClientProps) {
    const router = useRouter()
    const params = useParams()
    const lng = (params?.lng as string) || "ko"
    
    // Get the projectId from route parameters
    const projectIdFromUrl = React.useMemo(() => {
        if (params?.projectId) {
            const projectId = Array.isArray(params.projectId) 
                ? params.projectId[0] 
                : params.projectId
            return Number(projectId)
        }
        return null
    }, [params])
    
    // Use the URL projectId as the selected project
    const [selectedProjectId, setSelectedProjectId] = React.useState<number | null>(
        projectIdFromUrl
    )
    
    // 프로젝트 목록 상태
    const [projects, setProjects] = React.useState<Array<{ id: number; code: string; name: string; type: string }>>([])
    const [isLoading, setIsLoading] = React.useState(true)
    
    // Update selectedProjectId when URL changes
    React.useEffect(() => {
        if (projectIdFromUrl) {
            setSelectedProjectId(projectIdFromUrl)
        }
    }, [projectIdFromUrl])

    // 프로젝트 목록 로드
    React.useEffect(() => {
        const loadProjects = async () => {
            try {
                setIsLoading(true)
                console.log("Loading projects...")
                const result = await getProjectLists({ 
                    page: 1, 
                    perPage: 1000,
                    search: "",
                    sort: [],
                    filters: [],
                    joinOperator: "and",
                    flags: [],
                    code: "",
                    name: "",
                    type: ""
                })
                console.log("Projects result:", result)
                if (result.data) {
                    // plant 타입의 프로젝트만 필터링
                    const plantProjects = result.data.filter(project => project.type === 'plant')
                    console.log("Plant projects:", plantProjects)
                    setProjects(plantProjects)
                }
            } catch (error) {
                console.error("Failed to load projects:", error)
            } finally {
                setIsLoading(false)
            }
        }
        loadProjects()
    }, [])

    // Handle project selection
    function handleSelectProject(projectId: number) {
        console.log("Selecting project:", projectId)
        setSelectedProjectId(projectId)
        
        // Navigate to the project's document-class page
        router.push(`/${lng}/evcp/docu-list-rule/${projectId}`)
    }
    
    return (
        <>
            {/* 상단 영역: 제목 왼쪽 / ProjectSwitcher 오른쪽 */}
            <div className="flex items-center justify-between">
                {/* 왼쪽: 타이틀 & 설명 */}
                <div>
                    <div className="flex items-center gap-2">
                        <h2 className="text-2xl font-bold tracking-tight">Document Numbering Rule (해양)</h2>
                    </div>
                    <p className="text-muted-foreground">
                        벤더 제출 문서 리스트 작성 시에 사용되는 넘버링
                    </p>
                </div>
                
                {/* 오른쪽: ProjectSwitcher */}
                <div className="flex items-center space-x-2">
                    <Select 
                        value={selectedProjectId ? String(selectedProjectId) : ""} 
                        onValueChange={(value) => {
                            const projectId = Number(value)
                            if (projectId) {
                                handleSelectProject(projectId)
                            }
                        }}
                        disabled={isLoading}
                    >
                        <SelectTrigger className="max-w-[300px] whitespace-nowrap overflow-hidden text-ellipsis">
                            <SelectValue placeholder={isLoading ? "프로젝트 로딩 중..." : "프로젝트를 선택하세요"} />
                        </SelectTrigger>
                        <SelectContent>
                            {projects.map((project) => (
                                <SelectItem key={project.id} value={String(project.id)}>
                                    {project.code} - {project.name}
                                </SelectItem>
                            ))}
                        </SelectContent>
                    </Select>
                </div>
            </div>
            
            {/* 문서 목록/테이블 영역 */}
            <section className="overflow-hidden rounded-[0.5rem] border bg-background shadow p-5">
                {children}
            </section>
        </>
    )
}