2016-06-16 22:47:45 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-02-25 20:40:35 -08:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { loadEntries } from '../actions/entries';
|
2016-06-10 00:16:01 -03:00
|
|
|
import { selectEntries } from '../reducers';
|
2016-09-05 17:24:24 -03:00
|
|
|
import { Loader } from '../components/UI';
|
2016-11-11 17:54:58 -02:00
|
|
|
import EntryListing from '../components/EntryListing/EntryListing';
|
2017-04-07 09:32:30 -07:00
|
|
|
import styles from "./CollectionPage.css";
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-11-02 12:25:43 -02:00
|
|
|
class CollectionPage extends React.Component {
|
2016-10-10 15:34:21 -03:00
|
|
|
|
2016-10-03 14:33:48 +02:00
|
|
|
static propTypes = {
|
|
|
|
collection: ImmutablePropTypes.map.isRequired,
|
|
|
|
collections: ImmutablePropTypes.orderedMap.isRequired,
|
2016-11-17 11:41:54 -02:00
|
|
|
publicFolder: PropTypes.string.isRequired,
|
2016-10-03 14:33:48 +02:00
|
|
|
dispatch: PropTypes.func.isRequired,
|
2016-10-10 16:35:42 -03:00
|
|
|
page: PropTypes.number,
|
2016-10-03 14:33:48 +02:00
|
|
|
entries: ImmutablePropTypes.list,
|
2017-04-07 09:32:30 -07:00
|
|
|
isFetching: PropTypes.bool.isRequired,
|
2016-10-03 14:33:48 +02:00
|
|
|
};
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
componentDidMount() {
|
|
|
|
const { collection, dispatch } = this.props;
|
|
|
|
if (collection) {
|
|
|
|
dispatch(loadEntries(collection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
const { collection, dispatch } = this.props;
|
|
|
|
if (nextProps.collection !== collection) {
|
|
|
|
dispatch(loadEntries(nextProps.collection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 15:34:21 -03:00
|
|
|
handleLoadMore = (page) => {
|
|
|
|
const { collection, dispatch } = this.props;
|
|
|
|
dispatch(loadEntries(collection, page));
|
|
|
|
};
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
render() {
|
2017-04-07 09:32:30 -07:00
|
|
|
const { collections, collection, publicFolder, page, entries, isFetching } = this.props;
|
2016-02-25 20:40:35 -08:00
|
|
|
if (collections == null) {
|
|
|
|
return <h1>No collections defined in your config.yml</h1>;
|
|
|
|
}
|
2017-04-07 09:32:30 -07:00
|
|
|
|
|
|
|
const entriesContent = (<EntryListing
|
|
|
|
collections={collection}
|
|
|
|
entries={entries}
|
|
|
|
publicFolder={publicFolder}
|
|
|
|
page={page}
|
|
|
|
onPaginate={this.handleLoadMore}
|
|
|
|
>
|
|
|
|
{collection.get('label')}
|
|
|
|
</EntryListing>);
|
|
|
|
|
|
|
|
const fetchingEntriesContent = (<Loader active>
|
|
|
|
{['Loading Entries', 'Caching Entries', 'This might take several minutes']}
|
|
|
|
</Loader>);
|
|
|
|
const noEntriesContent = <div className={styles.noEntries}>No Entries</div>;
|
|
|
|
const fallbackContent = isFetching ? fetchingEntriesContent : noEntriesContent;
|
|
|
|
|
|
|
|
return (<div>{entries ? entriesContent : fallbackContent}</div>);
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 22:47:45 -03:00
|
|
|
|
2016-09-08 16:18:38 -03:00
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
function mapStateToProps(state, ownProps) {
|
2016-11-17 11:41:54 -02:00
|
|
|
const { collections, config } = state;
|
2016-02-25 20:40:35 -08:00
|
|
|
const { name, slug } = ownProps.params;
|
2016-11-17 11:41:54 -02:00
|
|
|
const publicFolder = config.get('public_folder');
|
2016-05-30 17:13:40 -07:00
|
|
|
const collection = name ? collections.get(name) : collections.first();
|
2016-10-10 15:34:21 -03:00
|
|
|
const page = state.entries.getIn(['pages', collection.get('name'), 'page']);
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
const entries = selectEntries(state, collection.get('name'));
|
2017-04-07 09:32:30 -07:00
|
|
|
const isFetching = state.entries.getIn(['pages', collection.get('name'), 'isFetching'], false);
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2017-04-07 09:32:30 -07:00
|
|
|
return { slug, publicFolder, collection, collections, page, entries, isFetching };
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:25:43 -02:00
|
|
|
export default connect(mapStateToProps)(CollectionPage);
|