|
|
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 ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ] |
|
|
); |
|
|
|