mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-04 18:22:05 +00:00
fix(work-orders): clear stale POCs and guard site-dialog draft state
Clear additional contacts on site change, omit additionalContacts from site-only patches unless dirty or known from GET, and dirty-guard Query refetch while editing.
This commit is contained in:
parent
cfb0e7ea45
commit
0beb54b3a4
3 changed files with 62 additions and 18 deletions
|
|
@ -25,7 +25,8 @@ export type SitePatch = {
|
|||
pocName: string;
|
||||
pocPhone: string;
|
||||
pocNotes: string;
|
||||
additionalContacts: WorkOrderAdditionalContact[];
|
||||
/** Omitted for site-only saves when GET had no contacts and the user did not edit them. */
|
||||
additionalContacts?: WorkOrderAdditionalContact[];
|
||||
};
|
||||
|
||||
type SiteDialogProps = {
|
||||
|
|
@ -60,6 +61,11 @@ export function SiteDialog({
|
|||
const [notes, setNotes] = useState(pocNotes);
|
||||
const [extraContacts, setExtraContacts] = useState(additionalContacts);
|
||||
const [pocFilledFor, setPocFilledFor] = useState("");
|
||||
const [dirty, setDirty] = useState(false);
|
||||
const [contactsDirty, setContactsDirty] = useState(false);
|
||||
const [baselineHadContacts, setBaselineHadContacts] = useState(
|
||||
() => additionalContacts.length > 0,
|
||||
);
|
||||
|
||||
const {
|
||||
data: locationDetail,
|
||||
|
|
@ -72,6 +78,14 @@ export function SiteDialog({
|
|||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
setDirty(false);
|
||||
setContactsDirty(false);
|
||||
return;
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || dirty) {
|
||||
return;
|
||||
}
|
||||
const resolved = resolveLocationId(locationId, value, sites);
|
||||
|
|
@ -81,8 +95,9 @@ export function SiteDialog({
|
|||
setPp(pocPhone);
|
||||
setNotes(pocNotes);
|
||||
setExtraContacts(additionalContacts);
|
||||
setBaselineHadContacts(additionalContacts.length > 0);
|
||||
setPocFilledFor(pocName.trim() || pocPhone.trim() ? resolved : "");
|
||||
}, [open, value, locationId, pocName, pocPhone, pocNotes, additionalContacts, sites]);
|
||||
}, [open, dirty, value, locationId, pocName, pocPhone, pocNotes, additionalContacts, sites]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !locId || !locationDetail || pocFilledFor === locId) {
|
||||
|
|
@ -95,24 +110,36 @@ export function SiteDialog({
|
|||
|
||||
const handlePick = (id: string) => {
|
||||
const next = sites.find((s) => String(s.id) === id);
|
||||
const siteChanged = id !== locId;
|
||||
setLocId(id);
|
||||
if (next) {
|
||||
setCode(resolveSiteCode(next));
|
||||
}
|
||||
if (id !== pocFilledFor) {
|
||||
if (siteChanged) {
|
||||
// Match wizard: clear POC 2+ when the site changes so stale contacts are not saved.
|
||||
setExtraContacts([]);
|
||||
setContactsDirty(true);
|
||||
setDirty(true);
|
||||
setPocFilledFor("");
|
||||
} else if (id !== pocFilledFor) {
|
||||
setPocFilledFor("");
|
||||
}
|
||||
};
|
||||
|
||||
const buildPatch = (): SitePatch => ({
|
||||
site: code || (selected ? resolveSiteCode(selected) : ""),
|
||||
location: selected ? extractLocationLabel(selected) : "",
|
||||
locationId: locId,
|
||||
pocName: pn,
|
||||
pocPhone: pp,
|
||||
pocNotes: notes,
|
||||
additionalContacts: extraContacts,
|
||||
});
|
||||
const buildPatch = (): SitePatch => {
|
||||
const patch: SitePatch = {
|
||||
site: code || (selected ? resolveSiteCode(selected) : ""),
|
||||
location: selected ? extractLocationLabel(selected) : "",
|
||||
locationId: locId,
|
||||
pocName: pn,
|
||||
pocPhone: pp,
|
||||
pocNotes: notes,
|
||||
};
|
||||
if (contactsDirty || baselineHadContacts) {
|
||||
patch.additionalContacts = extraContacts;
|
||||
}
|
||||
return patch;
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
|
|
@ -173,10 +200,23 @@ export function SiteDialog({
|
|||
pocPhone={pp}
|
||||
pocNotes={notes}
|
||||
additionalContacts={extraContacts}
|
||||
onPocNameChange={setPn}
|
||||
onPocPhoneChange={setPp}
|
||||
onPocNotesChange={setNotes}
|
||||
onAdditionalContactsChange={setExtraContacts}
|
||||
onPocNameChange={(next) => {
|
||||
setDirty(true);
|
||||
setPn(next);
|
||||
}}
|
||||
onPocPhoneChange={(next) => {
|
||||
setDirty(true);
|
||||
setPp(next);
|
||||
}}
|
||||
onPocNotesChange={(next) => {
|
||||
setDirty(true);
|
||||
setNotes(next);
|
||||
}}
|
||||
onAdditionalContactsChange={(contacts) => {
|
||||
setDirty(true);
|
||||
setContactsDirty(true);
|
||||
setExtraContacts(contacts);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,9 @@ export function WoTableInlineRowCells({
|
|||
pocName: p.pocName,
|
||||
pocPhone: p.pocPhone,
|
||||
pocNotes: p.pocNotes,
|
||||
additionalContacts: p.additionalContacts,
|
||||
...(p.additionalContacts !== undefined
|
||||
? { additionalContacts: p.additionalContacts }
|
||||
: {}),
|
||||
})
|
||||
}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -60,7 +60,9 @@ export function WoTableRowCells({
|
|||
pocName: p.pocName,
|
||||
pocPhone: p.pocPhone,
|
||||
pocNotes: p.pocNotes,
|
||||
additionalContacts: p.additionalContacts,
|
||||
...(p.additionalContacts !== undefined
|
||||
? { additionalContacts: p.additionalContacts }
|
||||
: {}),
|
||||
})
|
||||
}
|
||||
/>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue