Skip to content

Commit cd93c33

Browse files
Merge pull request #526 from devtron-labs/qa/build-infra-v3
refactor: use structuredClone for deep cloning in BuildInfraForm and utils
2 parents 0e61585 + 68facb9 commit cd93c33

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/Pages/GlobalConfigurations/BuildInfra/UseBuildInfraForm.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ const useBuildInfraForm = ({
118118
const targetPlatform =
119119
data && 'targetPlatform' in data && Object.hasOwn(data, 'targetPlatform') ? data.targetPlatform : ''
120120
const currentConfiguration = currentInput.configurations[targetPlatform]
121-
const lastSavedConfiguration = profileResponse.profile.configurations[targetPlatform] || currentConfiguration
121+
const lastSavedConfiguration = structuredClone(
122+
profileResponse.profile.configurations[targetPlatform] || currentConfiguration,
123+
)
122124

123125
switch (action) {
124126
case BuildInfraMetaConfigTypes.DESCRIPTION: {
@@ -236,9 +238,9 @@ const useBuildInfraForm = ({
236238
case 'de_activate_cm':
237239
case 'de_activate_cs': {
238240
const { id, componentType } = data
241+
const currentInfraConfigType = CM_SECRET_COMPONENT_TYPE_TO_INFRA_CONFIG_MAP[componentType]
239242
// Would just convert isOverridden to true and replace value with default value
240-
const cmSecretValue = currentConfiguration[CM_SECRET_COMPONENT_TYPE_TO_INFRA_CONFIG_MAP[componentType]]
241-
.value as BuildInfraCMCSValueType[]
243+
const cmSecretValue = currentConfiguration[currentInfraConfigType].value as BuildInfraCMCSValueType[]
242244
const selectedCMCSIndex = cmSecretValue.findIndex((configMapItem) => configMapItem.id === id)
243245

244246
if (selectedCMCSIndex === -1 || !cmSecretValue[selectedCMCSIndex].canOverride) {
@@ -254,10 +256,22 @@ const useBuildInfraForm = ({
254256
}
255257

256258
const isActivation = action === 'activate_cm' || action === 'activate_cs'
257-
cmSecretValue[selectedCMCSIndex].useFormProps = cmSecretValue[selectedCMCSIndex].defaultValue
258259
cmSecretValue[selectedCMCSIndex].isOverridden = isActivation
259-
cmSecretValue[selectedCMCSIndex].initialResponse =
260-
cmSecretValue[selectedCMCSIndex].defaultValueInitialResponse
260+
261+
if (isActivation) {
262+
const lastSavedCMSecretArray = (lastSavedConfiguration[currentInfraConfigType]?.value ||
263+
[]) as BuildInfraCMCSValueType[]
264+
const lastSavedCMSecret =
265+
lastSavedCMSecretArray.find((configMapItem) => configMapItem.id === id) ||
266+
cmSecretValue[selectedCMCSIndex]
267+
268+
cmSecretValue[selectedCMCSIndex].useFormProps = lastSavedCMSecret?.useFormProps
269+
cmSecretValue[selectedCMCSIndex].initialResponse = lastSavedCMSecret?.initialResponse
270+
} else {
271+
cmSecretValue[selectedCMCSIndex].useFormProps = cmSecretValue[selectedCMCSIndex].defaultValue
272+
cmSecretValue[selectedCMCSIndex].initialResponse =
273+
cmSecretValue[selectedCMCSIndex].defaultValueInitialResponse
274+
}
261275

262276
// Will remove error if present
263277
if (currentInputErrors[CM_SECRET_COMPONENT_TYPE_TO_INFRA_CONFIG_MAP[componentType]]) {

src/Pages/GlobalConfigurations/BuildInfra/utils.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ const parsePlatformServerConfigIntoUIConfig = (
196196
})
197197

198198
return {
199-
useFormProps: cmSecretFormProps,
200-
defaultValue: cmSecretFormProps,
201-
initialResponse: configMapSecretData,
202-
defaultValueInitialResponse: configMapSecretData,
199+
useFormProps: structuredClone(cmSecretFormProps),
200+
defaultValue: structuredClone(cmSecretFormProps),
201+
initialResponse: structuredClone(configMapSecretData),
202+
defaultValueInitialResponse: structuredClone(configMapSecretData),
203203
id: getUniqueId(),
204204
isOverridden: true,
205205
canOverride: !isDefaultProfile,
@@ -265,18 +265,21 @@ const getConfigurationMapWithoutDefaultFallback = (
265265
{} as BuildInfraConfigurationMapTypeWithoutDefaultFallback,
266266
) ?? ({} as BuildInfraConfigurationMapTypeWithoutDefaultFallback)
267267

268-
acc[platformName] = platformConfigValuesMap
268+
acc[platformName] = structuredClone(platformConfigValuesMap)
269269

270270
return acc
271271
}, {})
272272

273273
const getPlatformConfigurationsWithDefaultValues = ({
274-
profileConfigurationsMap,
275-
defaultConfigurationsMap,
274+
profileConfigurationsMap: profileConfigurationsMapProp,
275+
defaultConfigurationsMap: defaultConfigurationsMapProp,
276276
platformName,
277277
isDefaultProfile = false,
278-
}: GetPlatformConfigurationsWithDefaultValuesParamsType): BuildInfraConfigurationMapType =>
279-
Object.values(BuildInfraConfigTypes).reduce<BuildInfraConfigurationMapType>((acc, configType) => {
278+
}: GetPlatformConfigurationsWithDefaultValuesParamsType): BuildInfraConfigurationMapType => {
279+
const profileConfigurationsMap = structuredClone(profileConfigurationsMapProp)
280+
const defaultConfigurationsMap = structuredClone(defaultConfigurationsMapProp)
281+
282+
return Object.values(BuildInfraConfigTypes).reduce<BuildInfraConfigurationMapType>((acc, configType) => {
280283
const defaultConfiguration = defaultConfigurationsMap[configType]
281284
const profileConfiguration = profileConfigurationsMap[configType]?.active
282285
? profileConfigurationsMap[configType]
@@ -313,7 +316,9 @@ const getPlatformConfigurationsWithDefaultValues = ({
313316

314317
const finalValues: BuildInfraCMCSValueType[] =
315318
(profileConfiguration?.value as BuildInfraCMCSValueType[])?.map((configMapSecretData) => {
316-
const defaultConfigInfo = defaultConfigurationValueMap[configMapSecretData.useFormProps.name]
319+
const defaultConfigInfo = structuredClone(
320+
defaultConfigurationValueMap[configMapSecretData.useFormProps.name],
321+
)
317322

318323
return {
319324
...configMapSecretData,
@@ -365,6 +370,7 @@ const getPlatformConfigurationsWithDefaultValues = ({
365370

366371
return acc
367372
}, {} as BuildInfraConfigurationMapType)
373+
}
368374

369375
// Would receive a single profile and return transformed response
370376
export const getTransformedBuildInfraProfileResponse = ({

0 commit comments

Comments
 (0)