static-cms/src/containers/SearchPage.js

65 lines
2.0 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
2016-07-08 07:14:00 -03:00
import { connect } from 'react-redux';
import { selectSearchedEntries } from '../reducers';
import { searchEntries } from '../actions/entries';
import { Loader } from '../components/UI';
import EntryListing from '../components/EntryListing/EntryListing';
2016-11-02 12:25:43 -02:00
import styles from './breakpoints.css';
2016-07-08 07:14:00 -03:00
class SearchPage extends React.Component {
static propTypes = {
isFetching: PropTypes.bool,
searchEntries: PropTypes.func.isRequired,
searchTerm: PropTypes.string.isRequired,
2016-11-02 12:25:43 -02:00
entries: ImmutablePropTypes.list,
};
componentDidMount() {
const { searchTerm, searchEntries } = this.props;
searchEntries(searchTerm);
}
componentWillReceiveProps(nextProps) {
if (this.props.searchTerm === nextProps.searchTerm) return;
const { searchEntries } = this.props;
searchEntries(nextProps.searchTerm);
}
handleLoadMore = (page) => {
const { searchTerm, searchEntries } = this.props;
searchEntries(searchTerm, page);
};
2016-07-08 07:14:00 -03:00
render() {
const { collections, searchTerm, entries, isFetching, page } = this.props;
2016-11-02 12:25:43 -02:00
return (<div className={styles.root}>
{(isFetching === true || !entries) ?
<Loader active>{['Loading Entries', 'Caching Entries', 'This might take several minutes']}</Loader>
:
2016-11-02 12:25:43 -02:00
<EntryListing collections={collections} entries={entries} page={page} onPaginate={this.handleLoadMore}>
Results for {searchTerm}
</EntryListing>
}
2016-11-02 12:25:43 -02:00
</div>);
2016-07-08 07:14:00 -03:00
}
}
function mapStateToProps(state, ownProps) {
const isFetching = state.entries.getIn(['search', 'isFetching']);
const page = state.entries.getIn(['search', 'page']);
const entries = selectSearchedEntries(state);
const collections = state.collections.toIndexedSeq();
const searchTerm = ownProps.params && ownProps.params.searchTerm;
return { isFetching, page, collections, entries, searchTerm };
}
export default connect(
mapStateToProps,
{ searchEntries }
)(SearchPage);