diff --git a/web-v2/src/pages/AssetEditorPage.tsx b/web-v2/src/pages/AssetEditorPage.tsx index ee13884..4efab33 100644 --- a/web-v2/src/pages/AssetEditorPage.tsx +++ b/web-v2/src/pages/AssetEditorPage.tsx @@ -144,7 +144,6 @@ export function AssetEditorPage() { const parentType = types.find((type) => type.id === parent.type.id); if (parentType?.operationalRole === 'AREA') setOperationalAreaId(parent.id); else if (parent.operationalArea) setOperationalAreaId(parent.operationalArea.id); - if (parent.operatorCompany) setOperatorCompanyId(parent.operatorCompany.id); } }).catch((requestError) => setError(errorMessage(requestError))); }, [editing, contextParentId, types]); @@ -175,7 +174,16 @@ export function AssetEditorPage() { setOperationalCompanies([]); return; } - listCompaniesForArea(operationalAreaId).then(setOperationalCompanies).catch((requestError) => setError(errorMessage(requestError))); + listCompaniesForArea(operationalAreaId).then((loadedCompanies) => { + setOperationalCompanies(loadedCompanies); + setOperatorCompanyId((current) => loadedCompanies.some((company) => company.id === current) + ? current + : loadedCompanies.length === 1 ? loadedCompanies[0].id : ''); + }).catch((requestError) => { + setOperationalCompanies([]); + setOperatorCompanyId(''); + setError(errorMessage(requestError)); + }); }, [editing, canReadRelations, operationalAreaId, selectedType?.operationalRole]); const changeType = (nextTypeId: string) => { setTypeId(nextTypeId); setParentId(''); setOperationalAreaId(''); if (!editing) setOperatorCompanyId(''); setAttributeValues({}); setParentSearch(''); };