Download wp-calypso/client/state/comments/selectors/get-site-comments.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/state/comments/selectors/get-site-comments.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/state/comments/selectors/get-site-comments.js
-
curl -L -o get-site-comments.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/state/comments/selectors/get-site-comments.js
1.3 kB
| 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 ] | |
| ); | |