"use client"; import { useEffect, useState } from "react"; import { useRouter, useParams } from "next/navigation"; import { useAuth } from "@/contexts/AuthContext"; import { productsApi, ProductAdmin, getUploadUrl } from "@/lib/api"; import ProductForm from "@/components/admin/ProductForm"; import { Loader2, Upload, Trash2 } from "lucide-react"; import YouTubeVideoManager from "@/components/admin/YouTubeVideoManager"; export default function EditProductPage() { const { token } = useAuth(); const router = useRouter(); const params = useParams(); const productId = Number(params.id); const [product, setProduct] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [isUploading, setIsUploading] = useState(false); const fetchProduct = async () => { if (!token) return; try { const data = await productsApi.adminGetOne(productId, token); setProduct(data); } catch (err) { alert("제품을 찾을 수 없습니다."); router.push("/admin/products"); } finally { setIsLoading(false); } }; useEffect(() => { fetchProduct(); }, [token, productId]); const handleSubmit = async (data: any) => { if (!token) return; setIsSubmitting(true); try { await productsApi.adminUpdate(productId, data, token); alert("저장되었습니다."); fetchProduct(); } catch (err) { alert("저장에 실패했습니다."); } finally { setIsSubmitting(false); } }; const handleImageUpload = async ( e: React.ChangeEvent, isMain: boolean ) => { if (!token || !e.target.files?.[0]) return; setIsUploading(true); try { await productsApi.adminUploadImage( productId, e.target.files[0], isMain, token ); fetchProduct(); } catch (err) { alert("이미지 업로드에 실패했습니다."); } finally { setIsUploading(false); e.target.value = ""; } }; const handleDeleteImage = async (imageId: number) => { if (!token || !confirm("이미지를 삭제하시겠습니까?")) return; try { await productsApi.adminDeleteImage(imageId, token); fetchProduct(); } catch (err) { alert("이미지 삭제에 실패했습니다."); } }; if (isLoading) { return (
); } if (!product) return null; return (

제품 수정

{/* Form */}
{/* Image Upload */}
{/* Main Image */}

대표 이미지

{product.main_image ? (
Main
) : ( )}
{/* Additional Images */}

추가 이미지

{product.images?.map((img) => (
))}
{/* YouTube Videos */}
); }