2017-06-06 17:19:12 -04:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import lbryuri from "lbryuri";
|
|
|
|
import lighthouse from "lighthouse";
|
|
|
|
import { doResolveUri } from "actions/content";
|
|
|
|
import { doNavigate, doHistoryPush } from "actions/app";
|
|
|
|
import { selectCurrentPage } from "selectors/app";
|
2017-06-08 12:11:41 +07:00
|
|
|
import batchActions from "util/batchActions";
|
2017-04-24 21:17:36 +07:00
|
|
|
|
2017-05-10 20:59:47 -04:00
|
|
|
export function doSearch(query) {
|
2017-04-24 21:17:36 +07:00
|
|
|
return function(dispatch, getState) {
|
2017-06-06 17:19:12 -04:00
|
|
|
const state = getState();
|
|
|
|
const page = selectCurrentPage(state);
|
2017-04-30 10:31:20 +07:00
|
|
|
|
2017-04-25 12:47:21 +07:00
|
|
|
if (!query) {
|
|
|
|
return dispatch({
|
|
|
|
type: types.SEARCH_CANCELLED,
|
2017-06-06 17:19:12 -04:00
|
|
|
});
|
2017-04-25 12:47:21 +07:00
|
|
|
}
|
|
|
|
|
2017-05-07 19:50:32 +07:00
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_STARTED,
|
2017-06-06 17:19:12 -04:00
|
|
|
data: { query },
|
|
|
|
});
|
2017-05-07 19:50:32 +07:00
|
|
|
|
2017-06-06 17:19:12 -04:00
|
|
|
if (page != "search") {
|
|
|
|
dispatch(doNavigate("search", { query: query }));
|
2017-05-17 17:52:45 -04:00
|
|
|
} else {
|
2017-05-23 12:46:52 +04:00
|
|
|
lighthouse.search(query).then(results => {
|
2017-06-08 12:11:41 +07:00
|
|
|
const actions = [];
|
|
|
|
|
2017-05-23 12:46:52 +04:00
|
|
|
results.forEach(result => {
|
|
|
|
const uri = lbryuri.build({
|
|
|
|
channelName: result.channel_name,
|
|
|
|
contentName: result.name,
|
|
|
|
claimId: result.channel_id || result.claim_id,
|
2017-06-06 17:19:12 -04:00
|
|
|
});
|
2017-06-08 12:11:41 +07:00
|
|
|
actions.push(doResolveUri(uri));
|
2017-06-06 17:19:12 -04:00
|
|
|
});
|
2017-05-07 19:50:32 +07:00
|
|
|
|
2017-06-08 12:11:41 +07:00
|
|
|
actions.push({
|
2017-05-23 12:46:52 +04:00
|
|
|
type: types.SEARCH_COMPLETED,
|
|
|
|
data: {
|
|
|
|
query,
|
|
|
|
results,
|
2017-06-06 17:19:12 -04:00
|
|
|
},
|
|
|
|
});
|
2017-06-08 12:11:41 +07:00
|
|
|
|
|
|
|
dispatch(batchActions(...actions));
|
2017-06-06 17:19:12 -04:00
|
|
|
});
|
2017-05-23 12:46:52 +04:00
|
|
|
}
|
2017-06-06 17:19:12 -04:00
|
|
|
};
|
2017-05-07 19:50:32 +07:00
|
|
|
}
|