Skip to content

Commit

Permalink
Merge pull request #2322 from marmelab/fix-navigating-to-same-url-cle…
Browse files Browse the repository at this point in the history
…ars-form

[RFR] Fix resetform when navigating to same page
  • Loading branch information
fzaninotto authored Sep 19, 2018
2 parents 51acc98 + 4110f2d commit 99e9d4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/ra-core/src/sideEffect/recordForm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { put, takeEvery } from 'redux-saga/effects';
import { LOCATION_CHANGE } from 'react-router-redux';
import { destroy } from 'redux-form';
import isEqual from 'lodash/isEqual';

import { resetForm } from '../actions/formActions';
import { REDUX_FORM_NAME } from '../form/constants';

export function* handleLocationChange({ payload: { state } }) {
if (state && state.skipFormReset) {
let previousLocation;

export function* handleLocationChange({ payload }) {
if (payload.state && payload.state.skipFormReset) {
return;
}

if (isEqual(payload, previousLocation)) {
return;
}

previousLocation = payload;

yield put(resetForm());
yield put(destroy(REDUX_FORM_NAME));
}
Expand Down
21 changes: 21 additions & 0 deletions packages/ra-core/src/sideEffect/recordForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@ describe('recordForm saga', () => {

expect(saga.next().value).toBeUndefined();
});

it('does not reset the form when navigating to same location', () => {
const saga = handleLocationChange({
type: LOCATION_CHANGE,
payload: {
pathname: '/comments/create/2',
},
});
saga.next();
saga.next();
saga.next();

const saga2 = handleLocationChange({
type: LOCATION_CHANGE,
payload: {
pathname: '/comments/create/2',
},
});

expect(saga2.next().value).toBeUndefined();
});
});

0 comments on commit 99e9d4f

Please sign in to comment.