diff --git a/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/RelationInputDataManager.js b/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/RelationInputDataManager.js index ad023e4628..d7fb71e2c8 100644 --- a/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/RelationInputDataManager.js +++ b/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/RelationInputDataManager.js @@ -6,7 +6,6 @@ import get from 'lodash/get'; import pick from 'lodash/pick'; import take from 'lodash/take'; import isNil from 'lodash/isNil'; -import findIndex from 'lodash/findIndex'; import { useCMEditViewDataManager, NotAllowedInput } from '@strapi/helper-plugin'; @@ -54,16 +53,16 @@ export const RelationInputDataManager = ({ const initialDataPath = []; const nameSplit = name.split('.'); nameSplit.reduce((acc, currentValue, index) => { - const pathSoFar = take(nameSplit, index); + const initialDataParent = get(initialData, initialDataPath); + const modifiedDataTempKey = get(modifiedData, [ + ...take(nameSplit, index), + currentValue, + '__temp_key__', + ]); - const initialDataParent = get(initialData, pathSoFar); - const modifiedDataValue = get(modifiedData, [...pathSoFar, currentValue]); - const tempKey = get(modifiedDataValue, '__temp_key__'); - - if (!isNil(tempKey) && Array.isArray(initialDataParent)) { - const initialDataIndex = findIndex( - initialDataParent, - (entry) => entry.__temp_key__ === tempKey + if (!isNil(modifiedDataTempKey) && Array.isArray(initialDataParent)) { + const initialDataIndex = initialDataParent.findIndex( + (entry) => entry.__temp_key__ === modifiedDataTempKey ); acc.push(`${initialDataIndex}`); diff --git a/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/tests/RelationInputDataManger.test.js b/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/tests/RelationInputDataManger.test.js index 1455944b31..634f73b6e7 100644 --- a/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/tests/RelationInputDataManger.test.js +++ b/packages/core/admin/admin/src/content-manager/components/RelationInputDataManager/tests/RelationInputDataManger.test.js @@ -688,10 +688,6 @@ describe('RelationInputDataManager', () => { test('correctly computes modified and initial data paths for nested content and passes this to useRelation', async () => { const initialData = { dz: [ - { - __component: 'default.blank', - __temp_key__: 6, - }, { __component: 'default.withToManyComponentRelation', __temp_key__: 7, @@ -708,6 +704,10 @@ describe('RelationInputDataManager', () => { }, ], }, + { + __component: 'default.blank', + __temp_key__: 6, + }, ], }; @@ -753,7 +753,7 @@ describe('RelationInputDataManager', () => { expect(useRelation).toBeCalledWith( expect.any(String), expect.objectContaining({ - initialDataPath: ['initialData', 'dz', '1', 'toManyRelation', '1', 'publishers'], + initialDataPath: ['initialData', 'dz', '0', 'toManyRelation', '1', 'publishers'], modifiedDataPath: ['modifiedData', 'dz', '1', 'toManyRelation', '0', 'publishers'], }) );