feat(portal-shell): 教师域考试管理页面迁移(P2)
按 ARCHITECTURE.md §9.1/§10 P2 要求,迁移教师域 exams 模块: - 列表页 /shell/teacher/exams(ListPageShell + URL 状态 + 客户端二次筛选) - 详情页 /shell/teacher/exams/[id](DetailPageShell + 真实 exam(id) 查询) - 新建页 /shell/teacher/exams/new(FormPageShell + MSW 兜底) - 纯函数 transformations.ts + 19 个 vitest 单测 - @contract-pending:exams(classId) 列表查询、createExam mutation 走 MSW - 三态 UI(loading/error/empty)+ 路由级 loading.tsx/error.tsx - i18n:zh-CN/en 双语补全,无硬编码中文 - MSW handlers 支持 variables 透传 §11.3 DoD 验收: - lint: 0 errors(4 个 __generated__ 预存警告) - typecheck: 0 errors - test: 250/250 passed(含 19 个新增 transformations 测试) - lint:tokens: 0 errors
This commit is contained in:
@@ -245,6 +245,67 @@ const mockTextbooks = [
|
||||
},
|
||||
];
|
||||
|
||||
// ── Exams 域(@contract-pending,schema 无 exams 列表/createExam mutation)
|
||||
// 用于 /shell/teacher/exams 列表页 + /new 表单页 MSW 兜底
|
||||
const mockExams = [
|
||||
{
|
||||
id: "exam-001",
|
||||
classId: "cls-001",
|
||||
subjectId: "sub-math",
|
||||
title: "2026 春季期中考试",
|
||||
description: "覆盖集合、函数、基本初等函数",
|
||||
examDate: "2026-04-15T09:00:00Z",
|
||||
duration: 120,
|
||||
totalScore: "100",
|
||||
status: "SCORED",
|
||||
createdAt: "2026-04-01T00:00:00Z",
|
||||
},
|
||||
{
|
||||
id: "exam-002",
|
||||
classId: "cls-001",
|
||||
subjectId: "sub-math",
|
||||
title: "2026 春季期末考试",
|
||||
description: "全册内容",
|
||||
examDate: "2026-07-20T09:00:00Z",
|
||||
duration: 120,
|
||||
totalScore: "150",
|
||||
status: "IN_PROGRESS",
|
||||
createdAt: "2026-07-10T00:00:00Z",
|
||||
},
|
||||
{
|
||||
id: "exam-003",
|
||||
classId: "cls-001",
|
||||
subjectId: "sub-math",
|
||||
title: "单元测验 - 集合",
|
||||
description: null,
|
||||
examDate: "2026-07-25T14:00:00Z",
|
||||
duration: 45,
|
||||
totalScore: "50",
|
||||
status: "DRAFT",
|
||||
createdAt: "2026-07-22T00:00:00Z",
|
||||
},
|
||||
];
|
||||
|
||||
// ── Exam 单查 mock(与 combined-schema Exam 类型字段对齐)
|
||||
// 用于 /shell/teacher/exams/[id] 详情页 MSW 兜底
|
||||
const mockExamDetail = {
|
||||
id: "exam-001",
|
||||
classId: "cls-001",
|
||||
subjectId: "sub-math",
|
||||
title: "2026 春季期中考试",
|
||||
description: "覆盖集合、函数、基本初等函数",
|
||||
examDate: "2026-04-15T09:00:00Z",
|
||||
duration: 120,
|
||||
totalScore: "100",
|
||||
status: "SCORED",
|
||||
statusChangedAt: "2026-04-16T10:00:00Z",
|
||||
statusChangedBy: "usr-teacher-001",
|
||||
schoolId: "sch-001",
|
||||
createdBy: "usr-teacher-001",
|
||||
createdAt: "2026-04-01T00:00:00Z",
|
||||
updatedAt: "2026-04-16T10:00:00Z",
|
||||
};
|
||||
|
||||
const mockGrades = [
|
||||
{
|
||||
student_id: "stu-001",
|
||||
@@ -275,10 +336,26 @@ const mockGrades = [
|
||||
// ── GraphQL Response ───────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* 根据 operationName 返回 mock GraphQL 响应。
|
||||
* GraphQL 请求体结构(用于 MSW handler 与 route.ts 透传)。
|
||||
*/
|
||||
export interface GraphQLRequestBody {
|
||||
operationName?: string;
|
||||
variables?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 operationName + variables 返回 mock GraphQL 响应。
|
||||
*
|
||||
* Exams 域扩展:
|
||||
* - GetExam($id):返回 mockExamDetail(任意 id 都返回同一条,dev 兜底用)
|
||||
* - GetExams($classId):返回 mockExams(按 classId 过滤,未指定返回全部)
|
||||
* - CreateExam($input):返回新生成的 id(基于时间戳)
|
||||
*
|
||||
* 关联:ARCHITECTURE.md §5.4 / §9.1 / §11.4 契约工单
|
||||
*/
|
||||
export function graphqlResponse(
|
||||
operationName: string | undefined,
|
||||
variables?: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
switch (operationName) {
|
||||
// ── Dashboard 域 ──
|
||||
@@ -408,6 +485,62 @@ export function graphqlResponse(
|
||||
},
|
||||
};
|
||||
|
||||
// ── Exams 域(教师域 P2 迁移,@contract-pending)──
|
||||
// GetExam($id):按 id 单查,任意 id 都返回同一条(dev 兜底)
|
||||
case "GetExam": {
|
||||
const examId = (variables?.id as string | undefined) ?? "";
|
||||
// 若请求的是 mockExams 中的某一条,返回对应数据;否则返回 mockExamDetail
|
||||
const found = mockExams.find((e) => e.id === examId) ?? mockExamDetail;
|
||||
return {
|
||||
data: {
|
||||
exam: {
|
||||
...found,
|
||||
statusChangedAt: "2026-04-16T10:00:00Z",
|
||||
statusChangedBy: "usr-teacher-001",
|
||||
schoolId: "sch-001",
|
||||
createdBy: "usr-teacher-001",
|
||||
updatedAt: "2026-04-16T10:00:00Z",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
// GetExams($classId):列表查询,按 classId 过滤(未指定返回全部)
|
||||
case "GetExams": {
|
||||
const classId = variables?.classId as string | undefined;
|
||||
const status = variables?.status as string | undefined;
|
||||
const filtered = mockExams.filter(
|
||||
(e) =>
|
||||
(!classId || e.classId === classId) &&
|
||||
(!status || e.status === status),
|
||||
);
|
||||
return {
|
||||
data: {
|
||||
exams: {
|
||||
items: filtered,
|
||||
total: filtered.length,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
// CreateExam($input):mutation 兜底,返回基于时间戳的新 id
|
||||
case "CreateExam": {
|
||||
const input = (variables?.input ?? {}) as Record<string, unknown>;
|
||||
const newId = `exam-${Date.now()}`;
|
||||
mockExams.push({
|
||||
id: newId,
|
||||
classId: (input.classId as string) ?? "cls-001",
|
||||
subjectId: (input.subjectId as string) ?? "sub-math",
|
||||
title: (input.title as string) ?? "未命名考试",
|
||||
description: (input.description as string | null) ?? null,
|
||||
examDate: (input.examDate as string) ?? new Date().toISOString(),
|
||||
duration: (input.duration as number) ?? 120,
|
||||
totalScore: String((input.totalScore as number) ?? 100),
|
||||
status: "DRAFT",
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
return { data: { createExam: { id: newId } } };
|
||||
}
|
||||
|
||||
// ── Grades 域(预留) ──
|
||||
case "GetGrades":
|
||||
return { data: { grades: mockGrades } };
|
||||
|
||||
@@ -24,14 +24,24 @@ const APOLLO_ROUTER_GRAPHQL =
|
||||
export const handlers = [
|
||||
// GraphQL 同域代理(客户端 Apollo Client)
|
||||
http.post("/api/graphql", async ({ request }) => {
|
||||
const body = (await request.json()) as { operationName?: string };
|
||||
return HttpResponse.json(graphqlResponse(body.operationName));
|
||||
const body = (await request.json()) as {
|
||||
operationName?: string;
|
||||
variables?: Record<string, unknown>;
|
||||
};
|
||||
return HttpResponse.json(
|
||||
graphqlResponse(body.operationName, body.variables),
|
||||
);
|
||||
}),
|
||||
|
||||
// GraphQL SSR 直连兜底(msw/node server 拦截 RSC 端 fetch)
|
||||
http.post(APOLLO_ROUTER_GRAPHQL, async ({ request }) => {
|
||||
const body = (await request.json()) as { operationName?: string };
|
||||
return HttpResponse.json(graphqlResponse(body.operationName));
|
||||
const body = (await request.json()) as {
|
||||
operationName?: string;
|
||||
variables?: Record<string, unknown>;
|
||||
};
|
||||
return HttpResponse.json(
|
||||
graphqlResponse(body.operationName, body.variables),
|
||||
);
|
||||
}),
|
||||
|
||||
// 登录兜底(DEV_MODE 使用)
|
||||
|
||||
Reference in New Issue
Block a user