Confirm navigation when it would discard unsaved changes
- New state field: `state.entryDraft.hasChanged`, initialized to `false`. - `state.entryDraft.hasChanged` set to `true` in `entryDraft` reducer for `DRAFT_CHANGE_FIELD`. - `EntryPage` adds a `listenBefore` listener to `history` on `componentDidMount` that checks `this.props.entryDraft.hasChanged` and, if that is true, asks the user to confirm the navigation. - `EntryPage` removes its listener on `componentWillUnmount`.
This commit is contained in:
@ -19,7 +19,13 @@ import {
|
||||
REMOVE_ASSET,
|
||||
} from '../actions/media';
|
||||
|
||||
const initialState = Map({ entry: Map(), mediaFiles: List(), fieldsMetaData: Map(), fieldsErrors: Map() });
|
||||
const initialState = Map({
|
||||
entry: Map(),
|
||||
mediaFiles: List(),
|
||||
fieldsMetaData: Map(),
|
||||
fieldsErrors: Map(),
|
||||
hasChanged: false,
|
||||
});
|
||||
|
||||
const entryDraftReducer = (state = Map(), action) => {
|
||||
switch (action.type) {
|
||||
@ -47,6 +53,7 @@ const entryDraftReducer = (state = Map(), action) => {
|
||||
return state.withMutations((state) => {
|
||||
state.setIn(['entry', 'data', action.payload.field], action.payload.value);
|
||||
state.mergeIn(['fieldsMetaData'], fromJS(action.payload.metadata));
|
||||
state.set('hasChanged', true);
|
||||
});
|
||||
|
||||
case DRAFT_VALIDATION_ERRORS:
|
||||
|
Reference in New Issue
Block a user