fix(workorders): bind renamed service label to selected ID

This commit is contained in:
Alexandre Brandizzi 2026-09-30 20:45:40 -03:00
parent bb8d56d334
commit e5eed78fd2
2 changed files with 127 additions and 7 deletions

View file

@ -212,6 +212,41 @@ function getServiceOptionState(
};
}
function resolveRenamedPrimaryName({
selectedName,
selectedServiceId,
serviceIdsByName,
services,
previous,
pm,
serviceId,
}: {
selectedName: string | undefined;
selectedServiceId?: string | number | null;
serviceIdsByName: Record<string, string | number>;
services?: Service[];
previous: DialogTarget;
pm: string;
serviceId?: string | number | null;
}) {
const chosenService = services?.find(
(service) => String(service.id) === String(selectedServiceId),
);
const selectionTracksChosenId =
selectedName != null &&
selectedServiceId != null &&
String(serviceIdsByName[selectedName]) === String(selectedServiceId);
if (selectionTracksChosenId && chosenService != null) return chosenService.name;
const savedServiceUnchanged =
previous.serviceId != null && String(previous.serviceId) === String(serviceId);
const selectedSavedId = String(selectedServiceId ?? "") === String(serviceId ?? "");
const selectedSavedLabel = selectedName === previous.pm;
return savedServiceUnchanged && selectedSavedId && selectedSavedLabel && previous.pm !== pm
? pm
: undefined;
}
function useServiceNotesDraft({
open,
target,
@ -261,13 +296,25 @@ function useServiceNotesDraft({
setNote(serviceNotes ?? "");
setAveta(Boolean(avetaRequired));
setSearch("");
} else if (
previous.serviceId != null &&
String(previous.serviceId) === String(serviceId) &&
previous.pm !== pm
) {
setSel((current) => (current[0] === previous.pm ? [pm, ...current.slice(1)] : current));
setServiceIdsByName((current) => ({ ...current, [pm]: serviceId! }));
} else {
const renamedPrimary = resolveRenamedPrimaryName({
selectedName: sel[0],
selectedServiceId: primaryServiceId,
serviceIdsByName,
services,
previous,
pm,
serviceId,
});
if (renamedPrimary != null && renamedPrimary !== sel[0]) {
setSel((current) =>
current[0] === sel[0] ? [renamedPrimary, ...current.slice(1)] : current,
);
setServiceIdsByName((current) => ({
...current,
[renamedPrimary]: primaryServiceId ?? serviceId!,
}));
}
}
lastTarget.current = { key: targetKey, pm: targetPm, serviceId: targetServiceId };
}, [
@ -277,6 +324,9 @@ function useServiceNotesDraft({
targetServiceId,
pm,
serviceId,
sel,
primaryServiceId,
serviceIdsByName,
extraServices,
services,
serviceNotes,

View file

@ -729,4 +729,74 @@ describe("Aveta Service & Notes", () => {
expect.objectContaining({ pm: "Other service", serviceId: 702 }),
);
});
it("keeps the chosen identity when the saved same-label service is renamed", () => {
const onSave = vi.fn();
const common = {
open: true,
onOpenChange: vi.fn(),
extraServices: [],
serviceNotes: "Saved notes",
avetaRequired: false,
pmTypes: [],
serviceQuery: {
isLoading: false,
isError: false,
isFetching: false,
hasData: true,
retry: vi.fn(),
},
onSave,
};
const view = renderWithProviders(
<ServiceNotesDialog
{...common}
pm="Retired Service"
serviceId={1}
services={[serviceDefinition(2, "Retired Service"), serviceDefinition(3, "Extra Service")]}
/>,
{ withAuth: false },
);
fireEvent.click(screen.getByRole("button", { name: /Retired ServicePrimary/i }));
fireEvent.change(screen.getByPlaceholderText(/describe the specific issue/i), {
target: { value: "Draft note" },
});
fireEvent.click(screen.getByRole("checkbox", { name: /Requires Aveta documentation/i }));
fireEvent.click(screen.getByRole("button", { name: "Extra Service" }));
view.rerender(
<ServiceNotesDialog
{...common}
pm="Renamed historical service"
serviceId={1}
services={[serviceDefinition(2, "Retired Service"), serviceDefinition(3, "Extra Service")]}
/>,
);
expect(screen.getByRole("button", { name: /Retired ServicePrimary/i })).toBeInTheDocument();
view.rerender(
<ServiceNotesDialog
{...common}
pm="Renamed historical service"
serviceId={1}
services={[
serviceDefinition(2, "Renamed chosen service"),
serviceDefinition(3, "Extra Service"),
]}
/>,
);
expect(
screen.getByRole("button", { name: /Renamed chosen servicePrimary/i }),
).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({
pm: "Renamed chosen service",
serviceId: 2,
extraServices: ["Extra Service"],
serviceNotes: "Draft note",
avetaRequired: true,
}),
);
});
});