Vulcan/packages/vulcan-base-components/lib/posts/PostsList.jsx

77 lines
2.6 KiB
React
Raw Normal View History

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-06-02 07:19:39 +09:00
import Alert from 'react-bootstrap/lib/Alert'
2017-06-01 11:42:30 +09:00
import { FormattedMessage, intlShape } from 'meteor/vulcan:i18n';
2017-05-23 09:48:48 +09:00
import classNames from 'classnames';
2016-04-14 10:12:35 +09:00
const Error = ({error}) => <Alert className="flash-message" bsStyle="danger"><FormattedMessage id={error.id} values={{value: error.value}}/>{error.message}</Alert>
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;
if (results && results.length) {
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')}>
{showHeader ? <Components.PostsListHeader/> : null}
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
<div className="posts-list-content">
{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>
)
} else if (loading) {
return (
2017-05-23 09:48:48 +09:00
<div className={classNames(className, 'posts-list')}>
{showHeader ? <Components.PostsListHeader /> : null}
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
<div className="posts-list-content">
<Components.PostsLoading/>
</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')}>
{showHeader ? <Components.PostsListHeader /> : null}
{error ? <Error error={Utils.decodeIntlError(error)} /> : null }
<div className="posts-list-content">
<Components.PostsNoResults/>
2016-04-14 10:12:35 +09:00
</div>
</div>
)
}
};
PostsList.displayName = "PostsList";
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,
};
PostsList.contextTypes = {
intl: intlShape
};
2016-11-23 11:07:48 +09:00
const options = {
collection: Posts,
queryName: 'postsListQuery',
fragmentName: 'PostsList',
};
registerComponent('PostsList', PostsList, withCurrentUser, [withList, options]);