Update application code and dependencies
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
'use client'
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function DeleteNewsButton({ id }: { id: number }) {
|
||||
const router = useRouter()
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!confirm("确定要删除这条新闻吗?")) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/admin/news/${id}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
|
||||
if (res.ok) {
|
||||
router.refresh()
|
||||
}
|
||||
} catch (error) {
|
||||
alert("删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="text-red-600 hover:text-red-700 text-sm"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function NewsForm({
|
||||
news,
|
||||
}: {
|
||||
news?: {
|
||||
id?: number
|
||||
title: string
|
||||
content: string
|
||||
type?: string
|
||||
published?: boolean
|
||||
}
|
||||
}) {
|
||||
const router = useRouter()
|
||||
const [formData, setFormData] = useState({
|
||||
title: news?.title || "",
|
||||
content: news?.content || "",
|
||||
type: news?.type || "news",
|
||||
published: news?.published || false,
|
||||
})
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState("")
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setError("")
|
||||
|
||||
try {
|
||||
const url = news?.id
|
||||
? `/api/admin/news/${news.id}`
|
||||
: "/api/admin/news"
|
||||
|
||||
const method = news?.id ? "PUT" : "POST"
|
||||
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
if (res.ok) {
|
||||
router.push("/admin/news")
|
||||
} else {
|
||||
setError("保存失败")
|
||||
}
|
||||
} catch (err) {
|
||||
setError("保存失败,请稍后重试")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
标题
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
value={formData.title}
|
||||
onChange={(e) => setFormData({ ...formData, title: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="输入新闻标题"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
内容
|
||||
</label>
|
||||
<textarea
|
||||
required
|
||||
rows={6}
|
||||
value={formData.content}
|
||||
onChange={(e) => setFormData({ ...formData, content: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="输入新闻内容..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
类型
|
||||
</label>
|
||||
<select
|
||||
value={formData.type}
|
||||
onChange={(e) => setFormData({ ...formData, type: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="news">新闻</option>
|
||||
<option value="industry">行业洞察</option>
|
||||
<option value="cooperation">合作动态</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
状态
|
||||
</label>
|
||||
<select
|
||||
value={formData.published ? "true" : "false"}
|
||||
onChange={(e) => setFormData({ ...formData, published: e.target.value === "true" })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="true">立即发布</option>
|
||||
<option value="false">存为草稿</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-4">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 disabled:bg-blue-400"
|
||||
>
|
||||
{loading ? "保存中..." : "保存"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => window.history.back()}
|
||||
className="border border-gray-300 text-gray-700 px-6 py-2 rounded-lg hover:bg-gray-50"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import { prisma } from "@/app/lib/prisma"
|
||||
import NewsForm from "../../NewsForm"
|
||||
|
||||
export default async function EditNewsPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>
|
||||
}) {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const news = await prisma.news.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
})
|
||||
|
||||
if (!news) {
|
||||
redirect("/admin/news")
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">编辑新闻</h1>
|
||||
<Link href="/admin/news" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回列表
|
||||
</Link>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-3xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
|
||||
<NewsForm
|
||||
news={{
|
||||
id: news.id,
|
||||
title: news.title,
|
||||
content: news.content,
|
||||
type: news.type,
|
||||
published: news.published,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import NewsForm from "../NewsForm"
|
||||
|
||||
export default async function NewNewsPage() {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">添加新闻</h1>
|
||||
<Link href="/admin/news" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回列表
|
||||
</Link>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-3xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
|
||||
<NewsForm />
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import { prisma } from "@//app/lib/prisma"
|
||||
import Link from "next/link"
|
||||
import DeleteNewsButton from "./DeleteNewsButton"
|
||||
|
||||
export default async function AdminNewsPage() {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const news = await prisma.news.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">新闻管理</h1>
|
||||
<div className="flex items-center gap-4">
|
||||
<Link href="/admin" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回仪表盘
|
||||
</Link>
|
||||
<Link href="/admin/news/new" className="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-700">
|
||||
添加新闻
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-7xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 border-b border-gray-200">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">标题</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">类型</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">状态</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">日期</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{news.map((item) => (
|
||||
<tr key={item.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4">
|
||||
<div className="font-medium text-gray-900">{item.title}</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{item.type}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className={`inline-flex px-2 py-1 text-xs font-medium rounded-full ${
|
||||
item.published
|
||||
? "bg-green-100 text-green-700"
|
||||
: "bg-yellow-100 text-yellow-700"
|
||||
}`}>
|
||||
{item.published ? "已发布" : "草稿"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{new Date(item.createdAt).toLocaleDateString("zh-CN")}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Link
|
||||
href={`/admin/news/${item.id}/edit`}
|
||||
className="text-blue-600 hover:text-blue-700 text-sm"
|
||||
>
|
||||
编辑
|
||||
</Link>
|
||||
<DeleteNewsButton id={item.id} />
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{news.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
暂无新闻,<Link href="/admin/news/new" className="text-blue-600">点击添加</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user