Explorar el Código

fix(web): 修复全部订阅状态页签切换

sy hace 3 días
padre
commit
96a6306192

+ 8 - 8
web-scaffold/src/pages/OrderManagementPage.tsx

@@ -67,11 +67,12 @@ export function OrderManagementPage() {
   const columnWidths = [.21, .23, .14, .09, .12, .10, .11].map((weight) => contentWidth * weight).concat([104, 96]);
   const navigate = useNavigate();
   const [params, setParams] = useSearchParams();
-  function filter(key: string, value: string) {
+  function updateQuery(key: string, value: string) {
     setParams((current) => {
       const next = new URLSearchParams(current);
       if (value) next.set(key, value);
       else next.delete(key);
+      if (key !== "page") next.delete("page");
       return next;
     }, { replace: true });
   }
@@ -114,9 +115,8 @@ export function OrderManagementPage() {
   const paging = useLocalPage(serverTotal, LIST_PAGE_SIZE, pageFromUrl);
   const setPage = (next: number) => {
     const safe = Math.max(0, next);
-    filter("page", safe > 0 ? String(safe) : "");
+    updateQuery("page", safe > 0 ? String(safe) : "");
   };
-  const resetPage = () => filter("page", "");
   const exits = useMemo(
     () => (exitsLoad.data?.items ?? []).map((item) => item.exit_name).filter(Boolean).sort(),
     [exitsLoad.data],
@@ -149,13 +149,13 @@ export function OrderManagementPage() {
 
     <div className="order-list-shell list-shell-fill">
       <div className="order-tabs" role="group" aria-label="按订阅状态筛选">
-        {statuses.map((item) => <button key={item} type="button" aria-pressed={status === item} className={status === item ? "active" : ""} onClick={() => { filter("status", item === "全部状态" ? "" : item); resetPage(); }}>{item}<span>{tabCount(item)}</span></button>)}
+        {statuses.map((item) => <button key={item} type="button" aria-pressed={status === item} className={status === item ? "active" : ""} onClick={() => updateQuery("status", item === "全部状态" ? "" : item)}>{item}<span>{tabCount(item)}</span></button>)}
       </div>
       <div className="ops-toolbar order-filters">
-        <label className="ops-search order-search"><Search size={17} /><Input aria-label="搜索订阅" placeholder="搜索客户、套餐、合同、订单号或出口" title="服务端检索套餐名/订阅号/订单号/客户名/合同号" value={search} onChange={(e) => { filter("q", e.target.value); resetPage(); }} />{search && <button type="button" aria-label="清空搜索" onClick={() => { filter("q", ""); resetPage(); }}><X size={15} /></button>}</label>
-        <FilterSelect icon="type" aria-label="套餐类型筛选" value={planKind} onChange={(e) => { filter("plan", e.target.value === "全部类型" ? "" : e.target.value); resetPage(); }}><option>全部类型</option><option>固定套餐</option><option>自定义套餐</option></FilterSelect>
-        <FilterSelect icon="exit" aria-label="线路出口筛选" value={exitFilter} onChange={(e) => { filter("exit", e.target.value); resetPage(); }}><option value="">全部线路出口</option>{exits.map((exit) => <option key={exit}>{exit}</option>)}</FilterSelect>
-        <Button variant={expiringOnly ? "default" : "outline"} size="sm" aria-pressed={expiringOnly} title={`只看订阅止期在 ${EXPIRING_SOON_DAYS} 天内的订阅(已到期的不算);续签入口在合同管理与客户档案`} onClick={() => { filter("expiring", expiringOnly ? "" : "1"); resetPage(); }}><CalendarClock size={15} />快到期 {EXPIRING_SOON_DAYS} 天内{expiringCount ? `(${expiringCount})` : ""}</Button>
+        <label className="ops-search order-search"><Search size={17} /><Input aria-label="搜索订阅" placeholder="搜索客户、套餐、合同、订单号或出口" title="服务端检索套餐名/订阅号/订单号/客户名/合同号" value={search} onChange={(e) => updateQuery("q", e.target.value)} />{search && <button type="button" aria-label="清空搜索" onClick={() => updateQuery("q", "")}><X size={15} /></button>}</label>
+        <FilterSelect icon="type" aria-label="套餐类型筛选" value={planKind} onChange={(e) => updateQuery("plan", e.target.value === "全部类型" ? "" : e.target.value)}><option>全部类型</option><option>固定套餐</option><option>自定义套餐</option></FilterSelect>
+        <FilterSelect icon="exit" aria-label="线路出口筛选" value={exitFilter} onChange={(e) => updateQuery("exit", e.target.value)}><option value="">全部线路出口</option>{exits.map((exit) => <option key={exit}>{exit}</option>)}</FilterSelect>
+        <Button variant={expiringOnly ? "default" : "outline"} size="sm" aria-pressed={expiringOnly} title={`只看订阅止期在 ${EXPIRING_SOON_DAYS} 天内的订阅(已到期的不算);续签入口在合同管理与客户档案`} onClick={() => updateQuery("expiring", expiringOnly ? "" : "1")}><CalendarClock size={15} />快到期 {EXPIRING_SOON_DAYS} 天内{expiringCount ? `(${expiringCount})` : ""}</Button>
         {hasFilter && <Button variant="ghost" size="sm" onClick={resetFilters}>重置筛选</Button>}
       </div>
       <div ref={tableOverflow.ref} tabIndex={0} role="region" aria-label="全部订阅,可横向滚动查看字段" className={`measured-table-scroll order-table-wrap order-ledger-scroll${tableOverflow.overflow ? " has-horizontal-overflow" : ""}${tableOverflow.canScrollLeft ? " can-scroll-left" : ""}${tableOverflow.canScrollRight ? " can-scroll-right" : ""}`}><table className="order-table order-ledger"><OpsTableColumns widths={columnWidths} /><thead><tr><th>企业 / 订阅编号</th><th>套餐与合同</th><th title="运营商订单号=保存时冻结合同外部单号;合同未填或未冻结时显示「—」">运营商订单号</th><th>线路出口</th><th>年订阅价格</th><th>合同周期</th><th>实际开通</th><th>状态</th><th>操作</th></tr></thead><tbody>

+ 42 - 0
web-scaffold/src/pages/__tests__/order-management-filters.test.tsx

@@ -0,0 +1,42 @@
+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { MemoryRouter } from "react-router";
+import { afterEach, expect, it, vi } from "vitest";
+import { orderListAll } from "../../lib/api/bos";
+import { LIST_PAGE_SIZE } from "../../lib/list-paging";
+import { OrderManagementPage } from "../OrderManagementPage";
+
+vi.mock("../../lib/api/bos", async (load) => ({
+  ...await load<typeof import("../../lib/api/bos")>(),
+  orderListAll: vi.fn(async () => ({ items: [], total: 80 })),
+  lineExitGetList: vi.fn(async () => ({ items: [{ exit_name: "香港" }] })),
+}));
+vi.mock("../../lib/use-horizontal-overflow", () => ({ useHorizontalOverflow: () => ({ ref: null, width: 1460 }) }));
+
+afterEach(cleanup);
+
+function lastLedgerRequest() {
+  return vi.mocked(orderListAll).mock.calls.filter(([query]) => query?.page_size === LIST_PAGE_SIZE).at(-1)?.[0];
+}
+
+it("状态页签切换时重置分页,保留其他筛选,并将新状态传给列表接口", async () => {
+  const query = new URLSearchParams({ page: "2", q: "科技", plan: "固定套餐", exit: "香港" });
+  render(<MemoryRouter initialEntries={[`/orders?${query}`]}><OrderManagementPage /></MemoryRouter>);
+  await waitFor(() => expect(lastLedgerRequest()).toMatchObject({ page: 2 }));
+
+  const active = screen.getByRole("button", { name: /^生效中/ });
+  fireEvent.click(active);
+  expect(active.getAttribute("aria-pressed")).toBe("true");
+  await waitFor(() => expect(lastLedgerRequest()).toMatchObject({
+    page: 0, status: "active", keyword: "科技", plan_kind: "fixed", line_exit: "香港",
+  }));
+
+  fireEvent.click(screen.getByRole("button", { name: "下一页" }));
+  await waitFor(() => expect(lastLedgerRequest()).toMatchObject({ page: 1, status: "active" }));
+
+  const all = screen.getByRole("button", { name: /^全部状态/ });
+  fireEvent.click(all);
+  expect(all.getAttribute("aria-pressed")).toBe("true");
+  await waitFor(() => expect(lastLedgerRequest()).toMatchObject({
+    page: 0, status: undefined, keyword: "科技", plan_kind: "fixed", line_exit: "香港",
+  }));
+});