From 4d42a5572bee026df933f276dcb58a728392fdc5 Mon Sep 17 00:00:00 2001 From: Santiago <71732018+Zasa-san@users.noreply.github.com> Date: Thu, 20 Feb 2025 10:36:36 -0300 Subject: [PATCH] 7592 - Relationships save fix (#7687) * remove extra action * dispatch the correct action based on entity --- app/react/Relationships/actions/actions.js | 9 ++- .../actions/specs/actions.spec.js | 61 +++++++++++++++++-- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/app/react/Relationships/actions/actions.js b/app/react/Relationships/actions/actions.js index 0a48c8e713..09b1620f4b 100644 --- a/app/react/Relationships/actions/actions.js +++ b/app/react/Relationships/actions/actions.js @@ -190,9 +190,12 @@ function saveRelationships() { ]) ) .then(([response, parentEntity]) => { - dispatch(actions.set('entityView/entity', parentEntity)); - dispatch(actions.set('viewer/doc', parentEntity)); - + if (parentEntity.documents?.length) { + const defaultDoc = getState().documentViewer.doc.get('defaultDoc'); + dispatch(actions.set('viewer/doc', { ...parentEntity, defaultDoc })); + } else { + dispatch(actions.set('entityView/entity', parentEntity)); + } dispatch(uiActions.closePanel()); dispatch( edit( diff --git a/app/react/Relationships/actions/specs/actions.spec.js b/app/react/Relationships/actions/specs/actions.spec.js index 740a01093c..122880372b 100644 --- a/app/react/Relationships/actions/specs/actions.spec.js +++ b/app/react/Relationships/actions/specs/actions.spec.js @@ -4,11 +4,9 @@ import { fromJS as Immutable } from 'immutable'; import { mockID } from 'shared/uniqueID.js'; import { RequestParams } from 'app/utils/RequestParams'; import SearchApi from 'app/Search/SearchAPI'; - import api from 'app/utils/api'; import * as types from '../actionTypes'; import * as actions from '../actions'; - import * as routeUtils from '../../utils/routeUtils'; const middlewares = [thunk]; @@ -235,6 +233,13 @@ describe('Relationships actions', () => { describe('saveRelationships', () => { let store; let hubs; + let entity = { + _id: '1', + sharedId: 'entityId', + entity: 'fullEntity', + searchResults: 'storeSearchResults', + }; + let defaultDoc; function getState() { return { @@ -246,6 +251,9 @@ describe('Relationships actions', () => { }, hubs, }, + documentViewer: { + doc: Immutable({ ...entity, ...(defaultDoc && { defaultDoc, documents: [defaultDoc] }) }), + }, }; } @@ -313,7 +321,7 @@ describe('Relationships actions', () => { ]); spyOn(api, 'post').and.returnValue(Promise.resolve('POSTresponse')); - spyOn(api, 'get').and.returnValue(Promise.resolve({ json: { rows: ['entity'] } })); + spyOn(api, 'get').and.returnValue(Promise.resolve({ json: { rows: [entity] } })); spyOn(routeUtils, 'requestState').and.returnValue( Promise.resolve(['reloadedConnectionsGroups', 'reloadedSearchResults']) ); @@ -332,8 +340,49 @@ describe('Relationships actions', () => { value: 'reloadedConnectionsGroups', }, { type: 'relationships/list/searchResults/SET', value: 'reloadedSearchResults' }, - { type: 'entityView/entity/SET', value: 'entity' }, - { type: 'viewer/doc/SET', value: 'entity' }, + { type: 'entityView/entity/SET', value: entity }, + { type: 'CLOSE_RELATIONSHIPS_PANEL' }, + { + type: 'EDIT_RELATIONSHIPS', + value: false, + results: 'storeSearchResults', + parentEntity: 'fullEntity', + editing: false, + }, + { type: 'SAVED_RELATIONSHIPS', response: 'POSTresponse' }, + { + type: 'NOTIFY', + notification: { message: 'Relationships saved', type: 'success', id: 'unique_id' }, + }, + { + type: 'SET_REFERENCES', + references: { + rows: [entity], + }, + }, + ]); + done(); + }); + }); + + it('should update the store to update the document viewer if the entity has document', done => { + defaultDoc = { _id: '1', title: 'MyDoc' }; + entity.documents = [defaultDoc]; + + actions + .saveRelationships()(store.dispatch, getState) + .then(() => { + expect(store.getActions()).toEqual([ + { type: types.SAVING_RELATIONSHIPS }, + { + type: 'relationships/list/connectionsGroups/SET', + value: 'reloadedConnectionsGroups', + }, + { type: 'relationships/list/searchResults/SET', value: 'reloadedSearchResults' }, + { + type: 'viewer/doc/SET', + value: { ...entity, defaultDoc: Immutable(defaultDoc) }, + }, { type: 'CLOSE_RELATIONSHIPS_PANEL' }, { type: 'EDIT_RELATIONSHIPS', @@ -350,7 +399,7 @@ describe('Relationships actions', () => { { type: 'SET_REFERENCES', references: { - rows: ['entity'], + rows: [entity], }, }, ]);