Эх сурвалжийг харах

fix(web): 提前停机弹窗对齐订阅办理外壳并修复行内入口样式串扰

sy 9 цаг өмнө
parent
commit
43133d4c97

+ 22 - 12
web-scaffold/src/components/CustomerServiceStopAction.tsx

@@ -1,5 +1,7 @@
 import { useEffect, useId, useRef, useState } from "react";
 import { useNavigate } from "react-router";
+import { createPortal } from "react-dom";
+import { X } from "lucide-react";
 import { Button } from "./ui/button";
 import { useDemoIdentity } from "../lib/demo-session";
 import { ticketCreate } from "../lib/api/ticket";
@@ -41,6 +43,7 @@ export function CustomerServiceStopAction({ customerId, customerName, orderId, o
   const canSubmit = !identity || ["admin", "regAdmin"].includes(identity.role);
   const subjectName = customerName || customerId;
   const titleId = useId();
+  const reasonId = useId();
   const dialog = useRef<HTMLDialogElement>(null);
   const [open, setOpen] = useState(false);
   const [reason, setReason] = useState("");
@@ -83,23 +86,30 @@ export function CustomerServiceStopAction({ customerId, customerName, orderId, o
     <span title={canSubmit ? "建提前停机工单并登记待执行停机动作,实际停机由运维回填执行" : "仅运营侧账号可发起提前停机"}>
       <Button variant="destructive" size="sm" disabled={!canSubmit} onClick={() => { setReason(""); setError(""); setOpen(true); }}>发起提前停机</Button>
     </span>
-    <dialog ref={dialog} className="customer-account-dialog customer-stop-dialog" aria-labelledby={titleId} onCancel={(event) => { event.preventDefault(); if (!busy) setOpen(false); }} onClose={() => setOpen(false)}>
-      <form onSubmit={submit}>
-        <header><h2 id={titleId}>确认发起提前停机</h2></header>
-        <div className="customer-account-dialog-body">
-          <p>将为客户 <strong>{subjectName}</strong>{orderId && <> 的订阅 <strong>{orderId}</strong>{orderLabel && <>({orderLabel})</>}</>}创建提前停机工单。</p>
-          <p className="customer-account-state-note">实际停机以运维回填为准。订阅起止日和已出账期次保留。</p>
-          <label className="customer-team-field">
-            <span>停机原因 <b aria-hidden="true">*</b></span>
-            <textarea required disabled={busy} minLength={2} rows={3} placeholder="填写停机原因" value={reason} onChange={(event) => setReason(event.target.value)} />
-          </label>
-          {error && <p className="customer-account-error" role="alert">{error}</p>}
+    {/* 外壳与「全部订阅」的订阅办理弹窗同款(OrderChangeDialogs 的 Dialog):小标题+标题+关闭、正文可滚、底部按钮栏共用同一套样式。
+        ⚠️ 弹窗经 createPortal 挂到 body:本组件在表格行内 `.ops-inline-actions` 里渲染时,弹窗若留在单元格内,
+        会被 `.ops-inline-actions button`/`.ops-table td strong` 等祖先规则改写(按钮变文字链接、正文被拆行)。 */}
+    {createPortal(<dialog ref={dialog} className="customer-sub-dialog customer-stop-dialog" aria-labelledby={titleId} onCancel={(event) => { event.preventDefault(); if (!busy) setOpen(false); }} onClose={() => setOpen(false)}>
+      <header><div><span>订阅办理</span><h2 id={titleId}>确认发起提前停机</h2></div><button type="button" aria-label="关闭" disabled={busy} onClick={() => setOpen(false)}><X size={19} /></button></header>
+      <form className="customer-sub-form" onSubmit={submit}>
+        <div className="customer-sub-body">
+          <fieldset className="customer-sub-form-content" disabled={busy}>
+            <p className="customer-sub-context">将为客户 <strong>{subjectName}</strong>{orderId && <> 的订阅 <strong>{orderId}</strong>{orderLabel && <>({orderLabel})</>}</>}创建提前停机工单。</p>
+            <p className="customer-account-state-note">实际停机以运维回填为准。订阅起止日和已出账期次保留。</p>
+            <div className="customer-sub-fields">
+              <div className="customer-sub-field">
+                <div className="customer-sub-field-heading"><label htmlFor={reasonId}>停机原因<b aria-hidden="true"> *</b></label></div>
+                <textarea id={reasonId} required minLength={2} rows={3} placeholder="填写停机原因" value={reason} onChange={(event) => setReason(event.target.value)} />
+              </div>
+            </div>
+            {error && <p className="ops-form-error" role="alert">{error}</p>}
+          </fieldset>
         </div>
         <footer>
           <Button type="button" variant="outline" disabled={busy} onClick={() => setOpen(false)}>取消</Button>
           <Button type="submit" variant="destructive" disabled={busy}>{busy ? "提交中…" : "确认发起"}</Button>
         </footer>
       </form>
-    </dialog>
+    </dialog>, document.body)}
   </>;
 }

+ 99 - 0
web-scaffold/src/components/__tests__/customer-service-stop-dialog.test.tsx

@@ -0,0 +1,99 @@
+/**
+ * 「发起提前停机」弹窗:外壳与「全部订阅」的订阅办理弹窗同款(`customer-sub-dialog`)——
+ * 头部小标题+标题+关闭,正文=对象说明+提示+必填原因,底部=取消/确认发起。
+ * 只登记工单(`ticket/create`),实际停机由运维回填。
+ */
+import type { ReactNode } from "react";
+import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
+import { MemoryRouter } from "react-router";
+import { afterEach, beforeAll, beforeEach, afterAll, expect, it, vi } from "vitest";
+import { CustomerServiceStopAction } from "../CustomerServiceStopAction";
+
+type Props = Parameters<typeof CustomerServiceStopAction>[0];
+
+/** jsdom 未实现原生 `<dialog>` 的 `showModal()`/`close()`:与 operations-dialog/job-runs-dialog 测试同款最小桩。 */
+const dialogPrototype = HTMLDialogElement.prototype;
+const nativeDialog = { showModal: dialogPrototype.showModal, close: dialogPrototype.close };
+
+const { ticketCreate } = vi.hoisted(() => ({ ticketCreate: vi.fn(async () => ({ ticket_id: "TK-1" })) }));
+vi.mock("../../lib/demo-session", () => ({ useDemoIdentity: () => ({ role: "admin" }) }));
+vi.mock("../../lib/api/ticket", () => ({ ticketCreate }));
+
+beforeAll(() => {
+  Object.assign(dialogPrototype, {
+    showModal(this: HTMLDialogElement) { this.open = true; },
+    close(this: HTMLDialogElement) { this.open = false; },
+  });
+});
+beforeEach(() => { ticketCreate.mockClear(); });
+afterEach(cleanup);
+afterAll(() => { Object.assign(dialogPrototype, nativeDialog); });
+
+/** 弹窗经 createPortal 挂在 body 上(⛔ 留在触发表格行的单元格里):一律从 document 取。 */
+function findDialog() {
+  const dialog = document.querySelector("dialog.customer-stop-dialog");
+  if (!(dialog instanceof HTMLDialogElement)) throw new Error("弹窗未渲染");
+  return dialog;
+}
+
+/** `wrap` 可把入口包进表格行等真实容器,验证祖先样式不会漏进弹窗。 */
+function openDialog(props: Partial<Props> = {}, wrap: (node: ReactNode) => ReactNode = (node) => node) {
+  render(<MemoryRouter>{wrap(<CustomerServiceStopAction customerId="C-1" customerName="客户甲" orderId="SUB-9" orderLabel="在用" {...props} />)}</MemoryRouter>);
+  fireEvent.click(screen.getByRole("button", { name: "发起提前停机" }));
+  return findDialog();
+}
+
+it("在表格行内渲染时弹窗挂到 body,⛔ 留在单元格里被 .ops-inline-actions/td 的祖先规则改写", () => {
+  const dialog = openDialog({}, (node) => <table className="ops-table"><tbody><tr><td><div className="ops-inline-actions">{node}</div></td></tr></tbody></table>);
+  expect(dialog.parentElement).toBe(document.body);
+  expect(document.querySelector(".ops-inline-actions dialog")).toBeNull();
+  expect(document.querySelector(".ops-inline-actions button")?.textContent).toBe("发起提前停机");
+});
+
+it("外壳与订阅办理弹窗同款:小标题、标题、关闭,底部依次是取消和确认发起", () => {
+  const dialog = openDialog();
+  expect(dialog.open).toBe(true);
+  expect(dialog.classList.contains("customer-sub-dialog")).toBe(true);
+  expect(within(dialog).getByText("订阅办理")).toBeTruthy();
+  expect(within(dialog).getByRole("heading", { name: "确认发起提前停机" })).toBeTruthy();
+  expect(within(dialog).getByRole("button", { name: "关闭" })).toBeTruthy();
+  expect(dialog.querySelector(".customer-sub-context")?.textContent).toBe("将为客户 客户甲 的订阅 SUB-9(在用)创建提前停机工单。");
+  expect(within(dialog).getByText("实际停机以运维回填为准。订阅起止日和已出账期次保留。")).toBeTruthy();
+  const buttons = within(dialog.querySelector("footer")!).getAllByRole("button");
+  expect(buttons.map((button) => button.textContent)).toEqual(["取消", "确认发起"]);
+});
+
+it.each(["取消", "关闭"])("点「%s」关闭弹窗,不建单", (name) => {
+  const dialog = openDialog();
+  fireEvent.click(within(dialog).getByRole("button", { name }));
+  expect(dialog.open).toBe(false);
+  expect(ticketCreate).not.toHaveBeenCalled();
+});
+
+it("原因不足 2 个字 ⇒ 报错且不建单", () => {
+  const dialog = openDialog();
+  fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "短" } });
+  fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
+  expect(within(dialog).getByRole("alert").textContent).toBe("请填写至少 2 个字的停机原因。");
+  expect(ticketCreate).not.toHaveBeenCalled();
+});
+
+it("未取到订单号 ⇒ 报错且不建单(⛔ 按客户整户停用)", () => {
+  const dialog = openDialog({ orderId: undefined });
+  fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "设备故障,需提前停机" } });
+  fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
+  expect(within(dialog).getByRole("alert").textContent).toBe("未取到本条订单号:提前停机单必须挂在具体订阅上。");
+  expect(ticketCreate).not.toHaveBeenCalled();
+});
+
+it("原因合规 ⇒ 按本条订阅建提前停机工单并关窗", async () => {
+  const dialog = openDialog();
+  fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "设备故障,需提前停机" } });
+  fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
+  await waitFor(() => expect(ticketCreate).toHaveBeenCalledTimes(1));
+  expect(ticketCreate).toHaveBeenCalledWith({
+    ticket_type: "service_stop", customer_id: "C-1", subscription_id: "SUB-9",
+    title: "客户甲 提前停机", detail: "设备故障,需提前停机", confirm: true,
+  });
+  await waitFor(() => expect(dialog.open).toBe(false));
+});

+ 9 - 28
web-scaffold/src/styles/operations.css

@@ -629,7 +629,7 @@ body:has(.ops-content-dialog[open]) { overflow: hidden; }
 .customer-account-head-actions button { color:var(--foreground); }
 .customer-account-panel { display:grid;gap:12px;margin-top:17px;padding:16px;border:1px solid var(--primary-border);border-radius:8px;background:var(--primary-soft); }
 .customer-account-panel h3 { margin:0;font-size:var(--type-body); }
-.customer-account-panel p,.customer-account-dialog-body p { margin:0;color:var(--text-secondary);font-size:var(--type-support);line-height:1.55; }
+.customer-account-panel p { margin:0;color:var(--text-secondary);font-size:var(--type-support);line-height:1.55; }
 .customer-account-form-grid { display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px 15px; }
 .customer-account-form-grid label { display:grid;align-content:start;gap:6px;min-width:0;color:var(--field-label);font-size:var(--type-support);font-weight:400; }
 .customer-account-form-grid label>span { display:inline-flex;align-items:baseline;gap:3px;white-space:nowrap; }
@@ -645,31 +645,12 @@ body:has(.ops-content-dialog[open]) { overflow: hidden; }
 .customer-claim-result span { color:var(--text-secondary);font-size:var(--type-support); }
 .customer-claim-result strong { color:var(--foreground);font:700 var(--type-body) var(--font-mono);letter-spacing:.09em; }
 .customer-claim-result p { flex-basis:100%;margin:0;color:var(--text-secondary);font-size:var(--type-support);line-height:1.5; }
-.customer-account-error { color:var(--destructive)!important;font-weight:650; }
-.customer-account-dialog { width:min(620px,calc(100vw - 28px));max-height:min(88vh,760px);margin:auto;padding:0;border:1px solid var(--border);border-radius:12px;background:var(--card);color:var(--foreground);box-shadow:var(--shadow-dialog); }
-.customer-account-dialog::backdrop { background:var(--overlay); }
-.customer-account-dialog>form>header { display:flex;align-items:flex-start;justify-content:space-between;gap:12px;padding:18px 22px;border-bottom:1px solid var(--border); }
-.customer-account-dialog>form>header span { color:var(--primary);font-size:var(--type-support);font-weight:700; }
-.customer-account-dialog>form>header h2 { margin:4px 0 0;font-size:var(--type-section); }
-.customer-account-dialog>form>header button { display:grid;place-items:center;width:30px;height:30px;border:0;border-radius:5px;background:transparent;color:var(--text-secondary);cursor:pointer; }
-.customer-account-dialog>form>header button:hover { background:var(--surface-subtle); }
-.customer-account-dialog-body { display:grid;gap:14px;padding:20px 22px; }
-.customer-account-dialog>form>footer { display:flex;justify-content:flex-end;gap:8px;padding:14px 22px;border-top:1px solid var(--border); }
-.customer-stop-dialog .customer-account-dialog-body { color: var(--text-secondary); font-size: var(--type-support); line-height: 1.55; overflow-wrap: anywhere; }
-.customer-stop-dialog .customer-team-field { display: grid; gap: 6px; color: var(--field-label); font-size: var(--type-support); font-weight: 400; }
-.customer-stop-dialog .customer-team-field b { color: var(--destructive); }
-.customer-stop-dialog textarea {
-  width: 100%;
-  padding: 8px 10px;
-  border: 1px solid var(--border);
-  border-radius: var(--radius-control);
-  background: var(--card);
-  color: var(--foreground);
-  font: inherit;
-  resize: vertical;
-}
-.customer-stop-dialog textarea::placeholder { color: var(--muted-foreground); }
-.customer-stop-dialog textarea:focus-visible { outline: none; border-color: var(--ring); box-shadow: 0 0 0 3px var(--ring-glow); }
+/* 提前停机弹窗:沿用「订阅办理」弹窗外壳(.customer-sub-dialog)——头部/正文/底部按钮同一套;正文只有一个必填原因 ⇒ 宽度收窄、字段通栏。 */
+.customer-stop-dialog { width:min(620px,calc(100vw - 24px)); }
+.customer-stop-dialog .customer-sub-field { grid-column:1/-1; }
+.customer-stop-dialog .customer-account-state-note { margin:0 0 14px;font-size:var(--type-support); }
+.customer-stop-dialog .ops-form-error { margin:14px 0 0; }
+.customer-stop-dialog textarea::placeholder { color:var(--muted-foreground); }
 .customer-file-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 9px; }
 .customer-file-grid > div { display: grid; grid-template-columns: 18px 1fr; gap: 3px 9px; padding: 12px; border: 1px solid var(--border); border-radius: 8px; font-size: var(--type-body); }
 .customer-file-grid svg { color: var(--primary); }
@@ -682,7 +663,7 @@ body:has(.ops-content-dialog[open]) { overflow: hidden; }
 .customer-timeline small { grid-column: 3; }
 @media (max-width: 1050px) { .customer-profile-grid { grid-template-columns: 1fr; } }
 @media (max-width: 700px) { .customer-hero { flex-wrap: wrap; padding: 18px; } .customer-hero-actions { width: 100%; } .customer-metric-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); } .customer-metric-strip > div:nth-child(2) { border-right: 0; } .customer-metric-strip > div:nth-child(n+3) { border-top: 1px solid var(--border); } .customer-profile-grid .customer-fact-grid, .customer-attribution-form { grid-template-columns: 1fr; } .customer-master-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); } .customer-section { padding: 16px; } .customer-profile-grid--lower .customer-timeline > div { grid-template-columns: 1fr; } .customer-profile-grid--lower .customer-timeline p, .customer-profile-grid--lower .customer-timeline small { grid-column: 1; } }
-@media (max-width: 600px) { .customer-account-form-grid { grid-template-columns:1fr; } .customer-account-form-wide { grid-column:auto; } .customer-account-head-actions { justify-content:flex-start; } .customer-account-dialog-body { padding:16px; } }
+@media (max-width: 600px) { .customer-account-form-grid { grid-template-columns:1fr; } .customer-account-form-wide { grid-column:auto; } .customer-account-head-actions { justify-content:flex-start; } }
 .ops-form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 18px; }
 .ops-form-grid label { display: flex; flex-direction: column; gap: 6px; min-width: 0; color: var(--field-label); font-size: var(--type-support); font-weight: 400; }
 .ops-form-grid label b { color: var(--destructive); }
@@ -1136,7 +1117,7 @@ body:has(.ops-content-dialog[open]) { overflow: hidden; }
   background-position: right 10px center;
 }
 
-.ops-form-grid textarea {
+:is(.ops-form-grid, .customer-sub-fields) textarea {
   width: 100%;
   min-width: 0;
   height: auto;