"use client"; import React, { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogHeader, } from "@/components/ui/resizable-dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { toast } from "sonner"; import { CollectionAPI, DataCollectionConfig } from "@/lib/api/collection"; import { ExternalDbConnectionAPI } from "@/lib/api/externalDbConnection"; interface CollectionConfigModalProps { isOpen: boolean; onClose: () => void; onSave: () => void; config?: DataCollectionConfig | null; } export default function CollectionConfigModal({ isOpen, onClose, onSave, config, }: CollectionConfigModalProps) { const [formData, setFormData] = useState>({ config_name: "", description: "", source_connection_id: 0, source_table: "", target_table: "", collection_type: "full", schedule_cron: "", is_active: "Y", collection_options: {}, }); const [isLoading, setIsLoading] = useState(false); const [connections, setConnections] = useState([]); const [tables, setTables] = useState([]); const collectionTypeOptions = CollectionAPI.getCollectionTypeOptions(); useEffect(() => { if (isOpen) { loadConnections(); if (config) { setFormData({ ...config, collection_options: config.collection_options || {}, }); if (config.source_connection_id) { loadTables(config.source_connection_id); } } else { setFormData({ config_name: "", description: "", source_connection_id: 0, source_table: "", target_table: "", collection_type: "full", schedule_cron: "", is_active: "Y", collection_options: {}, }); } } }, [isOpen, config]); const loadConnections = async () => { try { const connectionList = await ExternalDbConnectionAPI.getConnections({ is_active: "Y", }); setConnections(connectionList); } catch (error) { console.error("외부 연결 목록 조회 오류:", error); toast.error("외부 연결 목록을 불러오는데 실패했습니다."); } }; const loadTables = async (connectionId: number) => { try { const result = await ExternalDbConnectionAPI.getTables(connectionId); if (result.success && result.data) { setTables(result.data); } } catch (error) { console.error("테이블 목록 조회 오류:", error); toast.error("테이블 목록을 불러오는데 실패했습니다."); } }; const handleConnectionChange = (connectionId: string) => { const id = parseInt(connectionId); setFormData(prev => ({ ...prev, source_connection_id: id, source_table: "", })); if (id > 0) { loadTables(id); } else { setTables([]); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.config_name || !formData.source_connection_id || !formData.source_table) { toast.error("필수 필드를 모두 입력해주세요."); return; } setIsLoading(true); try { if (config?.id) { await CollectionAPI.updateCollectionConfig(config.id, formData); toast.success("수집 설정이 수정되었습니다."); } else { await CollectionAPI.createCollectionConfig(formData as DataCollectionConfig); toast.success("수집 설정이 생성되었습니다."); } onSave(); onClose(); } catch (error) { console.error("수집 설정 저장 오류:", error); toast.error( error instanceof Error ? error.message : "수집 설정 저장에 실패했습니다." ); } finally { setIsLoading(false); } }; const handleSchedulePresetSelect = (preset: string) => { setFormData(prev => ({ ...prev, schedule_cron: preset, })); }; const schedulePresets = [ { value: "0 */1 * * *", label: "매시간" }, { value: "0 0 */6 * *", label: "6시간마다" }, { value: "0 0 * * *", label: "매일 자정" }, { value: "0 0 * * 0", label: "매주 일요일" }, { value: "0 0 1 * *", label: "매월 1일" }, ]; return ( {config ? "수집 설정 수정" : "새 수집 설정"}
{/* 기본 정보 */}

기본 정보

setFormData(prev => ({ ...prev, config_name: e.target.value })) } placeholder="수집 설정명을 입력하세요" required />