-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce formMiddleware to intercept location changes
- Loading branch information
Showing
8 changed files
with
56 additions
and
84 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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'; | ||
|
||
let previousLocation; | ||
|
||
/** | ||
* 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. | ||
* | ||
* A middleware is needed instead of a saga because we 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 = () => 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
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.