- 新增事件总线式toast通知系统 - 新增头像、阅读进度、404/500错误页等基础组件 - 重构页脚、布局、分页、点赞按钮等现有组件 - 新增用户个人页、通知中心、帖子编辑/发布/置顶/推荐管理功能 - 优化登录注册页面样式与交互 - 修复路径类型文件引用,更新站点元信息与字体配置
149 lines
4.3 KiB
TypeScript
149 lines
4.3 KiB
TypeScript
"use client";
|
||
|
||
import { useState, useEffect } from "react";
|
||
import { useRouter } from "next/navigation";
|
||
import Link from "next/link";
|
||
import { ChevronRight } from "lucide-react";
|
||
import { fetchPostDetail, apiMe, apiUpdatePost, type Post, type User } from "@/lib/api";
|
||
|
||
interface EditPageProps {
|
||
params: Promise<{ id: string }>;
|
||
}
|
||
|
||
export default function EditPostPage({ params }: EditPageProps) {
|
||
const router = useRouter();
|
||
const [post, setPost] = useState<Post | null>(null);
|
||
const [user, setUser] = useState<User | null>(null);
|
||
const [title, setTitle] = useState("");
|
||
const [content, setContent] = useState("");
|
||
const [tags, setTags] = useState("");
|
||
const [loading, setLoading] = useState(false);
|
||
const [error, setError] = useState("");
|
||
const [checking, setChecking] = useState(true);
|
||
|
||
const [id, setId] = useState("");
|
||
useEffect(() => {
|
||
params.then((p) => setId(p.id));
|
||
}, [params]);
|
||
|
||
useEffect(() => {
|
||
if (!id) return;
|
||
Promise.all([fetchPostDetail(id), apiMe()])
|
||
.then(([postData, meData]) => {
|
||
setPost(postData.post);
|
||
setTitle(postData.post.title);
|
||
setContent(postData.post.content);
|
||
setTags(postData.post.tags || "");
|
||
const u = meData.user;
|
||
setUser(u || null);
|
||
if (!u || (u.id !== postData.post.user.id && u.role !== "admin")) {
|
||
router.push(`/post/${id}`);
|
||
}
|
||
})
|
||
.catch(() => router.push(`/post/${id}`))
|
||
.finally(() => setChecking(false));
|
||
}, [id, router]);
|
||
|
||
const handleSubmit = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
if (!post) return;
|
||
setLoading(true);
|
||
setError("");
|
||
try {
|
||
const res = await apiUpdatePost(String(post.id), { title, content, tags });
|
||
if (res.post) {
|
||
router.push(`/post/${post.id}`);
|
||
} else {
|
||
setError(res.error || "更新失败");
|
||
}
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
if (checking) {
|
||
return (
|
||
<div className="max-w-3xl mx-auto text-center py-20">
|
||
<p className="meta">正在加载帖子…</p>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (!post) return null;
|
||
|
||
return (
|
||
<div className="max-w-3xl mx-auto">
|
||
<nav className="meta mb-5 flex items-center gap-1.5 flex-wrap text-[13px]">
|
||
<Link href="/" className="hover:text-[var(--accent)] transition-colors">首页</Link>
|
||
<ChevronRight size={13} />
|
||
<Link href={`/post/${post.id}`} className="hover:text-[var(--accent)] transition-colors truncate max-w-[12rem]">
|
||
帖子详情
|
||
</Link>
|
||
<ChevronRight size={13} />
|
||
<span style={{ color: "var(--accent)" }}>编辑</span>
|
||
</nav>
|
||
|
||
<div className="mb-7">
|
||
<h1 className="text-[26px] sm:text-[30px] font-extrabold tracking-tight" style={{ color: "var(--ink)" }}>
|
||
编辑帖子
|
||
</h1>
|
||
</div>
|
||
|
||
<form onSubmit={handleSubmit} className="panel p-5 sm:p-7 space-y-5">
|
||
{error && <div className="alert-error">{error}</div>}
|
||
|
||
<div>
|
||
<label className="field-label">标题</label>
|
||
<input
|
||
type="text"
|
||
value={title}
|
||
onChange={(e) => setTitle(e.target.value)}
|
||
required
|
||
maxLength={256}
|
||
className="field"
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="field-label">标签(逗号分隔,可选)</label>
|
||
<input
|
||
type="text"
|
||
value={tags}
|
||
onChange={(e) => setTags(e.target.value)}
|
||
placeholder="Go, PostgreSQL, 架构"
|
||
className="field"
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="field-label">正文(支持 Markdown)</label>
|
||
<textarea
|
||
value={content}
|
||
onChange={(e) => setContent(e.target.value)}
|
||
required
|
||
rows={14}
|
||
className="field"
|
||
/>
|
||
</div>
|
||
|
||
<div className="flex gap-3 pt-2">
|
||
<button
|
||
type="submit"
|
||
disabled={loading || !title.trim() || !content.trim()}
|
||
className="btn btn-primary"
|
||
>
|
||
{loading ? "保存中..." : "保存修改"}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => router.push(`/post/${post.id}`)}
|
||
className="btn btn-line"
|
||
>
|
||
取消
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
);
|
||
}
|