bigbuffet-rw/app/soapbox/reducers/search.js

98 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-01-10 14:01:24 -08:00
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
import {
COMPOSE_MENTION,
COMPOSE_REPLY,
COMPOSE_DIRECT,
COMPOSE_QUOTE,
} from '../actions/compose';
2020-03-27 13:59:38 -07:00
import {
SEARCH_CHANGE,
SEARCH_CLEAR,
SEARCH_FETCH_REQUEST,
2020-03-27 13:59:38 -07:00
SEARCH_FETCH_SUCCESS,
SEARCH_SHOW,
SEARCH_FILTER_SET,
SEARCH_EXPAND_REQUEST,
SEARCH_EXPAND_SUCCESS,
2020-03-27 13:59:38 -07:00
} from '../actions/search';
const initialState = ImmutableMap({
value: '',
submitted: false,
submittedValue: '',
2020-03-27 13:59:38 -07:00
hidden: false,
results: ImmutableMap(),
filter: 'accounts',
2020-03-27 13:59:38 -07:00
});
const toIds = items => {
return ImmutableOrderedSet(items.map(item => item.id));
};
const importResults = (state, results, searchTerm, searchType) => {
return state.withMutations(state => {
if (state.get('value') === searchTerm && state.get('filter') === searchType) {
state.set('results', ImmutableMap({
accounts: toIds(results.accounts),
statuses: toIds(results.statuses),
hashtags: fromJS(results.hashtags), // it's a list of maps
accountsHasMore: results.accounts.length >= 20,
statusesHasMore: results.statuses.length >= 20,
hashtagsHasMore: results.hashtags.length >= 20,
accountsLoaded: true,
statusesLoaded: true,
hashtagsLoaded: true,
}));
state.set('submitted', true);
}
});
};
const paginateResults = (state, searchType, results, searchTerm) => {
return state.withMutations(state => {
if (state.get('value') === searchTerm) {
state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20);
state.setIn(['results', `${searchType}Loaded`], true);
state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id)));
}
});
};
const handleSubmitted = (state, value) => {
return state.withMutations(state => {
state.set('results', ImmutableMap());
state.set('submitted', true);
state.set('submittedValue', value);
});
};
2020-03-27 13:59:38 -07:00
export default function search(state = initialState, action) {
switch(action.type) {
case SEARCH_CHANGE:
return state.set('value', action.value);
2020-03-27 13:59:38 -07:00
case SEARCH_CLEAR:
return initialState;
2020-03-27 13:59:38 -07:00
case SEARCH_SHOW:
return state.set('hidden', false);
case COMPOSE_REPLY:
case COMPOSE_MENTION:
case COMPOSE_DIRECT:
case COMPOSE_QUOTE:
2020-03-27 13:59:38 -07:00
return state.set('hidden', true);
case SEARCH_FETCH_REQUEST:
return handleSubmitted(state, action.value);
2020-03-27 13:59:38 -07:00
case SEARCH_FETCH_SUCCESS:
return importResults(state, action.results, action.searchTerm, action.searchType);
case SEARCH_FILTER_SET:
return state.set('filter', action.value);
case SEARCH_EXPAND_REQUEST:
return state.setIn(['results', `${action.searchType}Loaded`], false);
case SEARCH_EXPAND_SUCCESS:
return paginateResults(state, action.searchType, action.results, action.searchTerm);
2020-03-27 13:59:38 -07:00
default:
return state;
}
2021-08-03 12:22:51 -07:00
}