import React from 'react'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { injectIntl, defineMessages, FormattedMessage } from 'react-intl'; import { connect } from 'react-redux'; import { expandSearch } from 'flavours/glitch/actions/search'; import Account from 'flavours/glitch/containers/account_container'; import Status from 'flavours/glitch/containers/status_container'; import { ImmutableHashtag as Hashtag } from 'flavours/glitch/components/hashtag'; import { List as ImmutableList } from 'immutable'; import LoadMore from 'flavours/glitch/components/load_more'; import LoadingIndicator from 'flavours/glitch/components/loading_indicator'; import { title } from 'flavours/glitch/util/initial_state'; import { Helmet } from 'react-helmet'; const messages = defineMessages({ title: { id: 'search_results.title', defaultMessage: 'Search for {q}' }, }); const mapStateToProps = state => ({ isLoading: state.getIn(['search', 'isLoading']), results: state.getIn(['search', 'results']), q: state.getIn(['search', 'searchTerm']), }); const appendLoadMore = (id, list, onLoadMore) => { if (list.size >= 5) { return list.push(); } else { return list; } }; const renderAccounts = (results, onLoadMore) => appendLoadMore('accounts', results.get('accounts', ImmutableList()).map(item => ( )), onLoadMore); const renderHashtags = (results, onLoadMore) => appendLoadMore('hashtags', results.get('hashtags', ImmutableList()).map(item => ( )), onLoadMore); const renderStatuses = (results, onLoadMore) => appendLoadMore('statuses', results.get('statuses', ImmutableList()).map(item => ( )), onLoadMore); export default @connect(mapStateToProps) @injectIntl class Results extends React.PureComponent { static propTypes = { results: ImmutablePropTypes.map, isLoading: PropTypes.bool, multiColumn: PropTypes.bool, dispatch: PropTypes.func.isRequired, q: PropTypes.string, intl: PropTypes.object, }; state = { type: 'all', }; handleSelectAll = () => this.setState({ type: 'all' }); handleSelectAccounts = () => this.setState({ type: 'accounts' }); handleSelectHashtags = () => this.setState({ type: 'hashtags' }); handleSelectStatuses = () => this.setState({ type: 'statuses' }); handleLoadMoreAccounts = () => this.loadMore('accounts'); handleLoadMoreStatuses = () => this.loadMore('statuses'); handleLoadMoreHashtags = () => this.loadMore('hashtags'); loadMore (type) { const { dispatch } = this.props; dispatch(expandSearch(type)); } render () { const { intl, isLoading, q, results } = this.props; const { type } = this.state; let filteredResults = ImmutableList(); if (!isLoading) { switch(type) { case 'all': filteredResults = filteredResults.concat(renderAccounts(results, this.handleLoadMoreAccounts), renderHashtags(results, this.handleLoadMoreHashtags), renderStatuses(results, this.handleLoadMoreStatuses)); break; case 'accounts': filteredResults = filteredResults.concat(renderAccounts(results, this.handleLoadMoreAccounts)); break; case 'hashtags': filteredResults = filteredResults.concat(renderHashtags(results, this.handleLoadMoreHashtags)); break; case 'statuses': filteredResults = filteredResults.concat(renderStatuses(results, this.handleLoadMoreStatuses)); break; } if (filteredResults.size === 0) { filteredResults = (
); } } return (
{isLoading ? : filteredResults}
{intl.formatMessage(messages.title, { q })} - {title}
); } }