|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import { useParams } from 'next/navigation'; |
| 5 | +import Link from 'next/link'; |
| 6 | +import http from '@/lib/axios'; |
| 7 | +import MarkdownEditor from '@/components/MarkdownEditor'; |
| 8 | +import type { PostWithLabels } from '@/types/api'; |
| 9 | + |
| 10 | +export default function EditPostPage() { |
| 11 | + const params = useParams(); |
| 12 | + const postId = params.id as string; |
| 13 | + |
| 14 | + const [post, setPost] = useState<PostWithLabels | null>(null); |
| 15 | + const [isLoading, setIsLoading] = useState(true); |
| 16 | + const [error, setError] = useState<string | null>(null); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const fetchPost = async () => { |
| 20 | + try { |
| 21 | + const data = await http.get<PostWithLabels>(`/posts/${postId}`); |
| 22 | + if (data) { |
| 23 | + setPost(data); |
| 24 | + } else { |
| 25 | + setError('文章不存在'); |
| 26 | + } |
| 27 | + } catch (err) { |
| 28 | + console.error('获取文章失败:', err); |
| 29 | + setError('获取文章失败'); |
| 30 | + } finally { |
| 31 | + setIsLoading(false); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + if (postId) { |
| 36 | + fetchPost(); |
| 37 | + } |
| 38 | + }, [postId]); |
| 39 | + |
| 40 | + if (isLoading) { |
| 41 | + return ( |
| 42 | + <div className="flex items-center justify-center h-64"> |
| 43 | + <span className="loading loading-spinner loading-lg text-primary"></span> |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (error || !post) { |
| 49 | + return ( |
| 50 | + <div className="space-y-4"> |
| 51 | + <Link href="/dashboard/posts" className="btn btn-ghost btn-sm"> |
| 52 | + <svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 53 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" /> |
| 54 | + </svg> |
| 55 | + 返回 |
| 56 | + </Link> |
| 57 | + <div className="alert alert-error"> |
| 58 | + <svg className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 59 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> |
| 60 | + </svg> |
| 61 | + <span>{error || '文章不存在'}</span> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="space-y-4"> |
| 69 | + <div className="flex items-center justify-between"> |
| 70 | + <div className="flex items-center gap-4"> |
| 71 | + <Link href="/dashboard/posts" className="btn btn-ghost btn-sm"> |
| 72 | + <svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 73 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" /> |
| 74 | + </svg> |
| 75 | + 返回 |
| 76 | + </Link> |
| 77 | + <h2 className="text-2xl font-bold">编辑文章</h2> |
| 78 | + <span className="text-sm text-base-content/50">ID: {postId}</span> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div className="card bg-base-100 shadow-md"> |
| 83 | + <div className="card-body p-0"> |
| 84 | + <MarkdownEditor |
| 85 | + blogMode |
| 86 | + initialData={{ |
| 87 | + id: post.id, |
| 88 | + title: post.title, |
| 89 | + slug: post.slug, |
| 90 | + content: post.content, |
| 91 | + excerpt: post.excerpt || '', |
| 92 | + coverImage: post.featured_image || '', |
| 93 | + tags: post.labels || [], |
| 94 | + published: post.published, |
| 95 | + }} |
| 96 | + /> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments