-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[RFR] Fix create to edit form values #2339
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c3c027
[WIP] Fix create to edit form values
djhi cc16e80
Introduce formMiddleware to intercept location changes
djhi a40cc9e
Add unit tests
djhi d9b45d2
Fix test
djhi d78c01e
Typo fix s/change/changes/
sedy-bot 41bbb17
Typo fix s/may contains/may contain/
sedy-bot e04eaf9
Update CustomApp documentation
djhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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'; | ||
|
||
/** | ||
* This middleware ensure that whenever a location change happen, we get the | ||
* chance to properly reset the redux-form record form, preventing data to be | ||
* kept between different resources or form types (CREATE, EDIT). | ||
* | ||
* A middleware is needed instead of a saga because we need to control the actions | ||
* order: we need to ensure we reset the redux form BEFORE the location actually | ||
* change. Otherwise, the new page which may contains a record redux-form might | ||
* initialize before our reset and loose its data. | ||
*/ | ||
const formMiddleware = () => { | ||
let previousLocation; | ||
return next => action => { | ||
if ( | ||
action.type !== LOCATION_CHANGE || | ||
(action.payload.state && action.payload.state.skipFormReset) | ||
) { | ||
return next(action); | ||
} | ||
|
||
// history allows one to redirect to the same location which can happen | ||
// when using a special menu for a create page for instance. In this case, | ||
// we don't want to reset the form. | ||
// See https://github.com/marmelab/react-admin/issues/2291 | ||
if (isEqual(action.payload, previousLocation)) { | ||
return next(action); | ||
} | ||
|
||
previousLocation = action.payload; | ||
next(resetForm()); | ||
next(destroy(REDUX_FORM_NAME)); | ||
return next(action); | ||
}; | ||
}; | ||
|
||
export default formMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { LOCATION_CHANGE } from 'react-router-redux'; | ||
import { destroy } from 'redux-form'; | ||
|
||
import formMiddleware from './formMiddleware'; | ||
import { REDUX_FORM_NAME } from '../form/constants'; | ||
import { resetForm } from '../actions/formActions'; | ||
|
||
describe('form middleware', () => { | ||
it('does not prevent actions other than LOCATION_CHANGE to be handled', () => { | ||
const next = jest.fn(); | ||
const action = { type: '@@redux-form/INITIALIZE' }; | ||
|
||
formMiddleware()(next)(action); | ||
|
||
expect(next).toHaveBeenCalledWith(action); | ||
expect(next).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('does not prevent LOCATION_CHANGE actions to be handled if their state contains a skipFormReset set to true', () => { | ||
const next = jest.fn(); | ||
const action = { | ||
type: LOCATION_CHANGE, | ||
payload: { state: { skipFormReset: true } }, | ||
}; | ||
formMiddleware()(next)(action); | ||
|
||
expect(next).toHaveBeenCalledWith(action); | ||
}); | ||
|
||
it('resets the record state and destroy the redux form before letting the location change to be handled', () => { | ||
const next = jest.fn(); | ||
const action = { | ||
type: LOCATION_CHANGE, | ||
payload: { | ||
pathname: '/posts/create', | ||
}, | ||
}; | ||
formMiddleware()(next)(action); | ||
|
||
expect(next).toHaveBeenCalledWith(resetForm()); | ||
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME)); | ||
expect(next).toHaveBeenCalledWith(action); | ||
expect(next).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('does not resets the record and form if LOCATION_CHANGE targets the same location', () => { | ||
const next = jest.fn(); | ||
const action = { | ||
type: LOCATION_CHANGE, | ||
payload: { | ||
pathname: '/posts/create', | ||
}, | ||
}; | ||
const middleware = formMiddleware()(next); | ||
middleware(action); | ||
middleware(action); | ||
expect(next).toHaveBeenCalledWith(resetForm()); | ||
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME)); | ||
expect(next).toHaveBeenCalledWith(action); | ||
expect(next).toHaveBeenCalledTimes(4); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/change/changes/
s/may contains/may contain/