2017-06-06 23:19:12 +02:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import lbryuri from "lbryuri";
|
|
|
|
import { doResolveUri } from "actions/content";
|
2017-08-30 14:48:32 +02:00
|
|
|
import { doNavigate } from "actions/navigation";
|
|
|
|
import { selectCurrentPage } from "selectors/navigation";
|
2017-06-08 07:11:41 +02:00
|
|
|
import batchActions from "util/batchActions";
|
2017-04-24 16:17:36 +02:00
|
|
|
|
2017-10-12 15:37:24 +02:00
|
|
|
export function doSearch(rawQuery) {
|
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-10-13 15:32:49 +02:00
|
|
|
const query = rawQuery.replace(/^lbry:\/\//i, "");
|
2017-10-12 15:37:24 +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 }));
|
2017-05-17 23:52:45 +02:00
|
|
|
} else {
|
2017-10-06 23:46:41 +02:00
|
|
|
fetch("https://lighthouse.lbry.io/search?s=" + query)
|
|
|
|
.then(response => {
|
|
|
|
return response.status === 200
|
|
|
|
? Promise.resolve(response.json())
|
|
|
|
: Promise.reject(new Error(response.statusText));
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
let uris = [];
|
|
|
|
let actions = [];
|
2017-06-08 07:11:41 +02:00
|
|
|
|
2017-10-06 23:46:41 +02:00
|
|
|
data.forEach(result => {
|
|
|
|
const uri = lbryuri.build({
|
|
|
|
name: result.name,
|
|
|
|
claimId: result.claimId,
|
|
|
|
});
|
|
|
|
actions.push(doResolveUri(uri));
|
|
|
|
uris.push(uri);
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-07 14:50:32 +02:00
|
|
|
|
2017-10-06 23:46:41 +02:00
|
|
|
actions.push({
|
|
|
|
type: types.SEARCH_COMPLETED,
|
|
|
|
data: {
|
|
|
|
query,
|
|
|
|
uris,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dispatch(batchActions(...actions));
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_CANCELLED,
|
|
|
|
});
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-23 10:46:52 +02:00
|
|
|
}
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-05-07 14:50:32 +02:00
|
|
|
}
|