diff --git a/web-v2/src/pages/AssetEditorPage.tsx b/web-v2/src/pages/AssetEditorPage.tsx index 03abd55..ee13884 100644 --- a/web-v2/src/pages/AssetEditorPage.tsx +++ b/web-v2/src/pages/AssetEditorPage.tsx @@ -158,21 +158,27 @@ export function AssetEditorPage() { useEffect(() => { if (!canReadRelations || selectedType?.operationalRole !== 'GENERIC' || !parentId) { setOperationalAreas([]); - if (!parentId) { setOperationalAreaId(''); setOperatorCompanyId(''); } + if (!parentId) { setOperationalAreaId(''); if (!editing) setOperatorCompanyId(''); } return; } listOperationalAreas(parentId).then((loadedAreas) => { setOperationalAreas(loadedAreas); - if (operationalAreaId && !loadedAreas.some((area) => area.id === operationalAreaId)) { setOperationalAreaId(''); setOperatorCompanyId(''); } + if (operationalAreaId && !loadedAreas.some((area) => area.id === operationalAreaId)) { + setOperationalAreaId(''); + if (!editing) setOperatorCompanyId(''); + } }).catch((requestError) => setError(errorMessage(requestError))); - }, [canReadRelations, selectedType?.operationalRole, parentId]); + }, [canReadRelations, selectedType?.operationalRole, parentId, editing]); useEffect(() => { - if (!canReadRelations || !operationalAreaId || selectedType?.operationalRole !== 'GENERIC') { setOperationalCompanies([]); return; } + if (editing || !canReadRelations || !operationalAreaId || selectedType?.operationalRole !== 'GENERIC') { + setOperationalCompanies([]); + return; + } listCompaniesForArea(operationalAreaId).then(setOperationalCompanies).catch((requestError) => setError(errorMessage(requestError))); - }, [canReadRelations, operationalAreaId, selectedType?.operationalRole]); + }, [editing, canReadRelations, operationalAreaId, selectedType?.operationalRole]); - const changeType = (nextTypeId: string) => { setTypeId(nextTypeId); setParentId(''); setOperationalAreaId(''); setOperatorCompanyId(''); setAttributeValues({}); setParentSearch(''); }; + const changeType = (nextTypeId: string) => { setTypeId(nextTypeId); setParentId(''); setOperationalAreaId(''); if (!editing) setOperatorCompanyId(''); setAttributeValues({}); setParentSearch(''); }; const setAttribute = (definitionId: string, value: unknown) => setAttributeValues((current) => ({ ...current, [definitionId]: value })); const save = async (event: FormEvent) => { @@ -182,7 +188,7 @@ export function AssetEditorPage() { const attributes = normalizeAttributeValues(definitions, attributeValues); let saved: AssetDetail; if (editing && id) { - if (canEdit) saved = await updateAsset(id, { typeId: canDirectContextEdit ? typeId : undefined, code, name, commonName: commonName.trim() || null, parentId: canDirectContextEdit ? parentId || null : undefined, operationalAreaId: canDirectContextEdit ? operationalAreaId || null : undefined, operatorCompanyId: canDirectContextEdit ? operatorCompanyId || null : undefined, description: description.trim() || null, attributes }); + if (canEdit) saved = await updateAsset(id, { typeId: canDirectContextEdit ? typeId : undefined, code, name, commonName: commonName.trim() || null, parentId: canDirectContextEdit ? parentId || null : undefined, operationalAreaId: canDirectContextEdit ? operationalAreaId || null : undefined, description: description.trim() || null, attributes }); else if (asset) saved = asset; else return; if (canChangeStatus && saved.informationStatus !== status) saved = await updateAssetInformationStatus(id, status); if (canChangeOperationalStatus && saved.operationalStatus !== operationalStatus) saved = await updateAssetOperationalStatus(id, operationalStatus); @@ -229,9 +235,9 @@ export function AssetEditorPage() {