static-cms/src/containers/SearchPage.js

82 lines
2.5 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 as actionSearchEntries, clearSearch as actionClearSearch } from '../actions/search';
import { Loader } from '../components/UI';
import EntryListing from '../components/EntryListing/EntryListing';
2016-07-08 07:14:00 -03:00
class SearchPage extends React.Component {
static propTypes = {
isFetching: PropTypes.bool,
searchEntries: PropTypes.func.isRequired,
clearSearch: PropTypes.func.isRequired,
searchTerm: PropTypes.string.isRequired,
2016-12-02 15:19:08 -02:00
collections: ImmutablePropTypes.seq,
2016-11-02 12:25:43 -02:00
entries: ImmutablePropTypes.list,
2016-12-02 15:19:08 -02:00
page: PropTypes.number,
publicFolder: PropTypes.string,
};
componentDidMount() {
const { searchTerm, searchEntries } = this.props;
searchEntries(searchTerm);
}
componentWillReceiveProps(nextProps) {
if (this.props.searchTerm === nextProps.searchTerm) return;
const { searchEntries } = this.props;
searchEntries(nextProps.searchTerm);
}
componentWillUnmount() {
this.props.clearSearch();
}
handleLoadMore = (page) => {
const { searchTerm, searchEntries } = this.props;
if (!isNaN(page)) searchEntries(searchTerm, page);
};
2016-07-08 07:14:00 -03:00
render() {
2016-12-02 15:19:08 -02:00
const { collections, searchTerm, entries, isFetching, page, publicFolder } = this.props;
return (<div>
{(isFetching === true || !entries) ?
<Loader active>{['Loading Entries', 'Caching Entries', 'This might take several minutes']}</Loader>
:
<EntryListing
collections={collections}
entries={entries}
page={page}
publicFolder={publicFolder}
onPaginate={this.handleLoadMore}
>
2016-12-02 15:19:08 -02:00
Results for {searchTerm}
2016-11-02 12:25:43 -02:00
</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();
2016-12-02 15:19:08 -02:00
const publicFolder = state.config.get('public_folder');
const searchTerm = ownProps.params && ownProps.params.searchTerm;
2016-12-02 15:19:08 -02:00
return { isFetching, page, collections, entries, publicFolder, searchTerm };
}
export default connect(
mapStateToProps,
{
searchEntries: actionSearchEntries,
clearSearch: actionClearSearch,
}
)(SearchPage);