lbry-desktop/ui/redux/reducers/search.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-07-27 22:04:12 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
import { handleActions } from 'util/redux-utils';
2021-03-26 09:33:30 +01:00
import { SEARCH_OPTIONS, SEARCH_PAGE_SIZE } from 'constants/search';
import { createNormalizedSearchKey } from 'util/search';
2020-07-27 22:04:12 +02:00
2020-12-03 18:29:47 +01:00
const defaultState: SearchState = {
// $FlowFixMe
2020-07-27 22:04:12 +02:00
options: {
2021-03-26 09:33:30 +01:00
[SEARCH_OPTIONS.RESULT_COUNT]: SEARCH_PAGE_SIZE,
2020-07-27 22:04:12 +02:00
[SEARCH_OPTIONS.CLAIM_TYPE]: SEARCH_OPTIONS.INCLUDE_FILES_AND_CHANNELS,
[SEARCH_OPTIONS.MEDIA_AUDIO]: true,
[SEARCH_OPTIONS.MEDIA_VIDEO]: true,
[SEARCH_OPTIONS.MEDIA_TEXT]: true,
[SEARCH_OPTIONS.MEDIA_IMAGE]: true,
[SEARCH_OPTIONS.MEDIA_APPLICATION]: true,
},
urisByQuery: {},
2021-03-26 09:33:30 +01:00
hasReachedMaxResultsLength: {},
2020-12-03 18:29:47 +01:00
searching: false,
2020-07-27 22:04:12 +02:00
};
export default handleActions(
{
[ACTIONS.SEARCH_START]: (state: SearchState): SearchState => ({
...state,
searching: true,
}),
[ACTIONS.SEARCH_SUCCESS]: (state: SearchState, action: SearchSuccess): SearchState => {
const { query, uris, from, size } = action.data;
2021-03-26 09:33:30 +01:00
const normalizedQuery = createNormalizedSearchKey(query);
let newUris = uris;
if (from !== 0 && state.urisByQuery[normalizedQuery]) {
2021-03-26 09:33:30 +01:00
newUris = Array.from(new Set(state.urisByQuery[normalizedQuery].concat(uris)));
}
// The returned number of urls is less than the page size, so we're on the last page
const noMoreResults = size && uris.length < size;
2020-07-27 22:04:12 +02:00
return {
...state,
searching: false,
2021-03-26 09:33:30 +01:00
urisByQuery: Object.assign({}, state.urisByQuery, { [normalizedQuery]: newUris }),
hasReachedMaxResultsLength: Object.assign({}, state.hasReachedMaxResultsLength, {
[normalizedQuery]: noMoreResults,
}),
2020-07-27 22:04:12 +02:00
};
},
[ACTIONS.SEARCH_FAIL]: (state: SearchState): SearchState => ({
...state,
searching: false,
}),
[ACTIONS.UPDATE_SEARCH_OPTIONS]: (state: SearchState, action: UpdateSearchOptions): SearchState => {
const { options: oldOptions } = state;
const newOptions = action.data;
const options = { ...oldOptions, ...newOptions };
return {
...state,
options,
};
},
},
defaultState
);