Skip to content

Commit

Permalink
Data Layer: Update comments to use the new v1.1 comments tree endpoin…
Browse files Browse the repository at this point in the history
  • Loading branch information
kwight authored Oct 20, 2017
1 parent 68f8a60 commit f0b4919
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
19 changes: 15 additions & 4 deletions client/state/data-layer/wpcom/sites/comments-tree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { translate } from 'i18n-calypso';
import { map } from 'lodash';
import { map, flatMap, flatten } from 'lodash';

/**
* Internal dependencies
Expand All @@ -25,7 +25,7 @@ export const fetchCommentsTreeForSite = ( { dispatch }, action ) => {
{
method: 'GET',
path: `/sites/${ siteId }/comments-tree`,
apiVersion: '1',
apiVersion: '1.1',
query: {
status: 'unapproved' === status ? 'pending' : status,
},
Expand All @@ -35,14 +35,25 @@ export const fetchCommentsTreeForSite = ( { dispatch }, action ) => {
);
};

const mapTree = ( tree, status, type ) =>
map( tree, ( [ commentId, postId, commentParentId ] ) => ( {
const mapPosts = ( commentIds, apiPostId ) => {
const postId = parseInt( apiPostId, 10 );
const [ topLevelIds, replyIds ] = commentIds;

return flatten( [
topLevelIds.map( commentId => [ commentId, postId, 0 ] ),
replyIds.map( ( [ commentId, commentParentId ] ) => [ commentId, postId, commentParentId ] ),
] );
};

const mapTree = ( tree, status, type ) => {
return map( flatMap( tree, mapPosts ), ( [ commentId, postId, commentParentId ] ) => ( {
commentId,
commentParentId,
postId,
status,
type,
} ) );
};

export const addCommentsTree = ( { dispatch }, { query }, data ) => {
const { siteId, status } = query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe( 'comments-tree', () => {
method: 'GET',
path: '/sites/77203074/comments-tree',
query: { status: 'approved' },
apiVersion: '1',
apiVersion: '1.1',
},
action
)
Expand All @@ -39,9 +39,9 @@ describe( 'comments-tree', () => {
test( 'should dispatch comment tree updates', () => {
const dispatch = spy();
addCommentsTree( { dispatch }, action, {
comments_tree: [ [ 2, 1, 0 ] ],
pingbacks_tree: [ [ 3, 1, 0 ] ],
trackbacks_tree: [ [ 4, 1, 0 ] ],
comments_tree: { 1: [ [ 2 ], [] ] },
pingbacks_tree: { 1: [ [ 3 ], [] ] },
trackbacks_tree: { 1: [ [ 4 ], [] ] },
} );
expect( dispatch ).to.have.been.calledOnce;
expect( dispatch ).to.have.been.calledWith( {
Expand Down

0 comments on commit f0b4919

Please sign in to comment.