diff --git a/mailpoet/assets/js/src/automation/editor/index.tsx b/mailpoet/assets/js/src/automation/editor/index.tsx index 5e808af6c6..26426c7540 100644 --- a/mailpoet/assets/js/src/automation/editor/index.tsx +++ b/mailpoet/assets/js/src/automation/editor/index.tsx @@ -72,6 +72,25 @@ function updatingActiveWorkflowNotPossible() { ); } +function onUnload(event) { + if (!globalSelect(storeName).getWorkflowSaved()) { + // eslint-disable-next-line no-param-reassign + event.returnValue = __( + 'There are unsaved changes that will be lost. Do you want to continue?', + 'mailpoet', + ); + return event.returnValue; + } + return ''; +} + +function useConfirmUnsaved() { + useEffect(() => { + window.addEventListener('beforeunload', onUnload); + return () => window.removeEventListener('beforeunload', onUnload); + }, []); +} + function Editor(): JSX.Element { const { isFullscreenActive, @@ -93,6 +112,8 @@ function Editor(): JSX.Element { ); const [isBooting, setIsBooting] = useState(true); + useConfirmUnsaved(); + useEffect(() => { if (!isBooting) { return; diff --git a/mailpoet/tests/acceptance/Automation/ConfirmLeaveWhenUnsavedChangesCest.php b/mailpoet/tests/acceptance/Automation/ConfirmLeaveWhenUnsavedChangesCest.php new file mode 100644 index 0000000000..cc4cd82200 --- /dev/null +++ b/mailpoet/tests/acceptance/Automation/ConfirmLeaveWhenUnsavedChangesCest.php @@ -0,0 +1,52 @@ +withFeatureEnabled(FeaturesController::AUTOMATION); + $container = ContainerWrapper::getInstance(); + $migrator = $container->get(Migrator::class); + $migrator->deleteSchema(); + $migrator->createSchema(); + } + + public function confirmationIsRequiredIfWorkflowNotSaved(\AcceptanceTester $i) { + $i->wantTo('Edit a new workflow draft'); + $i->login(); + + $i->amOnMailpoetPage('Automation'); + $i->see('Automations'); + $i->waitForText('Better engagement begins with automation'); + $i->dontSee('Active'); + $i->dontSee('Entered'); + + $i->click('Start with a template'); + $i->see('Choose your automation template'); + $i->click('Simple welcome email'); + + $i->waitForText('Draft'); + $i->click('Trigger'); + $i->fillField('When someone subscribes to the following lists:', 'Newsletter mailing list'); + $i->click('Delay'); + $i->fillField('Wait for', '5'); + + $i->wantTo('Leave the page without saving.'); + $i->reloadPage(); + $i->cancelPopup(); + + $i->wantTo('Leave the page after saving.'); + $i->click('Save'); + $i->waitForText('saved'); + $i->reloadPage(); + $i->waitForText('Draft'); + } +}