updated ALC endpoint + nl editor + js tests

This commit is contained in:
Jonathan Labreuille
2016-08-05 15:25:54 +02:00
parent 4dd7f32f3a
commit 1305a10ee0
8 changed files with 70 additions and 56 deletions

View File

@ -51,9 +51,9 @@ define([
blocks: blocks, blocks: blocks,
}).then(_.partial(this.refreshBlocks, models)); }).then(_.partial(this.refreshBlocks, models));
}, },
refreshBlocks: function(models, renderedBlocks) { refreshBlocks: function(models, response) {
_.each( _.each(
_.zip(models, renderedBlocks), _.zip(models, response.data),
function(args) { function(args) {
var model = args[0], var model = args[0],
contents = args[1]; contents = args[1];
@ -217,17 +217,19 @@ define([
}; };
}, },
transport: function(options, success, failure) { transport: function(options, success, failure) {
var taxonomies, var taxonomies;
promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { var promise = CommunicationComponent.getTaxonomies(
taxonomies = tax; that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
// Fetch available terms based on the list of taxonomies already fetched // Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({ var promise = CommunicationComponent.getTerms({
search: options.data.term, search: options.data.term,
taxonomies: _.keys(taxonomies) taxonomies: _.keys(taxonomies)
}).then(function(terms) { }).then(function(response) {
return { return {
taxonomies: taxonomies, taxonomies: taxonomies,
terms: terms, terms: response.data
}; };
}); });
return promise; return promise;

View File

@ -108,8 +108,8 @@ define([
}, },
fetchAvailablePosts: function() { fetchAvailablePosts: function() {
var that = this; var that = this;
CommunicationComponent.getPosts(this.toJSON()).done(function(posts) { CommunicationComponent.getPosts(this.toJSON()).done(function(response) {
that.get('_availablePosts').reset(posts); that.get('_availablePosts').reset(response.data);
that.get('_selectedPosts').reset(); // Empty out the collection that.get('_selectedPosts').reset(); // Empty out the collection
that.trigger('change:_availablePosts'); that.trigger('change:_availablePosts');
}).fail(function() { }).fail(function() {
@ -127,8 +127,8 @@ define([
return; return;
} }
CommunicationComponent.getTransformedPosts(data).done(function(posts) { CommunicationComponent.getTransformedPosts(data).done(function(response) {
that.get('_transformedPosts').get('blocks').reset(posts, {parse: true}); that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true});
}).fail(function() { }).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
}); });
@ -143,8 +143,8 @@ define([
if (data.posts.length === 0) return; if (data.posts.length === 0) return;
CommunicationComponent.getTransformedPosts(data).done(function(posts) { CommunicationComponent.getTransformedPosts(data).done(function(response) {
collection.add(posts, { at: index }); collection.add(response.data, { at: index });
}).fail(function() { }).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
}); });
@ -302,17 +302,19 @@ define([
}; };
}, },
transport: function(options, success, failure) { transport: function(options, success, failure) {
var taxonomies, var taxonomies;
promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { var promise = CommunicationComponent.getTaxonomies(
taxonomies = tax; that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
// Fetch available terms based on the list of taxonomies already fetched // Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({ var promise = CommunicationComponent.getTerms({
search: options.data.term, search: options.data.term,
taxonomies: _.keys(taxonomies) taxonomies: _.keys(taxonomies)
}).then(function(terms) { }).then(function(response) {
return { return {
taxonomies: taxonomies, taxonomies: taxonomies,
terms: terms, terms: response.data
}; };
}); });
return promise; return promise;

View File

@ -27,8 +27,8 @@ define([
return Module._cachedQuery({ return Module._cachedQuery({
action: 'getPostTypes', action: 'getPostTypes',
options: {}, options: {},
}).then(function(types) { }).then(function(response) {
return _.values(types); return _.values(response.data);
}); });
}; };

View File

@ -8,7 +8,6 @@ abstract class Endpoint {
function successResponse( function successResponse(
$data = array(), $meta = array(), $status = Response::STATUS_OK $data = array(), $meta = array(), $status = Response::STATUS_OK
) { ) {
return new SuccessResponse($data, $meta, $status); return new SuccessResponse($data, $meta, $status);
} }

View File

@ -1,9 +1,11 @@
<?php <?php
namespace MailPoet\API\Endpoints; namespace MailPoet\API\Endpoints;
use \MailPoet\API\Endpoint as APIEndpoint;
use \MailPoet\API\Error as APIError;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class AutomatedLatestContent { class AutomatedLatestContent extends APIEndpoint {
public $ALC; public $ALC;
function __construct() { function __construct() {
@ -11,20 +13,26 @@ class AutomatedLatestContent {
} }
function getPostTypes() { function getPostTypes() {
return get_post_types(array(), 'objects'); return $this->successResponse(
get_post_types(array(), 'objects')
);
} }
function getTaxonomies($args) { function getTaxonomies($data = array()) {
$post_type = (isset($args['postType'])) ? $args['postType'] : 'post'; $post_type = (isset($data['postType'])) ? $data['postType'] : 'post';
return get_object_taxonomies($post_type, 'objects'); return $this->successResponse(
get_object_taxonomies($post_type, 'objects')
);
} }
function getTerms($args) { function getTerms($data = array()) {
$taxonomies = (isset($args['taxonomies'])) ? $args['taxonomies'] : array(); $taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : array();
$search = (isset($args['search'])) ? $args['search'] : ''; $search = (isset($data['search'])) ? $data['search'] : '';
$limit = (isset($args['limit'])) ? (int)$args['limit'] : 10; $limit = (isset($data['limit'])) ? (int)$data['limit'] : 10;
$page = (isset($args['page'])) ? (int)$args['page'] : 1; $page = (isset($data['page'])) ? (int)$data['page'] : 1;
return get_terms(
return $this->successResponse(
get_terms(
$taxonomies, $taxonomies,
array( array(
'hide_empty' => false, 'hide_empty' => false,
@ -32,25 +40,30 @@ class AutomatedLatestContent {
'number' => $limit, 'number' => $limit,
'offset' => $limit * ($page - 1) 'offset' => $limit * ($page - 1)
) )
)
); );
} }
function getPosts($args) { function getPosts($data = array()) {
return $this->ALC->getPosts($args); return $this->successResponse(
$this->ALC->getPosts($data)
);
} }
function getTransformedPosts($args) { function getTransformedPosts($data = array()) {
$posts = $this->ALC->getPosts($args); $posts = $this->ALC->getPosts($data);
return $this->ALC->transformPosts($args, $posts); return $this->successResponse(
$this->ALC->transformPosts($data, $posts)
);
} }
function getBulkTransformedPosts($args) { function getBulkTransformedPosts($data = array()) {
$alc = new \MailPoet\Newsletter\AutomatedLatestContent(); $alc = new \MailPoet\Newsletter\AutomatedLatestContent();
$used_posts = array(); $used_posts = array();
$rendered_posts = array(); $rendered_posts = array();
foreach($args['blocks'] as $block) { foreach($data['blocks'] as $block) {
$posts = $alc->getPosts($block, $used_posts); $posts = $alc->getPosts($block, $used_posts);
$rendered_posts[] = $alc->transformPosts($block, $posts); $rendered_posts[] = $alc->transformPosts($block, $posts);
@ -59,6 +72,6 @@ class AutomatedLatestContent {
} }
} }
return $rendered_posts; return $this->successResponse($rendered_posts);
} }
} }

View File

@ -12,12 +12,8 @@ class SuccessResponse extends Response {
} }
function getData() { function getData() {
if(empty($this->data)) {
return false;
} else {
return array( return array(
'data' => $this->data 'data' => $this->data
); );
} }
} }
}

View File

@ -53,7 +53,7 @@ define([
mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1); mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1);
mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2); mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2);
model.refreshBlocks([block1, block2], [postsSet1, postsSet2]); model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] });
mock1.verify(); mock1.verify();
mock2.verify(); mock2.verify();

View File

@ -12,8 +12,10 @@ define([
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ deferred.resolve({
data: {
'post': 'val1', 'post': 'val1',
'page': 'val2', 'page': 'val2',
}
}); });
return deferred; return deferred;
} }