'use client'; import { useEffect, useState } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card } from '@/components/ui/card'; import { Skeleton } from '@/components/ui/skeleton'; import { API_BASE } from '@/lib/config'; export default function EditContentPage() { const params = useParams(); const router = useRouter(); const [form, setForm] = useState({ title: '', summary: '', content: '', cover: '', contentType: 'article' }); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); useEffect(() => { loadContent(); }, [params.id]); const base = API_BASE; function token() { return localStorage.getItem('adminToken'); } function headers() { const t = token(); return { 'Content-Type': 'application/json', ...(t ? { Authorization: `Bearer ${t}` } : {}) }; } async function loadContent() { try { const res = await fetch(`${base}/contents/${params.id}`, { headers: headers() }); if (res.ok) { const data = await res.json(); setForm({ title: data.title || '', summary: data.summary || '', content: data.content || '', cover: data.cover || '', contentType: data.contentType || 'article' }); } } catch {} setLoading(false); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!form.title.trim() || !form.content.trim()) return; setSubmitting(true); try { const res = await fetch(`${base}/contents/${params.id}`, { method: 'PUT', headers: headers(), body: JSON.stringify(form), }); if (res.ok) router.push('/admin/contents'); } catch {} setSubmitting(false); } if (loading) return (
); return ( <>

编辑内容

setForm(f => ({ ...f, title: e.target.value }))} required />
setForm(f => ({ ...f, summary: e.target.value }))} />