2017-04-24 16:17:36 +02:00
|
|
|
import * as types from 'constants/action_types'
|
|
|
|
import lbry from 'lbry'
|
|
|
|
import lbryio from 'lbryio'
|
2017-04-25 07:47:21 +02:00
|
|
|
import lbryuri from 'lbryuri'
|
2017-04-24 16:17:36 +02:00
|
|
|
import lighthouse from 'lighthouse'
|
2017-04-25 07:47:21 +02:00
|
|
|
import {
|
|
|
|
doResolveUri,
|
|
|
|
} from 'actions/content'
|
2017-04-30 05:31:20 +02:00
|
|
|
import {
|
|
|
|
doNavigate,
|
|
|
|
} from 'actions/app'
|
|
|
|
import {
|
|
|
|
selectCurrentPage,
|
2017-05-07 14:50:32 +02:00
|
|
|
selectSearchQuery,
|
2017-04-30 05:31:20 +02:00
|
|
|
} from 'selectors/app'
|
2017-04-24 16:17:36 +02:00
|
|
|
|
|
|
|
export function doSearchContent(query) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
2017-04-30 05:31:20 +02:00
|
|
|
const page = selectCurrentPage(state)
|
|
|
|
|
2017-04-24 16:17:36 +02:00
|
|
|
|
2017-04-25 07:47:21 +02:00
|
|
|
if (!query) {
|
|
|
|
return dispatch({
|
|
|
|
type: types.SEARCH_CANCELLED,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-24 16:17:36 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_STARTED,
|
|
|
|
data: { query }
|
|
|
|
})
|
|
|
|
|
2017-05-07 14:50:32 +02:00
|
|
|
if(page != 'search' && query != undefined) {
|
|
|
|
dispatch(doNavigate('search', { query: query }))
|
|
|
|
}
|
2017-04-30 05:31:20 +02:00
|
|
|
|
2017-04-24 16:17:36 +02:00
|
|
|
lighthouse.search(query).then(results => {
|
2017-04-25 07:47:21 +02:00
|
|
|
results.forEach(result => {
|
|
|
|
const uri = lbryuri.build({
|
|
|
|
channelName: result.channel_name,
|
|
|
|
contentName: result.name,
|
|
|
|
claimId: result.channel_id || result.claim_id,
|
|
|
|
})
|
2017-04-29 11:50:29 +02:00
|
|
|
dispatch(doResolveUri(uri))
|
2017-04-25 07:47:21 +02:00
|
|
|
})
|
|
|
|
|
2017-04-24 16:17:36 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_COMPLETED,
|
|
|
|
data: {
|
|
|
|
query,
|
|
|
|
results,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 05:19:59 +02:00
|
|
|
|
|
|
|
export function doActivateSearch() {
|
2017-04-30 05:31:20 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
const page = selectCurrentPage(state)
|
|
|
|
const query = selectSearchQuery(state)
|
|
|
|
|
|
|
|
if(page != 'discover' && query != undefined) dispatch(doNavigate('discover'))
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.ACTIVATE_SEARCH,
|
|
|
|
})
|
2017-04-30 05:19:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doDeactivateSearch() {
|
|
|
|
return {
|
|
|
|
type: types.DEACTIVATE_SEARCH,
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 14:50:32 +02:00
|
|
|
|
|
|
|
export function doSetSearchQuery(query) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
dispatch(doNavigate('/search', { query }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doSearch() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
const page = selectCurrentPage(state)
|
|
|
|
const query = selectSearchQuery(state)
|
|
|
|
|
|
|
|
if (!query) {
|
|
|
|
return dispatch({
|
|
|
|
type: types.SEARCH_CANCELLED,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_STARTED,
|
|
|
|
data: { query }
|
|
|
|
})
|
|
|
|
|
|
|
|
lighthouse.search(query).then(results => {
|
|
|
|
results.forEach(result => {
|
|
|
|
const uri = lbryuri.build({
|
|
|
|
channelName: result.channel_name,
|
|
|
|
contentName: result.name,
|
|
|
|
claimId: result.channel_id || result.claim_id,
|
|
|
|
})
|
|
|
|
dispatch(doResolveUri(uri))
|
|
|
|
})
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.SEARCH_COMPLETED,
|
|
|
|
data: {
|
|
|
|
query,
|
|
|
|
results,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|