Update application code and dependencies
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function CategoryForm({
|
||||
category,
|
||||
}: {
|
||||
category?: {
|
||||
id?: number
|
||||
name: string
|
||||
icon?: string
|
||||
description?: string
|
||||
}
|
||||
}) {
|
||||
const router = useRouter()
|
||||
const [formData, setFormData] = useState({
|
||||
name: category?.name || "",
|
||||
icon: category?.icon || "",
|
||||
description: category?.description || "",
|
||||
})
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState("")
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setError("")
|
||||
|
||||
try {
|
||||
const url = category?.id
|
||||
? `/api/admin/categories/${category.id}`
|
||||
: "/api/admin/categories"
|
||||
|
||||
const method = category?.id ? "PUT" : "POST"
|
||||
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
if (res.ok) {
|
||||
router.push("/admin/categories")
|
||||
} 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.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: 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">
|
||||
图标 (Emoji)
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.icon}
|
||||
onChange={(e) => setFormData({ ...formData, icon: 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
|
||||
rows={4}
|
||||
value={formData.description}
|
||||
onChange={(e) => setFormData({ ...formData, description: 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="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,34 @@
|
||||
'use client'
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function DeleteCategoryButton({ id }: { id: number }) {
|
||||
const router = useRouter()
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!confirm("确定要删除这个分类吗?关联的智能体会变为无分类状态。")) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/admin/categories/${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,52 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import { prisma } from "@/app/lib/prisma"
|
||||
import CategoryForm from "../../CategoryForm"
|
||||
|
||||
export default async function EditCategoryPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>
|
||||
}) {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const category = await prisma.category.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
})
|
||||
|
||||
if (!category) {
|
||||
redirect("/admin/categories")
|
||||
}
|
||||
|
||||
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/categories" 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">
|
||||
<CategoryForm
|
||||
category={{
|
||||
id: category.id,
|
||||
name: category.name,
|
||||
icon: category.icon || "",
|
||||
description: category.description || "",
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import CategoryForm from "../CategoryForm"
|
||||
|
||||
export default async function NewCategoryPage() {
|
||||
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/categories" 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">
|
||||
<CategoryForm />
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import { prisma } from "@//app/lib/prisma"
|
||||
import Link from "next/link"
|
||||
import DeleteCategoryButton from "./DeleteCategoryButton"
|
||||
|
||||
export default async function AdminCategoriesPage() {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const categories = await prisma.category.findMany({
|
||||
include: { _count: { select: { agents: true } } },
|
||||
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/categories/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-right text-xs font-medium text-gray-500 uppercase">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{categories.map((cat) => (
|
||||
<tr key={cat.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4">
|
||||
<div className="font-medium text-gray-900">{cat.name}</div>
|
||||
<div className="text-sm text-gray-500">{cat.description}</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-2xl">
|
||||
{cat.icon || "-"}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{cat._count.agents}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Link
|
||||
href={`/admin/categories/${cat.id}/edit`}
|
||||
className="text-blue-600 hover:text-blue-700 text-sm"
|
||||
>
|
||||
编辑
|
||||
</Link>
|
||||
<DeleteCategoryButton id={cat.id} />
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{categories.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
暂无分类,<Link href="/admin/categories/new" className="text-blue-600">点击添加</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user