bigbuffet-rw/app/soapbox/features/compose/components/search_results.js

109 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
2020-03-27 13:59:38 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
import AccountContainer from '../../../containers/account_container';
import StatusContainer from '../../../containers/status_container';
import ImmutablePureComponent from 'react-immutable-pure-component';
import Hashtag from '../../../components/hashtag';
import LoadingIndicator from 'soapbox/components/loading_indicator';
import FilterBar from '../../search/components/filter_bar';
import LoadMore from '../../../components/load_more';
import classNames from 'classnames';
2020-03-27 13:59:38 -07:00
export default class SearchResults extends ImmutablePureComponent {
2020-03-27 13:59:38 -07:00
static propTypes = {
value: ImmutablePropTypes.string,
2020-03-27 13:59:38 -07:00
results: ImmutablePropTypes.map.isRequired,
submitted: PropTypes.bool,
expandSearch: PropTypes.func.isRequired,
selectedFilter: PropTypes.string.isRequired,
selectFilter: PropTypes.func.isRequired,
};
handleLoadMore = () => this.props.expandSearch(this.props.selectedFilter);
handleSelectFilter = newActiveFilter => this.props.selectFilter(newActiveFilter);
render() {
const { value, results, submitted, selectedFilter } = this.props;
2020-03-27 13:59:38 -07:00
if (submitted && results.isEmpty()) {
2020-03-27 13:59:38 -07:00
return (
<div className='search-results'>
<LoadingIndicator />
2020-03-27 13:59:38 -07:00
</div>
);
}
let searchResults;
let hasMore = false;
2020-03-27 13:59:38 -07:00
if (selectedFilter === 'accounts' && results.get('accounts')) {
hasMore = results.get('accountsHasMore');
2020-03-27 13:59:38 -07:00
searchResults = results.get('accounts').size > 0 ? (
<div className={classNames('search-results__section', { 'has-more': hasMore })}>
2020-03-27 13:59:38 -07:00
{results.get('accounts').map(accountId => <AccountContainer key={accountId} id={accountId} />)}
</div>
) : (
<div className='empty-column-indicator'>
<FormattedMessage
id='empty_column.search.accounts'
defaultMessage='There are no people results for "{term}"'
values={{ term: value }}
/>
</div>
2020-03-27 13:59:38 -07:00
);
}
if (selectedFilter === 'statuses' && results.get('statuses')) {
hasMore = results.get('statusesHasMore');
2020-03-27 13:59:38 -07:00
searchResults = results.get('statuses').size > 0 ? (
<div className={classNames('search-results__section', { 'has-more': hasMore })}>
2020-03-27 13:59:38 -07:00
{results.get('statuses').map(statusId => <StatusContainer key={statusId} id={statusId} />)}
</div>
) : (
<div className='empty-column-indicator'>
<FormattedMessage
id='empty_column.search.statuses'
defaultMessage='There are no posts results for "{term}"'
values={{ term: value }}
/>
</div>
2020-03-27 13:59:38 -07:00
);
}
if (selectedFilter === 'hashtags' && results.get('hashtags')) {
hasMore = results.get('hashtagsHasMore');
2020-03-27 13:59:38 -07:00
searchResults = results.get('hashtags').size > 0 ? (
<div className={classNames('search-results__section', { 'has-more': hasMore })}>
2020-03-27 13:59:38 -07:00
{results.get('hashtags').map(hashtag => <Hashtag key={hashtag.get('name')} hashtag={hashtag} />)}
</div>
) : (
<div className='empty-column-indicator'>
<FormattedMessage
id='empty_column.search.hashtags'
defaultMessage='There are no hashtags results for "{term}"'
values={{ term: value }}
/>
</div>
2020-03-27 13:59:38 -07:00
);
}
return (
<>
{submitted && <FilterBar selectedFilter={submitted ? selectedFilter : null} selectFilter={this.handleSelectFilter} />}
{searchResults}
{hasMore && <LoadMore visible onClick={this.handleLoadMore} />}
</>
2020-03-27 13:59:38 -07:00
);
}
}