Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: ignore metadata when setting entry unsaved flag #3305

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const entry = {
path: 'content/blog/art-and-wine-festival.md',
partial: false,
raw: '',
data: {},
data: {
dataField: 'dataFieldInitialValue',
},
metaData: null,
};

Expand Down Expand Up @@ -65,6 +67,47 @@ describe('entryDraft reducer', () => {
});
});

describe('DRAFT_CHANGE_FIELD', () => {
it('should update the draft field', () => {
const initialEntry = reducer(initialState, actions.createDraftFromEntry(fromJS(entry)));

const actualState = reducer(
initialEntry,
actions.changeDraftField('dataField', 'update', {}),
);

expect(actualState.toJS()).toEqual({
...initialEntry.toJS(),
entry: {
...initialEntry.toJS().entry,
data: {
dataField: 'update',
},
},
hasChanged: true,
});
});

it('should metadata update the draft field without marking hasChanged', () => {
const initialEntry = reducer(initialState, actions.createDraftFromEntry(fromJS(entry)));

const actualState = reducer(
initialEntry,
actions.changeDraftField('dataField', 'dataFieldInitialValue', {
metaField: 'metaFieldValue',
}),
);

expect(actualState.toJS()).toEqual({
...initialEntry.toJS(),
fieldsMetaData: {
metaField: 'metaFieldValue',
},
hasChanged: false,
});
});
});

describe('persisting', () => {
let initialState;

Expand Down
9 changes: 6 additions & 3 deletions packages/netlify-cms-core/src/reducers/entryDraft.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Map, List, fromJS } from 'immutable';
import { Map, List, fromJS, is } from 'immutable';
import uuid from 'uuid/v4';
import {
DRAFT_CREATE_FROM_ENTRY,
Expand Down Expand Up @@ -90,9 +90,12 @@ const entryDraftReducer = (state = Map(), action) => {
}
case DRAFT_CHANGE_FIELD:
return state.withMutations(state => {
state.setIn(['entry', 'data', action.payload.field], action.payload.value);
state.mergeDeepIn(['fieldsMetaData'], fromJS(action.payload.metadata));
state.set('hasChanged', true);
const prev = state.getIn(['entry', 'data', action.payload.field]);
if (!is(action.payload.value, prev)) {
state.setIn(['entry', 'data', action.payload.field], action.payload.value);
state.set('hasChanged', true);
}
});

case DRAFT_VALIDATION_ERRORS:
Expand Down