Warn a user when leaving the page without saving
[MAILPOET-2549]
This commit is contained in:
committed by
Jack Kitterhing
parent
996b58a388
commit
a8d0eff47b
@@ -14,6 +14,7 @@ import Header from './header.jsx';
|
||||
import Sidebar from './sidebar.jsx';
|
||||
import FormTitle from './form_title.jsx';
|
||||
import Notices from './notices.jsx';
|
||||
import UnsavedChangesNotice from './unsaved_changes_notice.jsx';
|
||||
import FormStyles from './form_styles.jsx';
|
||||
|
||||
// Editor settings - see @wordpress/block-editor/src/store/defaults.js
|
||||
@@ -56,6 +57,7 @@ export default () => {
|
||||
>
|
||||
<div className="edit-post-layout__content">
|
||||
<Notices />
|
||||
<UnsavedChangesNotice />
|
||||
<BlockSelectionClearer className="edit-post-visual-editor editor-styles-wrapper">
|
||||
<BlockEditorKeyboardShortcuts />
|
||||
<WritingFlow>
|
||||
|
@@ -0,0 +1,28 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import MailPoet from 'mailpoet';
|
||||
import { useSelect } from '@wordpress/data';
|
||||
|
||||
function UnsavedChangesNotice() {
|
||||
const hasUnsavedChanges = useSelect(
|
||||
(sel) => sel('mailpoet-form-editor').hasUnsavedChanges(),
|
||||
[]
|
||||
);
|
||||
|
||||
function onUnload(event) {
|
||||
if (hasUnsavedChanges) {
|
||||
event.returnValue = MailPoet.I18n.t('changesNotSaved'); // eslint-disable-line no-param-reassign
|
||||
return event.returnValue;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('beforeunload', onUnload);
|
||||
|
||||
return () => window.removeEventListener('beforeunload', onUnload);
|
||||
});
|
||||
|
||||
return (<></>);
|
||||
}
|
||||
|
||||
export default UnsavedChangesNotice;
|
@@ -7,6 +7,7 @@ export default (state, action) => {
|
||||
};
|
||||
return {
|
||||
...newState,
|
||||
hasUnsavedChanges: true,
|
||||
formErrors: validateForm(newState.formData, newState.formBlocks),
|
||||
};
|
||||
};
|
||||
|
@@ -2,6 +2,7 @@ export default (state, action) => ({
|
||||
...state,
|
||||
formData: {
|
||||
...state.formData,
|
||||
hasUnsavedChanges: true,
|
||||
name: action.name,
|
||||
},
|
||||
});
|
||||
|
@@ -10,6 +10,7 @@ export default (state, action) => {
|
||||
};
|
||||
return {
|
||||
...newState,
|
||||
hasUnsavedChanges: true,
|
||||
formErrors: validateForm(newState.formData, newState.formBlocks),
|
||||
};
|
||||
};
|
||||
|
@@ -3,5 +3,6 @@ export default (state, action) => ({
|
||||
formData: {
|
||||
...state.formData,
|
||||
styles: action.styles,
|
||||
hasUnsavedChanges: true,
|
||||
},
|
||||
});
|
||||
|
@@ -11,6 +11,7 @@ export default (state) => {
|
||||
return {
|
||||
...state,
|
||||
isFormSaving: false,
|
||||
hasUnsavedChanges: false,
|
||||
notices,
|
||||
};
|
||||
};
|
||||
|
@@ -62,4 +62,7 @@ export default {
|
||||
getIsCustomFieldCreating(state) {
|
||||
return state.isCustomFieldCreating;
|
||||
},
|
||||
hasUnsavedChanges(state) {
|
||||
return state.hasUnsavedChanges;
|
||||
},
|
||||
};
|
||||
|
@@ -34,6 +34,7 @@ export default () => {
|
||||
isCustomFieldSaving: false,
|
||||
isCustomFieldCreating: false,
|
||||
notices: [],
|
||||
hasUnsavedChanges: false,
|
||||
sidebar: {
|
||||
activeTab: 'form',
|
||||
openedPanels: ['basic-settings'],
|
||||
|
Reference in New Issue
Block a user