From 9fcb28635442c18d8a0d21c686e0973c7ca0905d Mon Sep 17 00:00:00 2001 From: Michael Wiencek Date: Thu, 23 Feb 2023 18:14:58 -0600 Subject: [PATCH] MBS-12937: Changing credits should work without modifying the source relationship (#2865) Fixes the check in `update-relationship-state` to proceed with updating entity credits even if the source relationship did not change. --- .../components/RelationshipEditor.js | 32 +++++++-- .../scripts/tests/relationship-editor.js | 65 +++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/root/static/scripts/relationship-editor/components/RelationshipEditor.js b/root/static/scripts/relationship-editor/components/RelationshipEditor.js index 1079cff14cb..fdb5aec7b07 100644 --- a/root/static/scripts/relationship-editor/components/RelationshipEditor.js +++ b/root/static/scripts/relationship-editor/components/RelationshipEditor.js @@ -435,9 +435,19 @@ export function runReducer( sourceEntity, } = action; + const relationshipStateChanged = ( + oldRelationshipState != null && + !relationshipsAreIdentical( + oldRelationshipState, + newRelationshipState, + ) + ); + if ( oldRelationshipState == null || - !relationshipsAreIdentical(oldRelationshipState, newRelationshipState) + relationshipStateChanged || + creditsToChangeForSource || + creditsToChangeForTarget ) { const targetEntity = getRelationshipTarget( newRelationshipState, @@ -445,7 +455,10 @@ export function runReducer( ); const updates = []; - if (oldRelationshipState != null) { + if ( + oldRelationshipState != null && + relationshipStateChanged + ) { /* * The old relationship state must be removed first in a separate * `updateRelationships` call, because its presence affects other @@ -464,11 +477,16 @@ export function runReducer( ); } - updates.push(...getUpdatesForAcceptedRelationship( - writableState, - newRelationshipState, - sourceEntity, - )); + if ( + oldRelationshipState == null || + relationshipStateChanged + ) { + updates.push(...getUpdatesForAcceptedRelationship( + writableState, + newRelationshipState, + sourceEntity, + )); + } /* * `updateEntityCredits` only uses `newRelationshipState` to obtain diff --git a/root/static/scripts/tests/relationship-editor.js b/root/static/scripts/tests/relationship-editor.js index f7dea5080d1..7bbf0360ec0 100644 --- a/root/static/scripts/tests/relationship-editor.js +++ b/root/static/scripts/tests/relationship-editor.js @@ -671,6 +671,71 @@ test('splitRelationshipByAttributes', function (t) { ); }); +test('MBS-12937: Changing credits for other relationships without modifying the source relationship', function (t) { + t.plan(1); + + const relationship1 = { + _lineage: [], + _original: null, + _status: REL_STATUS_ADD, + attributes: null, + begin_date: null, + editsPending: false, + end_date: null, + ended: false, + entity0: artist, + entity0_credit: 'SOMECREDIT', + entity1: recording, + entity1_credit: '', + id: -1, + linkOrder: 0, + linkTypeID: 148, + }; + + const relationship2 = { + ...relationship1, + entity0_credit: '', + id: -2, + linkTypeID: 297, + }; + + Object.freeze(relationship1); + Object.freeze(relationship2); + + let state = addRelationships( + initialState, + recording, + [relationship1, relationship2], + ); + + state = reducer( + state, + { + batchSelectionCount: 0, + creditsToChangeForSource: '', + creditsToChangeForTarget: 'all', + newRelationshipState: relationship1, + oldRelationshipState: relationship1, + sourceEntity: recording, + type: 'update-relationship-state', + }, + ); + + currentRelationshipsEqual( + t, + state, + [ + relationship1, + { + ...relationship2, + entity0_credit: 'SOMECREDIT', + }, + ], + 'all entity credits are updated despite not modifying the source ' + + 'relationship', + ); +}); + function addRelationships( rootState: RelationshipEditorStateT, source: CoreEntityT,