Keep active search results tab

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2021-08-02 20:51:15 +02:00
parent 29cdc4867b
commit bd16715763
5 changed files with 23 additions and 13 deletions

View File

@ -10,12 +10,12 @@ 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_FILTER_SET = 'SEARCH_FILTER_SET';
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 const SEARCH_FILTER_SET = 'SEARCH_FILTER_SET';
export function changeSearch(value) {
return {
type: SEARCH_CHANGE,
@ -82,6 +82,14 @@ export function fetchSearchFail(error) {
};
};
export const setFilter = filterType => dispatch => {
dispatch({
type: SEARCH_FILTER_SET,
path: ['search', 'filter'],
value: filterType,
});
};
export const expandSearch = type => (dispatch, getState) => {
const value = getState().getIn(['search', 'value']);
const offset = getState().getIn(['search', 'results', type]).size;

View File

@ -16,21 +16,15 @@ export default class SearchResults extends ImmutablePureComponent {
results: ImmutablePropTypes.map.isRequired,
submitted: PropTypes.bool,
expandSearch: PropTypes.func.isRequired,
};
state = {
selectedFilter: 'accounts',
selectedFilter: PropTypes.string.isRequired,
};
handleLoadMore = () => this.props.expandSearch(this.state.selectedFilter);
handleSelectFilter = newActiveFilter => {
this.setState({ selectedFilter: newActiveFilter });
};
handleSelectFilter = newActiveFilter => this.props.selectFilter(newActiveFilter);
render() {
const { results, submitted } = this.props;
const { selectedFilter } = this.state;
const { results, submitted, selectedFilter } = this.props;
if (submitted && results.isEmpty()) {
return (

View File

@ -1,13 +1,14 @@
import { connect } from 'react-redux';
import SearchResults from '../components/search_results';
import { fetchSuggestions, dismissSuggestion } from '../../../actions/suggestions';
import { expandSearch } from '../../../actions/search';
import { expandSearch, setFilter } from '../../../actions/search';
const mapStateToProps = state => {
return {
results: state.getIn(['search', 'results']),
suggestions: state.getIn(['suggestions', 'items']),
submitted: state.getIn(['search', 'submitted']),
selectedFilter: state.getIn(['search', 'filter']),
};
};
@ -15,6 +16,7 @@ const mapDispatchToProps = dispatch => ({
fetchSuggestions: () => dispatch(fetchSuggestions()),
expandSearch: type => dispatch(expandSearch(type)),
dismissSuggestion: account => dispatch(dismissSuggestion(account.get('id'))),
selectFilter: newActiveFilter => dispatch(setFilter(newActiveFilter)),
});
export default connect(mapStateToProps, mapDispatchToProps)(SearchResults);

View File

@ -188,6 +188,7 @@
"column.public": "Globalna oś czasu",
"column.remote": "Sfederowana oś czasu",
"column.scheduled_statuses": "Zaplanowane wpisy",
"column.search": "Szukaj",
"column.security": "Bezpieczeństwo",
"column.soapbox_config": "Konfiguracja Soapbox",
"column_back_button.label": "Wróć",

View File

@ -4,6 +4,7 @@ import {
SEARCH_FETCH_REQUEST,
SEARCH_FETCH_SUCCESS,
SEARCH_SHOW,
SEARCH_FILTER_SET,
SEARCH_EXPAND_SUCCESS,
} from '../actions/search';
import {
@ -18,6 +19,7 @@ const initialState = ImmutableMap({
submitted: false,
hidden: false,
results: ImmutableMap(),
filter: 'accounts',
});
export default function search(state = initialState, action) {
@ -33,6 +35,7 @@ export default function search(state = initialState, action) {
map.set('results', ImmutableMap());
map.set('submitted', false);
map.set('hidden', false);
map.set('filter', 'accounts');
});
case SEARCH_SHOW:
return state.set('hidden', false);
@ -53,7 +56,9 @@ export default function search(state = initialState, action) {
accountsHasMore: action.results.accounts.length >= 20,
statusesHasMore: action.results.statuses.length >= 20,
hashtagsHasMore: action.results.hashtags.length >= 20,
})).set('submitted', true);
})).set('submitted', true).set('filter', 'accounts');
case SEARCH_FILTER_SET:
return state.set('filter', action.value);
case SEARCH_EXPAND_SUCCESS:
return state.withMutations((state) => {
state.setIn(['results', `${action.searchType}HasMore`], action.results[action.searchType].length >= 20);