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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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