Change newsletter and template saving to JSON encode body separately

This commit is contained in:
Tautvidas Sipavičius
2016-01-11 18:27:30 +02:00
parent ffc1d0a61c
commit b7cfa549d5
6 changed files with 89 additions and 37 deletions

View File

@ -63,9 +63,10 @@ define([
}; };
Module.saveNewsletter = function(options) { Module.saveNewsletter = function(options) {
return Module._query({ return MailPoet.Ajax.post({
endpoint: 'newsletters',
action: 'save', action: 'save',
options: options, data: options || {},
}); });
}; };

View File

@ -1,5 +1,6 @@
define([ define([
'newsletter_editor/App', 'newsletter_editor/App',
'newsletter_editor/components/communication',
'mailpoet', 'mailpoet',
'notice', 'notice',
'backbone', 'backbone',
@ -8,7 +9,18 @@ define([
'blob', 'blob',
'filesaver', 'filesaver',
'html2canvas' 'html2canvas'
], function(App, MailPoet, Notice, Backbone, Marionette, jQuery, Blob, FileSaver, html2canvas) { ], function(
App,
CommunicationComponent,
MailPoet,
Notice,
Backbone,
Marionette,
jQuery,
Blob,
FileSaver,
html2canvas
) {
"use strict"; "use strict";
@ -17,16 +29,18 @@ define([
// Save editor contents to server // Save editor contents to server
Module.save = function() { Module.save = function() {
App.getChannel().trigger('beforeEditorSave');
var json = App.toJSON(); var json = App.toJSON();
// Stringify to enable transmission of primitive non-string value types
if (!_.isUndefined(json.body)) {
json.body = JSON.stringify(json.body);
}
App.getChannel().trigger('beforeEditorSave', json);
// save newsletter // save newsletter
MailPoet.Ajax.post({ CommunicationComponent.saveNewsletter(json).done(function(response) {
endpoint: 'newsletters',
action: 'save',
data: json,
}).done(function(response) {
if(response.success !== undefined && response.success === true) { if(response.success !== undefined && response.success === true) {
// TODO: Handle translations // TODO: Handle translations
//MailPoet.Notice.success("<?php _e('Newsletter has been saved.'); ?>"); //MailPoet.Notice.success("<?php _e('Newsletter has been saved.'); ?>");
@ -58,7 +72,7 @@ define([
promise.then(function(thumbnail) { promise.then(function(thumbnail) {
var data = _.extend(options || {}, { var data = _.extend(options || {}, {
thumbnail: thumbnail.toDataURL('image/jpeg'), thumbnail: thumbnail.toDataURL('image/jpeg'),
body: App.getBody(), body: JSON.stringify(App.getBody()),
}); });
return MailPoet.Ajax.post({ return MailPoet.Ajax.post({

View File

@ -18,6 +18,12 @@ define(
var ImportTemplate = React.createClass({ var ImportTemplate = React.createClass({
saveTemplate: function(template) { saveTemplate: function(template) {
// Stringify to enable transmission of primitive non-string value types
if (!_.isUndefined(template.body)) {
template.body = JSON.stringify(template.body);
}
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: 'newsletterTemplates', endpoint: 'newsletterTemplates',
action: 'save', action: 'save',
@ -111,12 +117,19 @@ define(
}.bind(this)); }.bind(this));
}, },
handleSelectTemplate: function(template) { handleSelectTemplate: function(template) {
var body = template.body;
// Stringify to enable transmission of primitive non-string value types
if (!_.isUndefined(body)) {
body = JSON.stringify(body);
}
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: 'newsletters', endpoint: 'newsletters',
action: 'save', action: 'save',
data: { data: {
id: this.props.params.id, id: this.props.params.id,
body: template.body body: body
} }
}).done(function(response) { }).done(function(response) {
if(response.result === true) { if(response.result === true) {

View File

@ -30,10 +30,6 @@ class NewsletterTemplates {
} }
function save($data = array()) { function save($data = array()) {
if (isset($data['body'])) {
$data['body'] = json_encode($data['body']);
}
$result = NewsletterTemplate::createOrUpdate($data); $result = NewsletterTemplate::createOrUpdate($data);
if($result !== true) { if($result !== true) {
wp_send_json($result); wp_send_json($result);

View File

@ -59,10 +59,6 @@ class Newsletters {
unset($data['options']); unset($data['options']);
} }
if (isset($data['body'])) {
$data['body'] = json_encode($data['body']);
}
$errors = array(); $errors = array();
$result = false; $result = false;

View File

@ -1,21 +1,18 @@
define([ define([
'newsletter_editor/App', 'newsletter_editor/App',
'newsletter_editor/components/save', 'newsletter_editor/components/save',
'amd-inject-loader!newsletter_editor/components/save' 'amd-inject-loader!newsletter_editor/components/save',
], function(EditorApplication, SaveComponent, SaveInjector) { 'jquery'
], function(EditorApplication, SaveComponent, SaveInjector, jQuery) {
describe('Save', function() { describe('Save', function() {
describe('save method', function() { describe('save method', function() {
var module; var module;
before(function() { before(function() {
module = SaveInjector({ module = SaveInjector({
'mailpoet': { 'newsletter_editor/components/communication': {
Ajax: { saveNewsletter: function() {
post: function() { return jQuery.Deferred();
var deferred = jQuery.Deferred();
deferred.resolve({});
return deferred;
}
} }
} }
}); });
@ -26,29 +23,41 @@ define([
global.stubChannel(EditorApplication, { global.stubChannel(EditorApplication, {
trigger: spy, trigger: spy,
}); });
EditorApplication.toJSON = sinon.stub(); EditorApplication.toJSON = sinon.stub().returns({
body: {
type: 'container',
},
});
module.save(); module.save();
expect(spy.withArgs('beforeEditorSave').calledOnce).to.be.true; expect(spy.withArgs('beforeEditorSave').calledOnce).to.be.true;
}); });
it('triggers afterEditorSave event', function() { it('triggers afterEditorSave event', function() {
var stub = sinon.stub().callsArgWith(2, { success: true }), var spy = sinon.spy(),
spy = sinon.spy(); promise = jQuery.Deferred();
global.stubChannel(EditorApplication, { global.stubChannel(EditorApplication, {
trigger: spy, trigger: spy,
}); });
EditorApplication.toJSON = sinon.stub(); EditorApplication.toJSON = sinon.stub().returns({
body: {
type: 'container',
},
});
var module = SaveInjector({
'newsletter_editor/components/communication': {
saveNewsletter: sinon.stub().returns(promise),
}
});
promise.resolve({ success: true });
module.save(); module.save();
expect(spy.withArgs('afterEditorSave').calledOnce).to.be.true; expect(spy.withArgs('afterEditorSave').calledOnce).to.be.true;
}); });
it('sends newsletter json to server for saving', function() { it('sends newsletter json to server for saving', function() {
var mock = sinon.mock({ saveNewsletter: function() {} }).expects('saveNewsletter').once().returns(jQuery.Deferred()); var mock = sinon.mock().once().returns(jQuery.Deferred());
var module = SaveInjector({ var module = SaveInjector({
'mailpoet': { 'newsletter_editor/components/communication': {
Ajax: { saveNewsletter: mock,
post: mock,
}
} }
}); });
global.stubChannel(EditorApplication); global.stubChannel(EditorApplication);
@ -58,6 +67,29 @@ define([
mock.verify(); mock.verify();
}); });
it('encodes newsletter body in JSON format', function() {
var body = {type: 'testType'},
mock = sinon.mock()
.once()
.withArgs({
body: JSON.stringify(body),
})
.returns(jQuery.Deferred());
global.stubChannel(EditorApplication);
EditorApplication.toJSON = sinon.stub().returns({
body: body,
});
var module = SaveInjector({
'newsletter_editor/components/communication': {
saveNewsletter: mock,
}
});
module.save();
mock.verify();
});
}); });
describe('view', function() { describe('view', function() {