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

43 lines
1 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import lbryuri from "lbryuri";
2017-04-24 16:17:36 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
const defaultState = {};
2017-04-24 16:17:36 +02:00
reducers[types.SEARCH_STARTED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { query } = action.data;
2017-04-24 16:17:36 +02:00
return Object.assign({}, state, {
searching: true,
query: query,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-24 16:17:36 +02:00
reducers[types.SEARCH_COMPLETED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { query, results } = action.data;
const oldResults = Object.assign({}, state.results);
const newByQuery = Object.assign({}, oldResults.byQuery);
newByQuery[query] = results;
2017-04-25 07:47:21 +02:00
const newResults = Object.assign({}, oldResults, {
2017-06-06 23:19:12 +02:00
byQuery: newByQuery,
});
2017-04-24 16:17:36 +02:00
return Object.assign({}, state, {
searching: false,
results: newResults,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-24 16:17:36 +02:00
2017-04-25 07:47:21 +02:00
reducers[types.SEARCH_CANCELLED] = function(state, action) {
return Object.assign({}, state, {
searching: false,
query: undefined,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-25 07:47:21 +02:00
2017-04-24 16:17:36 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}