"use client"; import React, { useState } from "react"; import { useRouter } from "next/navigation"; 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 { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Switch } from "@/components/ui/switch"; import { Badge } from "@/components/ui/badge"; import { toast } from "sonner"; import { ArrowLeft, Save, RotateCcw } from "lucide-react"; import { useWebTypes, type WebTypeFormData } from "@/hooks/admin/useWebTypes"; import { AVAILABLE_COMPONENTS } from "@/lib/utils/availableComponents"; import { AVAILABLE_CONFIG_PANELS, getConfigPanelInfo } from "@/lib/utils/availableConfigPanels"; import Link from "next/link"; // 기본 카테고리 목록 const DEFAULT_CATEGORIES = ["input", "select", "display", "special"]; export default function NewWebTypePage() { const router = useRouter(); const { createWebType, isCreating, createError } = useWebTypes(); const [formData, setFormData] = useState({ web_type: "", type_name: "", type_name_eng: "", description: "", category: "input", component_name: "TextWidget", config_panel: "none", default_config: {}, validation_rules: {}, default_style: {}, input_properties: {}, sort_order: 0, is_active: "Y", }); const [jsonErrors, setJsonErrors] = useState<{ default_config?: string; validation_rules?: string; default_style?: string; input_properties?: string; }>({}); // JSON 문자열 상태 (편집용) const [jsonStrings, setJsonStrings] = useState({ default_config: "{}", validation_rules: "{}", default_style: "{}", input_properties: "{}", }); // 입력값 변경 핸들러 const handleInputChange = (field: keyof WebTypeFormData, value: any) => { setFormData((prev) => ({ ...prev, [field]: value, })); }; // JSON 입력 변경 핸들러 const handleJsonChange = ( field: "default_config" | "validation_rules" | "default_style" | "input_properties", value: string, ) => { setJsonStrings((prev) => ({ ...prev, [field]: value, })); // JSON 파싱 시도 try { const parsed = value.trim() ? JSON.parse(value) : {}; setFormData((prev) => ({ ...prev, [field]: parsed, })); setJsonErrors((prev) => ({ ...prev, [field]: undefined, })); } catch (error) { setJsonErrors((prev) => ({ ...prev, [field]: "유효하지 않은 JSON 형식입니다.", })); } }; // 폼 유효성 검사 const validateForm = (): boolean => { if (!formData.web_type.trim()) { toast.error("웹타입 코드를 입력해주세요."); return false; } if (!formData.type_name.trim()) { toast.error("웹타입명을 입력해주세요."); return false; } if (!formData.category.trim()) { toast.error("카테고리를 선택해주세요."); return false; } // JSON 에러가 있는지 확인 const hasJsonErrors = Object.values(jsonErrors).some((error) => error); if (hasJsonErrors) { toast.error("JSON 형식 오류를 수정해주세요."); return false; } return true; }; // 저장 핸들러 const handleSave = async () => { if (!validateForm()) return; try { await createWebType(formData); toast.success("웹타입이 성공적으로 생성되었습니다."); router.push("/admin/standards"); } catch (error) { toast.error(error instanceof Error ? error.message : "생성 중 오류가 발생했습니다."); } }; // 폼 초기화 const handleReset = () => { setFormData({ web_type: "", type_name: "", type_name_eng: "", description: "", category: "input", component_name: "TextWidget", config_panel: "none", default_config: {}, validation_rules: {}, default_style: {}, input_properties: {}, sort_order: 0, is_active: "Y", }); setJsonStrings({ default_config: "{}", validation_rules: "{}", default_style: "{}", input_properties: "{}", }); setJsonErrors({}); }; return (
{/* 헤더 */}

새 웹타입 추가

새로운 웹타입을 생성하여 화면관리에서 사용할 수 있습니다.

{/* 기본 정보 */} 기본 정보 웹타입의 기본적인 정보를 입력해주세요. {/* 웹타입 코드 */}
handleInputChange("web_type", e.target.value)} placeholder="예: text, number, email..." className="font-mono" />

영문 소문자, 숫자, 언더스코어(_)만 사용 가능합니다.

{/* 웹타입명 */}
handleInputChange("type_name", e.target.value)} placeholder="예: 텍스트 입력" />
handleInputChange("type_name_eng", e.target.value)} placeholder="예: Text Input" />
{/* 카테고리 */}
{/* 연결된 컴포넌트 */}
{formData.component_name && (
현재 선택:{" "} {formData.component_name}
)}
{/* 설정 패널 */}
{formData.config_panel && (
현재 선택: {getConfigPanelInfo(formData.config_panel)?.label || formData.config_panel}
{getConfigPanelInfo(formData.config_panel)?.description && (

{getConfigPanelInfo(formData.config_panel)?.description}

)}
)}
{/* 설명 */}