Warn a user when leaving the page without saving

[MAILPOET-2549]
This commit is contained in:
Pavel Dohnal
2020-01-20 13:32:52 +01:00
committed by Jack Kitterhing
parent 996b58a388
commit a8d0eff47b
10 changed files with 40 additions and 0 deletions

View File

@@ -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>

View File

@@ -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;

View File

@@ -7,6 +7,7 @@ export default (state, action) => {
};
return {
...newState,
hasUnsavedChanges: true,
formErrors: validateForm(newState.formData, newState.formBlocks),
};
};

View File

@@ -2,6 +2,7 @@ export default (state, action) => ({
...state,
formData: {
...state.formData,
hasUnsavedChanges: true,
name: action.name,
},
});

View File

@@ -10,6 +10,7 @@ export default (state, action) => {
};
return {
...newState,
hasUnsavedChanges: true,
formErrors: validateForm(newState.formData, newState.formBlocks),
};
};

View File

@@ -3,5 +3,6 @@ export default (state, action) => ({
formData: {
...state.formData,
styles: action.styles,
hasUnsavedChanges: true,
},
});

View File

@@ -11,6 +11,7 @@ export default (state) => {
return {
...state,
isFormSaving: false,
hasUnsavedChanges: false,
notices,
};
};

View File

@@ -62,4 +62,7 @@ export default {
getIsCustomFieldCreating(state) {
return state.isCustomFieldCreating;
},
hasUnsavedChanges(state) {
return state.hasUnsavedChanges;
},
};

View File

@@ -34,6 +34,7 @@ export default () => {
isCustomFieldSaving: false,
isCustomFieldCreating: false,
notices: [],
hasUnsavedChanges: false,
sidebar: {
activeTab: 'form',
openedPanels: ['basic-settings'],

View File

@@ -31,6 +31,7 @@
<% block translations %>
<%= localize({
'addFormName': _x('Add form name', 'A placeholder for form name input'),
'changesNotSaved': __('Your changes you made may not be saved'),
'form': __('Form'),
'formSettings': _x('Settings', 'A settings section heading'),
'customFieldSettings': _x('Custom field settings', 'A settings section heading'),