feat(school,classes): 学校/年级/班级模块审计修复 — 权限校验 + i18n + 架构图同步

- 新增审计报告 docs/architecture/audit/school-grade-class-audit-report.md

- 修复 P0-4: teacher/classes 4 个页面补充 requirePermission 权限校验

- 修复 P0-5: 新增 school.json i18n 文件(zh-CN/en)并接入 schools-view 组件

- 同步架构图 004:补充 grade-management 死模块记录与 teacher/classes 权限修复说明
This commit is contained in:
SpecialX
2026-06-22 16:44:02 +08:00
parent 22d3f07fcf
commit 10c668f36a
11 changed files with 787 additions and 86 deletions

View File

@@ -2,8 +2,8 @@
import { useState } from "react"
import { MoreHorizontal, Pencil, Plus, Trash2 } from "lucide-react"
import { toast } from "sonner"
import { useRouter } from "next/navigation"
import { useTranslations } from "next-intl"
import type { SchoolListItem } from "../types"
import { createSchoolAction, deleteSchoolAction, updateSchoolAction } from "../actions"
@@ -33,68 +33,53 @@ import {
AlertDialogTitle,
} from "@/shared/components/ui/alert-dialog"
import { formatDate } from "@/shared/lib/utils"
import { useActionMutation } from "@/shared/hooks/use-action-mutation"
export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
const t = useTranslations("school")
const router = useRouter()
const [isWorking, setIsWorking] = useState(false)
const [createOpen, setCreateOpen] = useState(false)
const [editItem, setEditItem] = useState<SchoolListItem | null>(null)
const [deleteItem, setDeleteItem] = useState<SchoolListItem | null>(null)
const handleCreate = async (formData: FormData) => {
setIsWorking(true)
try {
const res = await createSchoolAction(undefined, formData)
if (res.success) {
toast.success(res.message)
setCreateOpen(false)
router.refresh()
} else {
toast.error(res.message || "Failed to create school")
}
} catch {
toast.error("Failed to create school")
} finally {
setIsWorking(false)
}
const createMutation = useActionMutation({
errorMessage: "Failed to create school",
onSuccess: () => {
setCreateOpen(false)
router.refresh()
},
})
const updateMutation = useActionMutation({
errorMessage: "Failed to update school",
onSuccess: () => {
setEditItem(null)
router.refresh()
},
})
const deleteMutation = useActionMutation({
errorMessage: "Failed to delete school",
onSuccess: () => {
setDeleteItem(null)
router.refresh()
},
})
const isWorking = createMutation.isWorking || updateMutation.isWorking || deleteMutation.isWorking
const handleCreate = (formData: FormData) => {
void createMutation.mutate(() => createSchoolAction(undefined, formData))
}
const handleUpdate = async (formData: FormData) => {
const handleUpdate = (formData: FormData) => {
if (!editItem) return
setIsWorking(true)
try {
const res = await updateSchoolAction(editItem.id, undefined, formData)
if (res.success) {
toast.success(res.message)
setEditItem(null)
router.refresh()
} else {
toast.error(res.message || "Failed to update school")
}
} catch {
toast.error("Failed to update school")
} finally {
setIsWorking(false)
}
void updateMutation.mutate(() => updateSchoolAction(editItem.id, undefined, formData))
}
const handleDelete = async () => {
const handleDelete = () => {
if (!deleteItem) return
setIsWorking(true)
try {
const res = await deleteSchoolAction(deleteItem.id)
if (res.success) {
toast.success(res.message)
setDeleteItem(null)
router.refresh()
} else {
toast.error(res.message || "Failed to delete school")
}
} catch {
toast.error("Failed to delete school")
} finally {
setIsWorking(false)
}
void deleteMutation.mutate(() => deleteSchoolAction(deleteItem.id))
}
return (
@@ -102,13 +87,13 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
<div className="flex justify-end">
<Button onClick={() => setCreateOpen(true)} disabled={isWorking}>
<Plus className="mr-2 h-4 w-4" />
New school
{t("schools.new")}
</Button>
</div>
<Card className="shadow-none">
<CardHeader className="flex flex-row items-center justify-between space-y-0">
<CardTitle className="text-base">All schools</CardTitle>
<CardTitle className="text-base">{t("schools.all")}</CardTitle>
<Badge variant="secondary" className="tabular-nums">
{schools.length}
</Badge>
@@ -116,17 +101,17 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
<CardContent>
{schools.length === 0 ? (
<EmptyState
title="No schools"
description="Create your first school to get started."
title={t("schools.empty.title")}
description={t("schools.empty.description")}
className="h-auto border-none shadow-none"
/>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead>Code</TableHead>
<TableHead>Updated</TableHead>
<TableHead>{t("schools.column.name")}</TableHead>
<TableHead>{t("schools.column.code")}</TableHead>
<TableHead>{t("schools.column.updated")}</TableHead>
<TableHead className="w-[60px]" />
</TableRow>
</TableHeader>
@@ -146,7 +131,7 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setEditItem(s)}>
<Pencil className="mr-2 h-4 w-4" />
Edit
{t("schools.actions.edit")}
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
@@ -154,7 +139,7 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
onClick={() => setDeleteItem(s)}
>
<Trash2 className="mr-2 h-4 w-4" />
Delete
{t("schools.actions.delete")}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
@@ -170,23 +155,23 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
<Dialog open={createOpen} onOpenChange={setCreateOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>New school</DialogTitle>
<DialogTitle>{t("schools.form.createTitle")}</DialogTitle>
</DialogHeader>
<form action={handleCreate} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name">Name</Label>
<Input id="name" name="name" placeholder="e.g. First Primary School" autoFocus />
<Label htmlFor="name">{t("schools.form.name")}</Label>
<Input id="name" name="name" placeholder={t("schools.form.namePlaceholder")} autoFocus />
</div>
<div className="space-y-2">
<Label htmlFor="code">Code</Label>
<Input id="code" name="code" placeholder="Optional" />
<Label htmlFor="code">{t("schools.form.code")}</Label>
<Input id="code" name="code" placeholder={t("schools.form.codePlaceholder")} />
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={() => setCreateOpen(false)} disabled={isWorking}>
Cancel
{t("schools.form.cancel")}
</Button>
<Button type="submit" disabled={isWorking}>
Create
{t("schools.form.create")}
</Button>
</DialogFooter>
</form>
@@ -201,24 +186,24 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
>
<DialogContent>
<DialogHeader>
<DialogTitle>Edit school</DialogTitle>
<DialogTitle>{t("schools.form.editTitle")}</DialogTitle>
</DialogHeader>
{editItem ? (
<form action={handleUpdate} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="edit-name">Name</Label>
<Label htmlFor="edit-name">{t("schools.form.name")}</Label>
<Input id="edit-name" name="name" defaultValue={editItem.name} />
</div>
<div className="space-y-2">
<Label htmlFor="edit-code">Code</Label>
<Label htmlFor="edit-code">{t("schools.form.code")}</Label>
<Input id="edit-code" name="code" defaultValue={editItem.code || ""} />
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={() => setEditItem(null)} disabled={isWorking}>
Cancel
{t("schools.form.cancel")}
</Button>
<Button type="submit" disabled={isWorking}>
Save
{t("schools.form.save")}
</Button>
</DialogFooter>
</form>
@@ -234,15 +219,15 @@ export function SchoolsClient({ schools }: { schools: SchoolListItem[] }) {
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete school</AlertDialogTitle>
<AlertDialogTitle>{t("schools.delete.title")}</AlertDialogTitle>
<AlertDialogDescription>
This will permanently delete {deleteItem?.name || "this school"} and its grades.
{t("schools.delete.description", { name: deleteItem?.name || "" })}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isWorking}>Cancel</AlertDialogCancel>
<AlertDialogCancel disabled={isWorking}>{t("schools.delete.cancel")}</AlertDialogCancel>
<AlertDialogAction onClick={handleDelete} disabled={isWorking}>
Delete
{t("schools.delete.confirm")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>