Files
piratepoet/assets/js/src/newsletter_editor/components/communication.js
Amine Ben hammou 4e2e9f6f8f rebasing on master
2017-10-09 11:17:23 +00:00

108 lines
2.3 KiB
JavaScript

define([
'newsletter_editor/App',
'underscore',
'mailpoet',
'ajax'
], function (App, _, MailPoet) {
var Module = {};
Module._query = function (args) {
return MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'automatedLatestContent',
action: args.action,
data: args.options || {}
});
};
Module._cachedQuery = _.memoize(Module._query, JSON.stringify);
Module.getNewsletter = function (options) {
return Module._query({
action: 'get',
options: options
});
};
Module.getPostTypes = function () {
return Module._cachedQuery({
action: 'getPostTypes',
options: {}
}).then(function (response) {
return _.values(response.data);
});
};
Module.getTaxonomies = function (postType) {
return Module._cachedQuery({
action: 'getTaxonomies',
options: {
postType: postType
}
}).then(function (response) {
return response.data;
});
};
Module.getTerms = function (options) {
return Module._cachedQuery({
action: 'getTerms',
options: options
}).then(function (response) {
return response.data;
});
};
Module.getPosts = function (options) {
return Module._cachedQuery({
action: 'getPosts',
options: options
}).then(function (response) {
return response.data;
});
};
Module.getTransformedPosts = function (options) {
return Module._cachedQuery({
action: 'getTransformedPosts',
options: options
}).then(function (response) {
return response.data;
});
};
Module.getBulkTransformedPosts = function (options) {
return Module._query({
action: 'getBulkTransformedPosts',
options: options
}).then(function (response) {
return response.data;
});
};
Module.saveNewsletter = function (options) {
return MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'newsletters',
action: 'save',
data: options || {}
});
};
Module.previewNewsletter = function (options) {
return MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'newsletters',
action: 'sendPreview',
data: options || {}
});
};
App.on('start', function () {
// Prefetch post types
Module.getPostTypes();
});
return Module;
});