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

53 lines
1.3 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";
import lighthouse from "lighthouse";
import { doResolveUri } from "actions/content";
import { doNavigate } from "actions/navigation";
import { selectCurrentPage } from "selectors/navigation";
import batchActions from "util/batchActions";
2017-04-24 16:17:36 +02:00
export function doSearch(query) {
2017-04-24 16:17:36 +02:00
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const page = selectCurrentPage(state);
2017-04-30 05:31:20 +02:00
2017-04-25 07:47:21 +02:00
if (!query) {
return dispatch({
type: types.SEARCH_CANCELLED,
2017-06-06 23:19:12 +02:00
});
2017-04-25 07:47:21 +02:00
}
2017-05-07 14:50:32 +02:00
dispatch({
type: types.SEARCH_STARTED,
2017-06-06 23:19:12 +02:00
data: { query },
});
2017-05-07 14:50:32 +02:00
2017-06-06 23:19:12 +02:00
if (page != "search") {
dispatch(doNavigate("search", { query: query }));
} else {
lighthouse.search(query).then(results => {
const actions = [];
results.forEach(result => {
const uri = lbryuri.build({
channelName: result.channel_name,
contentName: result.name,
claimId: result.channel_id || result.claim_id,
2017-06-06 23:19:12 +02:00
});
actions.push(doResolveUri(uri));
2017-06-06 23:19:12 +02:00
});
2017-05-07 14:50:32 +02:00
actions.push({
type: types.SEARCH_COMPLETED,
data: {
query,
results,
2017-06-06 23:19:12 +02:00
},
});
dispatch(batchActions(...actions));
2017-06-06 23:19:12 +02:00
});
}
2017-06-06 23:19:12 +02:00
};
2017-05-07 14:50:32 +02:00
}