/**
* WordPress dependencies
*/
import { Button, Dashicon } from '@safe-wordpress/components';
import { useDispatch, useSelect } from '@safe-wordpress/data';
import { _x } from '@safe-wordpress/i18n';
/**
* External dependencies
*/
import { LoadingAnimation } from '@nelio-content/components';
import { EMPTY_ARRAY } from '@nelio-content/constants';
import { isEmpty } from '@nelio-content/utils';
/**
* Internal dependencies
*/
import './style.scss';
import { store as NC_ANALYTICS } from '~/nelio-content-pages/analytics/store';
import { Post } from './post';
export const PostList = (): JSX.Element | null => {
const isLoading = useIsLoading();
const isPeriodIncomplete = useIsPeriodIncomplete();
const postIds = usePostIds();
const hasMore = ! useArePostsFullyLoaded();
const { loadPosts } = useDispatch( NC_ANALYTICS );
if ( isPeriodIncomplete ) {
return null;
}
if ( isLoading && isEmpty( postIds ) ) {
return (
);
}
if ( isEmpty( postIds ) ) {
return (
{ _x(
'No posts matched your criteria',
'text',
'nelio-content'
) }
);
}
return (
{ postIds.map( ( postId ) => (
) ) }
{ hasMore && (
) }
);
};
// =====
// HOOKS
// =====
const usePostIds = () =>
useSelect(
( select ) => select( NC_ANALYTICS ).getPostIds() || EMPTY_ARRAY,
[]
);
const useIsLoading = () =>
useSelect( ( select ) => select( NC_ANALYTICS ).isLoadingPosts(), [] );
const useIsPeriodIncomplete = () =>
useSelect( ( select ) => {
const { getFilterCriteria } = select( NC_ANALYTICS );
const { periodMode, periodValue } = getFilterCriteria();
const { from, to } = periodValue || {};
return 'custom' === periodMode && ( ! from || ! to );
}, [] );
const useArePostsFullyLoaded = () =>
useSelect( ( select ) => select( NC_ANALYTICS ).arePostsFullyLoaded(), [] );