File size: 1,301 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createSelector } from '@automattic/state-utils';
import { filter, orderBy } from 'lodash';

import 'calypso/state/comments/init';

function filterCommentsByStatus( comments, status ) {
	return 'all' === status
		? filter(
				comments,
				( comment ) => 'approved' === comment.status || 'unapproved' === comment.status
		  )
		: filter( comments, ( comment ) => status === comment.status );
}

/**
 * Returns list of loaded comments for a given site, filtered by status
 * @param {Object} state Redux state
 * @param {number} siteId Site for whose comments to find
 * @param {string} [status] Status to filter comments
 * @param {string} [order=asc] Order in which to sort filtered comments
 * @returns {Array<Object>} Available comments for site, filtered by status
 */
export const getSiteComments = 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 ]
);