mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 01:52:04 +00:00
fix(workorders): sync the whole saved row into the panel detail
Copying only the version left the cached detail with pre-save fields, so a queued second panel save could send them with a fresh version and silently revert what the first save wrote (e.g. POC name).
This commit is contained in:
parent
32b4ea0efa
commit
47cdca55c2
2 changed files with 49 additions and 8 deletions
|
|
@ -134,20 +134,19 @@ function readDetailRow(
|
||||||
return queryClient.getQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id))?.info;
|
return queryClient.getQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id))?.info;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Carry a saved row's version into the open panel's detail before the next queued patch runs. */
|
/** Carry a saved row into the open panel's detail before the next queued patch runs. */
|
||||||
function syncDetailRowVersion(
|
function syncDetailRow(queryClient: QueryClient, id: string | number, row: WorkOrderTableRow) {
|
||||||
queryClient: QueryClient,
|
|
||||||
id: string | number,
|
|
||||||
row: WorkOrderTableRow,
|
|
||||||
) {
|
|
||||||
queryClient.setQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id), (detail) =>
|
queryClient.setQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id), (detail) =>
|
||||||
detail && row.rowVersion
|
detail && row.rowVersion
|
||||||
? {
|
? {
|
||||||
...detail,
|
...detail,
|
||||||
|
// The whole saved row, not just its version: a queued patch built from fresh
|
||||||
|
// version + stale fields would silently revert what the previous save wrote.
|
||||||
info: {
|
info: {
|
||||||
...detail.info,
|
...detail.info,
|
||||||
rowVersion: row.rowVersion,
|
...row,
|
||||||
dispatchRowVersion: row.dispatchRowVersion || detail.info.dispatchRowVersion,
|
dispatchRowVersion: row.dispatchRowVersion || detail.info.dispatchRowVersion,
|
||||||
|
mediaCount: detail.info.mediaCount,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
: detail,
|
: detail,
|
||||||
|
|
@ -166,7 +165,7 @@ function recordSavedRow(
|
||||||
row: WorkOrderTableRow,
|
row: WorkOrderTableRow,
|
||||||
) {
|
) {
|
||||||
savedRows.set(String(id), row);
|
savedRows.set(String(id), row);
|
||||||
syncDetailRowVersion(queryClient, id, row);
|
syncDetailRow(queryClient, id, row);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runPatchAndSyncDetail(
|
async function runPatchAndSyncDetail(
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,12 @@ import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-ta
|
||||||
import { queryKeys } from "@/infra/query-key/query-key";
|
import { queryKeys } from "@/infra/query-key/query-key";
|
||||||
|
|
||||||
const patchBoardField = vi.fn();
|
const patchBoardField = vi.fn();
|
||||||
|
const updatePoc = vi.fn();
|
||||||
|
|
||||||
vi.mock("@/domain/work-orders/api/work-orders-api", () => ({
|
vi.mock("@/domain/work-orders/api/work-orders-api", () => ({
|
||||||
workOrdersApi: {
|
workOrdersApi: {
|
||||||
patchBoardField: (...args: unknown[]) => patchBoardField(...args),
|
patchBoardField: (...args: unknown[]) => patchBoardField(...args),
|
||||||
|
updatePoc: (...args: unknown[]) => updatePoc(...args),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -123,6 +125,46 @@ describe("useWorkOrderTableMutations for a work order outside the loaded list",
|
||||||
).toBe("saved-2");
|
).toBe("saved-2");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps the first save's fields when a queued second panel save patches the same group", async () => {
|
||||||
|
const client = makeClient();
|
||||||
|
client.setQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(9), {
|
||||||
|
info: { ...DETAIL_ROW, pocName: "Old POC", pocPhone: "111", pocNotes: "" },
|
||||||
|
} as WorkOrderBoardDetail);
|
||||||
|
let version = 0;
|
||||||
|
updatePoc.mockImplementation(
|
||||||
|
async (_id: unknown, payload: { pocName: string; pocPhone: string; pocNotes: string }) => {
|
||||||
|
version += 1;
|
||||||
|
return { ...DETAIL_ROW, ...payload, rowVersion: `saved-${version}` };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const onSecondSuccess = vi.fn();
|
||||||
|
const { result } = renderHook(
|
||||||
|
() =>
|
||||||
|
useWorkOrderTableMutations({
|
||||||
|
onPatch: vi.fn(),
|
||||||
|
clearPatch: vi.fn(),
|
||||||
|
getRow: () => undefined,
|
||||||
|
}),
|
||||||
|
{ wrapper: makeWrapper(client) },
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.patchField(9, { pocName: "New POC" }, { fallbackRow: DETAIL_ROW });
|
||||||
|
result.current.patchField(
|
||||||
|
9,
|
||||||
|
{ pocPhone: "555" },
|
||||||
|
{ fallbackRow: DETAIL_ROW, onSuccess: onSecondSuccess },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => expect(onSecondSuccess).toHaveBeenCalled());
|
||||||
|
expect(updatePoc.mock.calls[1][1]).toMatchObject({
|
||||||
|
pocName: "New POC",
|
||||||
|
pocPhone: "555",
|
||||||
|
workOrderVersion: "saved-1",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("chains the saved version when the panel's detail query never cached data", async () => {
|
it("chains the saved version when the panel's detail query never cached data", async () => {
|
||||||
let version = 0;
|
let version = 0;
|
||||||
patchBoardField.mockImplementation(async () => {
|
patchBoardField.mockImplementation(async () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue