Skip to content

Commit

Permalink
[Glitch] Add search results pagination to web UI (mastodon#11409)
Browse files Browse the repository at this point in the history
Port 8a4674f to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
  • Loading branch information
Gargron authored and ClearlyClaire committed Jul 28, 2019
1 parent 7f147ac commit 5141126
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
54 changes: 50 additions & 4 deletions app/javascript/flavours/glitch/actions/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const SEARCH_FETCH_REQUEST = 'SEARCH_FETCH_REQUEST';
export const SEARCH_FETCH_SUCCESS = 'SEARCH_FETCH_SUCCESS';
export const SEARCH_FETCH_FAIL = 'SEARCH_FETCH_FAIL';

export const SEARCH_EXPAND_REQUEST = 'SEARCH_EXPAND_REQUEST';
export const SEARCH_EXPAND_SUCCESS = 'SEARCH_EXPAND_SUCCESS';
export const SEARCH_EXPAND_FAIL = 'SEARCH_EXPAND_FAIL';

export function changeSearch(value) {
return {
type: SEARCH_CHANGE,
Expand Down Expand Up @@ -77,8 +81,50 @@ export function fetchSearchFail(error) {
};
};

export function showSearch() {
return {
type: SEARCH_SHOW,
};
export const expandSearch = type => (dispatch, getState) => {
const value = getState().getIn(['search', 'value']);
const offset = getState().getIn(['search', 'results', type]).size;

dispatch(expandSearchRequest());

api(getState).get('/api/v2/search', {
params: {
q: value,
type,
offset,
},
}).then(({ data }) => {
if (data.accounts) {
dispatch(importFetchedAccounts(data.accounts));
}

if (data.statuses) {
dispatch(importFetchedStatuses(data.statuses));
}

dispatch(expandSearchSuccess(data, value, type));
dispatch(fetchRelationships(data.accounts.map(item => item.id)));
}).catch(error => {
dispatch(expandSearchFail(error));
});
};

export const expandSearchRequest = () => ({
type: SEARCH_EXPAND_REQUEST,
});

export const expandSearchSuccess = (results, searchTerm, searchType) => ({
type: SEARCH_EXPAND_SUCCESS,
results,
searchTerm,
searchType,
});

export const expandSearchFail = error => ({
type: SEARCH_EXPAND_FAIL,
error,
});

export const showSearch = () => ({
type: SEARCH_SHOW,
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ImmutablePureComponent from 'react-immutable-pure-component';
import Hashtag from 'flavours/glitch/components/hashtag';
import Icon from 'flavours/glitch/components/icon';
import { searchEnabled } from 'flavours/glitch/util/initial_state';
import LoadMore from 'flavours/glitch/components/load_more';

const messages = defineMessages({
dismissSuggestion: { id: 'suggestions.dismiss', defaultMessage: 'Dismiss suggestion' },
Expand All @@ -20,15 +21,24 @@ class SearchResults extends ImmutablePureComponent {
results: ImmutablePropTypes.map.isRequired,
suggestions: ImmutablePropTypes.list.isRequired,
fetchSuggestions: PropTypes.func.isRequired,
expandSearch: PropTypes.func.isRequired,
dismissSuggestion: PropTypes.func.isRequired,
searchTerm: PropTypes.string,
intl: PropTypes.object.isRequired,
};

componentDidMount () {
this.props.fetchSuggestions();
if (this.props.searchTerm === '') {
this.props.fetchSuggestions();
}
}

handleLoadMoreAccounts = () => this.props.expandSearch('accounts');

handleLoadMoreStatuses = () => this.props.expandSearch('statuses');

handleLoadMoreHashtags = () => this.props.expandSearch('hashtags');

render () {
const { intl, results, suggestions, dismissSuggestion, searchTerm } = this.props;

Expand Down Expand Up @@ -75,6 +85,8 @@ class SearchResults extends ImmutablePureComponent {
<h5><Icon icon='users' fixedWidth /><FormattedMessage id='search_results.accounts' defaultMessage='People' /></h5>

{results.get('accounts').map(accountId => <AccountContainer id={accountId} key={accountId} />)}

{results.get('accounts').size >= 5 && <LoadMore visible onClick={this.handleLoadMoreAccounts} />}
</section>
);
}
Expand All @@ -86,6 +98,8 @@ class SearchResults extends ImmutablePureComponent {
<h5><Icon icon='quote-right' fixedWidth /><FormattedMessage id='search_results.statuses' defaultMessage='Toots' /></h5>

{results.get('statuses').map(statusId => <StatusContainer id={statusId} key={statusId}/>)}

{results.get('statuses').size >= 5 && <LoadMore visible onClick={this.handleLoadMoreStatuses} />}
</section>
);
}
Expand All @@ -97,6 +111,8 @@ class SearchResults extends ImmutablePureComponent {
<h5><Icon icon='hashtag' fixedWidth /><FormattedMessage id='search_results.hashtags' defaultMessage='Hashtags' /></h5>

{results.get('hashtags').map(hashtag => <Hashtag key={hashtag.get('name')} hashtag={hashtag} />)}

{results.get('hashtags').size >= 5 && <LoadMore visible onClick={this.handleLoadMoreHashtags} />}
</section>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { connect } from 'react-redux';
import SearchResults from '../components/search_results';
import { fetchSuggestions, dismissSuggestion } from '../../../actions/suggestions';
import { fetchSuggestions, dismissSuggestion } from 'mastodon/actions/suggestions';
import { expandSearch } from 'mastodon/actions/search';

const mapStateToProps = state => ({
results: state.getIn(['search', 'results']),
Expand All @@ -10,6 +11,7 @@ const mapStateToProps = state => ({

const mapDispatchToProps = dispatch => ({
fetchSuggestions: () => dispatch(fetchSuggestions()),
expandSearch: type => dispatch(expandSearch(type)),
dismissSuggestion: account => dispatch(dismissSuggestion(account.get('id'))),
});

Expand Down
3 changes: 3 additions & 0 deletions app/javascript/flavours/glitch/reducers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SEARCH_CLEAR,
SEARCH_FETCH_SUCCESS,
SEARCH_SHOW,
SEARCH_EXPAND_SUCCESS,
} from 'flavours/glitch/actions/search';
import {
COMPOSE_MENTION,
Expand Down Expand Up @@ -42,6 +43,8 @@ export default function search(state = initialState, action) {
statuses: ImmutableList(action.results.statuses.map(item => item.id)),
hashtags: fromJS(action.results.hashtags),
})).set('submitted', true).set('searchTerm', action.searchTerm);
case SEARCH_EXPAND_SUCCESS:
return state.updateIn(['results', action.searchType], list => list.concat(action.results[action.searchType].map(item => item.id)));
default:
return state;
}
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/flavours/glitch/styles/components/search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@
}

.search-results__info {
padding: 10px;
color: $secondary-text-color;
padding: 20px;
color: $darker-text-color;
text-align: center;
}

.trends {
Expand Down

0 comments on commit 5141126

Please sign in to comment.