2017-04-28 16:37:08 +09:00
|
|
|
import { Components, registerComponent, withList, withCurrentUser, Utils } from 'meteor/vulcan:core';
|
2016-04-14 10:12:35 +09:00
|
|
|
import React from 'react';
|
2017-05-19 14:42:43 -06:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-23 16:27:59 +09:00
|
|
|
import Posts from 'meteor/vulcan:posts';
|
2017-02-03 15:36:40 +09:00
|
|
|
import { Alert } from 'react-bootstrap';
|
2017-04-07 16:06:33 +09:00
|
|
|
import { FormattedMessage, intlShape } from 'react-intl';
|
2017-05-23 09:48:48 +09:00
|
|
|
import classNames from 'classnames';
|
2016-04-14 10:12:35 +09:00
|
|
|
|
2017-04-07 16:06:33 +09:00
|
|
|
const Error = ({error}) => <Alert className="flash-message" bsStyle="danger"><FormattedMessage id={error.id} values={{value: error.value}}/>{error.message}</Alert>
|
2017-03-05 08:09:53 +00:00
|
|
|
|
2017-05-23 09:48:48 +09:00
|
|
|
const PostsList = ({className, results, loading, count, totalCount, loadMore, showHeader = true, showLoadMore = true, networkStatus, currentUser, error, terms}) => {
|
2017-01-09 22:09:09 +09:00
|
|
|
|
|
|
|
const loadingMore = networkStatus === 2;
|
2016-11-10 15:39:04 +09:00
|
|
|
|
2017-03-05 08:09:53 +00:00
|
|
|
if (results && results.length) {
|
2016-11-11 18:19:18 +09:00
|
|
|
|
|
|
|
const hasMore = totalCount > results.length;
|
|
|
|
|
2016-04-14 10:12:35 +09:00
|
|
|
return (
|
2017-05-23 09:48:48 +09:00
|
|
|
<div className={classNames(className, 'posts-list')}>
|
2016-12-06 18:06:29 +01:00
|
|
|
{showHeader ? <Components.PostsListHeader/> : null}
|
2017-04-07 16:06:33 +09:00
|
|
|
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
|
2016-04-19 15:45:36 +09:00
|
|
|
<div className="posts-list-content">
|
2017-02-06 10:43:55 +09:00
|
|
|
{results.map(post => <Components.PostsItem post={post} key={post._id} currentUser={currentUser} terms={terms} />)}
|
2016-04-14 10:12:35 +09:00
|
|
|
</div>
|
2017-04-28 16:37:08 +09:00
|
|
|
{showLoadMore ? hasMore ? (loadingMore ? <Components.PostsLoading/> : <Components.PostsLoadMore loadMore={loadMore} count={count} totalCount={totalCount} />) : <Components.PostsNoMore/> : null}
|
2016-04-14 10:12:35 +09:00
|
|
|
</div>
|
|
|
|
)
|
2016-11-10 15:39:04 +09:00
|
|
|
} else if (loading) {
|
|
|
|
return (
|
2017-05-23 09:48:48 +09:00
|
|
|
<div className={classNames(className, 'posts-list')}>
|
2016-12-06 18:06:29 +01:00
|
|
|
{showHeader ? <Components.PostsListHeader /> : null}
|
2017-04-07 16:06:33 +09:00
|
|
|
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
|
2016-11-10 15:39:04 +09:00
|
|
|
<div className="posts-list-content">
|
2016-12-06 18:06:29 +01:00
|
|
|
<Components.PostsLoading/>
|
2016-11-10 15:39:04 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2016-04-14 10:12:35 +09:00
|
|
|
} else {
|
|
|
|
return (
|
2017-05-23 09:48:48 +09:00
|
|
|
<div className={classNames(className, 'posts-list')}>
|
2016-12-06 18:06:29 +01:00
|
|
|
{showHeader ? <Components.PostsListHeader /> : null}
|
2017-04-07 16:06:33 +09:00
|
|
|
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
|
2016-04-19 15:45:36 +09:00
|
|
|
<div className="posts-list-content">
|
2016-12-06 18:06:29 +01:00
|
|
|
<Components.PostsNoResults/>
|
2016-04-14 10:12:35 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-05-22 16:42:24 +09:00
|
|
|
PostsList.displayName = "PostsList";
|
|
|
|
|
2016-11-10 15:39:04 +09:00
|
|
|
PostsList.propTypes = {
|
2017-05-19 14:42:43 -06:00
|
|
|
results: PropTypes.array,
|
|
|
|
terms: PropTypes.object,
|
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
count: PropTypes.number,
|
|
|
|
totalCount: PropTypes.number,
|
|
|
|
loadMore: PropTypes.func,
|
|
|
|
showHeader: PropTypes.bool,
|
2016-11-10 15:39:04 +09:00
|
|
|
};
|
|
|
|
|
2017-04-07 16:06:33 +09:00
|
|
|
PostsList.contextTypes = {
|
|
|
|
intl: intlShape
|
|
|
|
};
|
|
|
|
|
2016-11-23 11:07:48 +09:00
|
|
|
const options = {
|
|
|
|
collection: Posts,
|
|
|
|
queryName: 'postsListQuery',
|
2017-01-30 19:46:48 +09:00
|
|
|
fragmentName: 'PostsList',
|
2016-11-19 20:01:17 +01:00
|
|
|
};
|
|
|
|
|
2017-01-30 19:46:48 +09:00
|
|
|
registerComponent('PostsList', PostsList, withCurrentUser, [withList, options]);
|