feat: refactor quick replies page to use DashboardCrudPage component

- Removed redundant state management and effects from DashboardQuickRepliesPage.
- Integrated DashboardCrudPage for handling CRUD operations and filtering.
- Created a new DashboardCrudPage component to encapsulate common CRUD logic.
- Added utility functions for building and normalizing dashboard CRUD queries.
- Implemented tests for the new utility functions to ensure correctness.
This commit is contained in:
mlogclub
2026-05-28 08:39:14 +08:00
parent dd1b0d1b81
commit fe480ff131
7 changed files with 1054 additions and 987 deletions
@@ -0,0 +1,90 @@
import assert from "node:assert/strict"
import { describe, it } from "node:test"
import ts from "typescript"
import { readFile } from "node:fs/promises"
import vm from "node:vm"
function plain(value) {
return JSON.parse(JSON.stringify(value))
}
async function loadModule() {
const source = await readFile(
new URL("./dashboard-crud-utils.ts", import.meta.url),
"utf8"
)
const compiled = ts.transpileModule(source, {
compilerOptions: {
target: ts.ScriptTarget.ES2017,
module: ts.ModuleKind.CommonJS,
},
fileName: "dashboard-crud-utils.ts",
})
const sandbox = {
exports: {},
module: { exports: {} },
}
sandbox.exports = sandbox.module.exports
vm.runInNewContext(compiled.outputText, sandbox)
return sandbox.module.exports
}
describe("buildDashboardCrudQuery", () => {
it("trims text filters and omits empty values", async () => {
const { buildDashboardCrudQuery } = await loadModule()
const query = buildDashboardCrudQuery({
values: {
title: " hello ",
groupName: " ",
},
filters: [
{ name: "title", trim: true },
{ name: "groupName", trim: true },
],
page: 2,
limit: 50,
})
assert.deepEqual(plain(query), {
title: "hello",
page: 2,
limit: 50,
})
})
it("omits configured all values and parses numbers", async () => {
const { buildDashboardCrudQuery } = await loadModule()
const query = buildDashboardCrudQuery({
values: {
status: "all",
companyId: "42",
},
filters: [
{ name: "status", allValue: "all" },
{ name: "companyId", allValue: "0", valueType: "number" },
],
page: 1,
limit: 20,
})
assert.deepEqual(plain(query), {
companyId: 42,
page: 1,
limit: 20,
})
})
})
describe("normalizeDashboardCrudPageResult", () => {
it("returns a stable empty page when the API result is missing", async () => {
const { normalizeDashboardCrudPageResult } = await loadModule()
assert.deepEqual(plain(normalizeDashboardCrudPageResult(null, 3, 10)), {
results: [],
page: {
page: 3,
limit: 10,
total: 0,
},
})
})
})