From 38e80f69ab122f9d9b49b9833723e2ed6c92e6f5 Mon Sep 17 00:00:00 2001 From: Joakim Uddholm Date: Wed, 15 Nov 2023 00:26:12 +0100 Subject: [PATCH] fix: ui crash if copying component with no controls/threats --- app/src/actions/model/copyNodes.js | 38 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/app/src/actions/model/copyNodes.js b/app/src/actions/model/copyNodes.js index 8bdb2048..1121045b 100644 --- a/app/src/actions/model/copyNodes.js +++ b/app/src/actions/model/copyNodes.js @@ -76,7 +76,7 @@ export const copyNodes = (componentIds) => async (dispatch, getState) => { //copy threats const copyThreats = async () => { await Promise.all( - threats?.threats[oldComponentId].map(async (oldThreatData) => { + (threats?.threats[oldComponentId] || []).map(async (oldThreatData) => { const result = await dispatch( api.endpoints.createThreat.initiate({ modelId: oldThreatData.modelId, @@ -97,29 +97,31 @@ export const copyNodes = (componentIds) => async (dispatch, getState) => { const copyControls = async () => await Promise.all( - controls?.controls[oldComponentId].map(async (oldControlData) => { - const result = await dispatch( - api.endpoints.createControl.initiate({ - modelId: oldControlData.modelId, - control: { - componentId: newComponentId, - description: oldControlData.description, - title: oldControlData.title, - inPlace: oldControlData.inPlace, - }, - }) - ); - - if (result?.data?.control?.id) { - idMap.set(oldControlData.id, result.data.control.id); + (controls?.controls[oldComponentId] || []).map( + async (oldControlData) => { + const result = await dispatch( + api.endpoints.createControl.initiate({ + modelId: oldControlData.modelId, + control: { + componentId: newComponentId, + description: oldControlData.description, + title: oldControlData.title, + inPlace: oldControlData.inPlace, + }, + }) + ); + + if (result?.data?.control?.id) { + idMap.set(oldControlData.id, result.data.control.id); + } } - }) + ) ); await Promise.all([copyThreats(), copyControls()]); const oldThreats = new Set( - threats?.threats[oldComponentId].map((t) => t.id) + threats?.threats[oldComponentId]?.map((t) => t.id) || [] ); //link mitigations