/** * Gets comment items for post * * @param {object} state redux state * @param {number} siteId site identification * @param {number} postId site identification * @returns {Array} comment items */ export function getPostCommentItems( state, siteId, postId ) { return state.comments.items[ `${ siteId }-${ postId }` ]; }
createSelector( ( state, siteId, commentId ) => { const comment = getSiteComment( state, siteId, commentId ); const parentId = comment?.parent?.ID ?? 0; if ( ! comment ) { return 0; } return parentId ? 1 + getSiteCommentParentDepth( state, siteId, parentId ) : 0; }, ( state, siteId ) => [ state.comments.trees[ siteId ] ] )
test('it should handle nested ordering with one ASC and one DESC', async t => { const query = makeQuery(false) const { data, errors } = await run(query) errCheck(t, errors) t.deepEqual( [{ id: 8 }, { id: 7 }, { id: 6 }, { id: 5 }, { id: 4 }], data.user.posts[0].comments ) t.deepEqual([{ id: 1 }, { id: 4 }, { id: 6 }, { id: 8 }], data.user.comments) })
export function getCommentById( { state, commentId, siteId } ) { const errorKey = getErrorKey( siteId, commentId ); if ( state.comments.errors[ errorKey ] ) { return state.comments.errors[ errorKey ]; } const commentsForSite = flatMap( filter( state.comments.items, ( comment, key ) => { return deconstructStateKey( key ).siteId === siteId; } ) ); return find( commentsForSite, ( comment ) => commentId === comment.ID ); }
createSelector( ( state, siteId, status, order = 'asc' ) => { const comments = state.comments.items ?? {}; const parsedComments = Object.keys( comments ) .filter( ( key ) => parseInt( key.split( '-', 1 ), 10 ) === siteId ) .reduce( ( list, key ) => [ ...list, ...comments[ key ] ], [] ); return status ? orderBy( filterCommentsByStatus( parsedComments, status ), 'date', order ) : orderBy( parsedComments, 'date', order ); }, ( state ) => [ state.comments.items ] )
export function commentsFetchingStatus( state, siteId, postId, commentTotal = 0 ) { const fetchStatus = state.comments.fetchStatus[ getStateKey( siteId, postId ) ] ?? fetchStatusInitialState; const hasMoreComments = commentTotal > size( getPostCommentItems( state, siteId, postId ) ); return { haveEarlierCommentsToFetch: fetchStatus.before && hasMoreComments, haveLaterCommentsToFetch: fetchStatus.after && hasMoreComments, hasReceivedBefore: fetchStatus.hasReceivedBefore, hasReceivedAfter: fetchStatus.hasReceivedAfter, }; }
createSelector( ( state, siteId, status, commentParentId ) => filter( getSiteCommentsTree( state, siteId, status ), { commentParentId } ), ( state, siteId ) => [ state.comments.trees[ siteId ] ] )
describe( 'wpcom.site.post.comments', function () { it( 'should get the post comments list', function ( done ) { testing_post.comments( function ( err, data ) { if ( err ) throw err; assert.equal( 'number', typeof data.found ); assert.equal( 'object', typeof data.comments ); assert.ok( data.comments instanceof Array ); done(); } ); } ); } );
/** * Return comment counts for the given site and post ID, if applicable. * * @param {object} state Redux state * @param {number} siteId Site identifier * @param {number} [postId] Post identifier * @returns {object} The requested comment counts */ export function getSiteCommentCounts( state, siteId, postId ) { if ( postId ) { return state.comments.counts[ siteId ]?.[ postId ] ?? null; } return state.comments.counts[ siteId ]?.site ?? null; }
/** * Returns the active reply comment for a given site and post. * * @param {object} options options object. * @param {object} options.state Global state tree * @param {number} options.siteId The ID of the site we're querying * @param {number} options.postId The ID of the post we're querying * @returns {number|string} commentId Can be a string if the comment is a placeholder */ export function getActiveReplyCommentId( { state, siteId, postId } ) { return state.comments.activeReplies[ getStateKey( siteId, postId ) ] ?? null; }
/** * Get total number of comments on the server for a given post * * @param {object} state redux state * @param {number} siteId site identification * @param {number} postId site identification * @returns {number} total comments count on the server. if not found, assume infinity */ export function getPostTotalCommentsCount( state, siteId, postId ) { return state.comments.totalCommentsCount[ `${ siteId }-${ postId }` ]; }
export function getCommentErrors( state ) { return state.comments.errors; }
export function getExpansionsForPost( state, siteId, postId ) { return state.comments.expansions[ getStateKey( siteId, postId ) ]; }
test('it should handle nested ordering with both ASC', async t => { const query = makeQuery(true) const { data, errors } = await run(query) errCheck(t, errors) t.deepEqual( [{ id: 4 }, { id: 5 }, { id: 6 }, { id: 7 }, { id: 8 }], data.user.posts[0].comments ) t.deepEqual([{ id: 1 }, { id: 4 }, { id: 6 }, { id: 8 }], data.user.comments) })