From d76c90fdc41052d1662bc9496859a3d44edccb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Miguel?= Date: Tue, 9 Jan 2018 11:32:47 +0000 Subject: [PATCH] Fix relation lists (regression #359) --- src/actions/entries.js | 6 +++--- src/components/Editor/Editor.js | 7 ++++--- src/reducers/entryDraft.js | 11 +++++++---- src/reducers/search.js | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/actions/entries.js b/src/actions/entries.js index 4797f4797eee..5b65f36add5c 100644 --- a/src/actions/entries.js +++ b/src/actions/entries.js @@ -177,10 +177,10 @@ export function emptyDraftCreated(entry) { /* * Exported simple Action Creators */ -export function createDraftFromEntry(entry) { +export function createDraftFromEntry(entry, metadata) { return { type: DRAFT_CREATE_FROM_ENTRY, - payload: entry, + payload: { entry, metadata }, }; } @@ -276,7 +276,7 @@ export function persistEntry(collection) { if (!fieldsErrors.isEmpty()) { const hasPresenceErrors = fieldsErrors .some(errors => errors.some(error => error.type && error.type === ValidationErrorTypes.PRESENCE)); - + if (hasPresenceErrors) { dispatch(notifSend({ message: 'Oops, you\'ve missed a required field. Please complete before saving.', diff --git a/src/components/Editor/Editor.js b/src/components/Editor/Editor.js index 2348f0c14a74..8e1b4580e95c 100644 --- a/src/components/Editor/Editor.js +++ b/src/components/Editor/Editor.js @@ -164,7 +164,8 @@ class Editor extends React.Component { */ const values = deserializeValues(entry.get('data'), fields); const deserializedEntry = entry.set('data', values); - this.createDraft(deserializedEntry); + const fieldsMetaData = nextProps.entryDraft && nextProps.entryDraft.get('fieldsMetaData'); + this.createDraft(deserializedEntry, fieldsMetaData); } else if (newEntry) { this.props.createEmptyDraft(collection); } @@ -175,8 +176,8 @@ class Editor extends React.Component { window.removeEventListener('beforeunload', this.exitBlocker); } - createDraft = (entry) => { - if (entry) this.props.createDraftFromEntry(entry); + createDraft = (entry, metadata) => { + if (entry) this.props.createDraftFromEntry(entry, metadata); }; handleChangeStatus = (newStatusName) => { diff --git a/src/reducers/entryDraft.js b/src/reducers/entryDraft.js index 16d589e17f15..936462a30005 100644 --- a/src/reducers/entryDraft.js +++ b/src/reducers/entryDraft.js @@ -33,10 +33,13 @@ const entryDraftReducer = (state = Map(), action) => { case DRAFT_CREATE_FROM_ENTRY: // Existing Entry return state.withMutations((state) => { - state.set('entry', action.payload); + state.set('entry', action.payload.entry); state.setIn(['entry', 'newRecord'], false); state.set('mediaFiles', List()); - state.set('fieldsMetaData', Map()); + // An existing entry may already have metadata. If we surfed away and back to its + // editor page, the metadata will have been fetched already, so we shouldn't + // clear it as to not break relation lists. + state.set('fieldsMetaData', action.payload.metadata || Map()); state.set('fieldsErrors', Map()); state.set('hasChanged', false); }); @@ -55,10 +58,10 @@ const entryDraftReducer = (state = Map(), action) => { case DRAFT_CHANGE_FIELD: return state.withMutations((state) => { state.setIn(['entry', 'data', action.payload.field], action.payload.value); - state.mergeIn(['fieldsMetaData'], fromJS(action.payload.metadata)); + state.mergeDeepIn(['fieldsMetaData'], fromJS(action.payload.metadata)); state.set('hasChanged', true); }); - + case DRAFT_VALIDATION_ERRORS: if (action.payload.errors.length === 0) { return state.deleteIn(['fieldsErrors', action.payload.field]); diff --git a/src/reducers/search.js b/src/reducers/search.js index 9ccbb5867fea..db73abe85209 100644 --- a/src/reducers/search.js +++ b/src/reducers/search.js @@ -56,7 +56,7 @@ const entries = (state = defaultState, action) => { return state.withMutations((map) => { map.set('isFetching', false); map.set('term', searchTerm); - map.set('queryHits', Map({ [action.payload.namespace]: response.hits })); + map.mergeIn(['queryHits'], Map({ [action.payload.namespace]: response.hits })); }); default: