Save newsletter before navigating away when clicked on "Next" button

This commit is contained in:
Tautvidas Sipavičius
2017-08-21 18:31:17 +03:00
parent 14810a22b5
commit dcb0b45c21
4 changed files with 40 additions and 34 deletions

View File

@ -65,15 +65,6 @@ define([
});
};
// For getting a promise after triggering save event
Module.saveAndProvidePromise = function(saveResult) {
var result = saveResult;
var promise = Module.save();
if (saveResult !== undefined) {
result.promise = promise;
}
};
Module.getThumbnail = function(element, options) {
var promise = html2canvas(element, options || {});
@ -169,7 +160,7 @@ define([
},
save: function() {
this.hideOptionContents();
App.getChannel().trigger('save');
App.getChannel().request('save');
},
beforeSave: function() {
// TODO: Add a loading animation instead
@ -289,7 +280,10 @@ define([
next: function() {
this.hideOptionContents();
if(!this.$('.mailpoet_save_next').hasClass('button-disabled')) {
window.location.href = App.getConfig().get('urls.send');
Module._cancelAutosave();
Module.save().done(function(response) {
window.location.href = App.getConfig().get('urls.send');
});
}
},
validateNewsletter: function(jsonObject) {
@ -326,15 +320,18 @@ define([
// may be requested
var AUTOSAVE_DELAY_DURATION = 1000;
// Cancel save timer if another change happens before it completes
if (saveTimeout) clearTimeout(saveTimeout);
Module._cancelAutosave();
saveTimeout = setTimeout(function() {
App.getChannel().trigger('save');
clearTimeout(saveTimeout);
saveTimeout = undefined;
App.getChannel().request('save').always(function() {
Module._cancelAutosave();
});
}, AUTOSAVE_DELAY_DURATION);
};
Module._cancelAutosave = function() {
if (saveTimeout) { clearTimeout(saveTimeout); saveTimeout = undefined; }
};
Module.beforeExitWithUnsavedChanges = function(e) {
if (saveTimeout) {
var message = MailPoet.I18n.t('unsavedChangesWillBeLost');
@ -349,13 +346,12 @@ define([
};
App.on('before:start', function(App, options) {
var Application = App;
Application.save = Module.saveAndProvidePromise;
Application.save = Module.save; //saveAndProvidePromise;
Application.getChannel().on('autoSave', Module.autoSave);
window.onbeforeunload = Module.beforeExitWithUnsavedChanges;
Application.getChannel().on('save', function(saveResult) { Application.save(saveResult); });
Application.getChannel().reply('save', App.save);
});
App.on('start', function(App, options) {

View File

@ -321,10 +321,7 @@ define([
MailPoet.Modal.loading(true);
// save before sending
var saveResult = {promise: null};
App.getChannel().trigger('save', saveResult);
saveResult.promise.always(function() {
App.getChannel().request('save').always(function() {
CommunicationComponent.previewNewsletter(data).always(function() {
MailPoet.Modal.loading(false);
}).done(function(response) {

View File

@ -57,6 +57,8 @@ jQuery.fn.stick_in_parent = function() { return this; };
global.stubChannel = function (EditorApplication, returnObject) {
var App = EditorApplication;
App.getChannel = sinon.stub().returns(_.defaults(returnObject || {}, {
request: function () {
},
trigger: function () {
},
on: function () {

View File

@ -92,15 +92,6 @@ define([
mock.verify();
});
it('provides a promise if a result container is passed to save event', function() {
var spy = sinon.spy(module, 'save'),
saveResult = {promise: null};
module.saveAndProvidePromise(saveResult);
spy.restore();
expect(spy.calledOnce).to.be.true;
expect(saveResult.promise).to.be.an('object');
expect(saveResult.promise.then).to.be.a('function');
});
});
describe('view', function() {
@ -124,9 +115,9 @@ define([
});
it('triggers newsletter saving when clicked on save button', function() {
var mock = sinon.mock({ trigger: function() {} }).expects('trigger').once().withArgs('save');
var mock = sinon.mock({ request: function() {} }).expects('request').once().withArgs('save');
global.stubChannel(EditorApplication, {
trigger: mock
request: mock
});
view.$('.mailpoet_save_button').click();
@ -177,6 +168,26 @@ define([
mock.verify();
});
it('saves newsletter when clicked on "next" button', function() {
var spy = sinon.spy(),
module = SaveInjector({
'newsletter_editor/components/communication': {
saveNewsletter: function() {
return jQuery.Deferred();
}
}
});
global.stubChannel(EditorApplication, {
trigger: spy
});
var view = new (module.SaveView)();
view.render();
view.$('.mailpoet_save_next').click();
expect(spy.withArgs('beforeEditorSave').calledOnce).to.be.true;
});
});
});
});