chore: removed unused function on Search page; wip: searching logic

This commit is contained in:
Roberto Tonino 2020-11-23 21:37:18 +01:00
parent e84d5e7e9f
commit b5ff097286
5 changed files with 191 additions and 75 deletions

File diff suppressed because one or more lines are too long

View File

@ -14,7 +14,7 @@
:placeholder="$t('searchbar')" :placeholder="$t('searchbar')"
autofocus autofocus
ref="searchbar" ref="searchbar"
@keyup="handleSearchBarKeyup($event)" @keyup="performSearch($event)"
/> />
<!-- @keyup.enter.exact="onEnter" <!-- @keyup.enter.exact="onEnter"
@keyup.ctrl.enter="onCTRLEnter" --> @keyup.ctrl.enter="onCTRLEnter" -->
@ -98,14 +98,15 @@ input[type='search']::-webkit-search-cancel-button {
</style> </style>
<script> <script>
import { defineComponent, ref } from '@vue/composition-api'
import { isValidURL } from '@/utils/utils' import { isValidURL } from '@/utils/utils'
import { sendAddToQueue } from '@/utils/downloads' import { sendAddToQueue } from '@/utils/downloads'
import { socket } from '@/utils/socket' import { socket } from '@/utils/socket'
export default { export default defineComponent({
data() { setup() {
return { return {
lastTextSearch: '' lastTextSearch: ref('')
} }
}, },
created() { created() {
@ -132,52 +133,49 @@ export default {
}) })
}, },
methods: { methods: {
test() { async performSearch(keyEvent) {
console.log('test passato')
},
async handleSearchBarKeyup(keyEvent) {
let isEnterPressed = keyEvent.keyCode === 13 let isEnterPressed = keyEvent.keyCode === 13
// If not enter do nothing
if (!isEnterPressed) return if (!isEnterPressed) return
let term = this.$refs.searchbar.value let term = this.$refs.searchbar.value
let isEmptySearch = term === '' let isEmptySearch = term === ''
// If empty do nothing
if (isEmptySearch) return if (isEmptySearch) return
let isSearchingURL = isValidURL(term) let isSearchingURL = isValidURL(term)
let isCtrlPressed = keyEvent.ctrlKey let isCtrlPressed = keyEvent.ctrlKey
let isShowingAnalyzer = this.$route.name === 'Link Analyzer' let isShowingAnalyzer = this.$route.name === 'Link Analyzer'
let isShowingSearch = this.$route.name === 'Search' let isShowingSearch = this.$route.name === 'Search'
let sameAsLastSearch = term === this.lastTextSearch let isSameAsLastSearch = term === this.lastTextSearch
if (isSearchingURL) { if (isSearchingURL) {
if (isCtrlPressed) { if (isCtrlPressed) {
this.$root.$emit('QualityModal:open', term) this.$root.$emit('QualityModal:open', term)
} else {
if (isShowingAnalyzer) {
socket.emit('analyzeLink', term)
} else {
// ? Open downloads tab ?
sendAddToQueue(term)
}
}
} else {
if (isShowingSearch && sameAsLastSearch) {
// ? Has this any sense since we're not performing any call?
// this.$root.$emit('mainSearch:updateResults', term)
return return
} }
if (isShowingAnalyzer) {
socket.emit('analyzeLink', term)
return
}
// ? Open downloads tab maybe?
sendAddToQueue(term)
} else {
// The user is searching a normal string
if (isShowingSearch && isSameAsLastSearch) return
if (!isShowingSearch) { if (!isShowingSearch) {
await this.$router.push({ await this.$router.push({
name: 'Search' name: 'Search',
query: {
term
}
}) })
} }
if (!sameAsLastSearch) { if (!isSameAsLastSearch) {
this.$root.$emit('updateSearchLoadingState', true) this.$root.$emit('updateSearchLoadingState', true)
this.lastTextSearch = term this.lastTextSearch = term
} }
@ -186,7 +184,7 @@ export default {
} }
} }
} }
} })
</script> </script>

View File

@ -10,7 +10,7 @@
<BaseTab <BaseTab
v-for="tab in tabs" v-for="tab in tabs"
:key="tab.name" :key="tab.name"
@click="currentTab = tab" @click="changeSearchTab(tab.searchType)"
:class="{ active: currentTab.name === tab.name }" :class="{ active: currentTab.name === tab.name }"
> >
{{ tab.name }} {{ tab.name }}
@ -45,10 +45,12 @@ import { numberWithDots, convertDuration } from '@/utils/utils'
import { formatSingleTrack, formatAlbums, formatArtist, formatPlaylist } from '@/data/search' import { formatSingleTrack, formatAlbums, formatArtist, formatPlaylist } from '@/data/search'
import { standardizeData } from '@/data/standardize' import { standardizeData } from '@/data/standardize'
import { computed, defineComponent, reactive, ref, toRefs, watch, watchEffect } from '@vue/composition-api'
import { useMainSearch } from '@/use/main-search'
const resetObj = { data: [], next: 0, total: 0, hasLoaded: false } const resetObj = { data: [], next: 0, total: 0, hasLoaded: false }
export default { export default defineComponent({
components: { components: {
BaseLoadingPlaceholder, BaseLoadingPlaceholder,
BaseTabs, BaseTabs,
@ -60,11 +62,8 @@ export default {
required: false required: false
} }
}, },
data() { setup(props, ctx) {
const $t = this.$t.bind(this) const state = reactive({
const $tc = this.$tc.bind(this)
return {
currentTab: { currentTab: {
name: '', name: '',
searchType: '', searchType: '',
@ -72,6 +71,69 @@ export default {
viewInfo: '', viewInfo: '',
formatFunc: () => {} formatFunc: () => {}
}, },
results: {
query: '',
allTab: {
ORDER: [],
TOP_RESULT: [],
ALBUM: {
hasLoaded: false
},
ARTIST: {
hasLoaded: false
},
TRACK: {
hasLoaded: false
},
PLAYLIST: {
hasLoaded: false
}
},
trackTab: { ...resetObj },
albumTab: { ...resetObj },
artistTab: { ...resetObj },
playlistTab: { ...resetObj }
}
})
const { searchResult, performSearch } = useMainSearch()
watch(searchResult, newValue => {
console.log('show main search results watcher')
// Hide loading placeholder
ctx.root.$emit('updateSearchLoadingState', false)
state.results.query = searchResult.QUERY
state.results.allTab = searchResult
state.results.allTab.TRACK.hasLoaded = true
state.results.allTab.ALBUM.hasLoaded = true
state.results.allTab.ARTIST.hasLoaded = true
state.results.allTab.PLAYLIST.hasLoaded = true
state.results.trackTab = { ...resetObj }
state.results.albumTab = { ...resetObj }
state.results.artistTab = { ...resetObj }
state.results.playlistTab = { ...resetObj }
})
return {
...toRefs(state),
searchResult,
performSearch
}
},
data() {
const $t = this.$t.bind(this)
const $tc = this.$tc.bind(this)
return {
// currentTab: {
// name: '',
// searchType: '',
// component: {},
// viewInfo: '',
// formatFunc: () => {}
// },
tabs: [ tabs: [
{ {
name: $t('globals.listTabs.all'), name: $t('globals.listTabs.all'),
@ -107,30 +169,30 @@ export default {
viewInfo: 'playlistTab', viewInfo: 'playlistTab',
formatFunc: formatPlaylist formatFunc: formatPlaylist
} }
], ]
results: { // results: {
query: '', // query: '',
allTab: { // allTab: {
ORDER: [], // ORDER: [],
TOP_RESULT: [], // TOP_RESULT: [],
ALBUM: { // ALBUM: {
hasLoaded: false // hasLoaded: false
}, // },
ARTIST: { // ARTIST: {
hasLoaded: false // hasLoaded: false
}, // },
TRACK: { // TRACK: {
hasLoaded: false // hasLoaded: false
}, // },
PLAYLIST: { // PLAYLIST: {
hasLoaded: false // hasLoaded: false
} // }
}, // },
trackTab: { ...resetObj }, // trackTab: { ...resetObj },
albumTab: { ...resetObj }, // albumTab: { ...resetObj },
artistTab: { ...resetObj }, // artistTab: { ...resetObj },
playlistTab: { ...resetObj } // playlistTab: { ...resetObj }
} // }
} }
}, },
computed: { computed: {
@ -158,7 +220,6 @@ export default {
}, },
mounted() { mounted() {
this.$root.$on('mainSearch:showNewResults', this.checkIfPerformNewMainSearch) this.$root.$on('mainSearch:showNewResults', this.checkIfPerformNewMainSearch)
this.$root.$on('mainSearch:updateResults', this.checkIfUpdateResults)
socket.on('mainSearch', this.saveMainSearchResult) socket.on('mainSearch', this.saveMainSearchResult)
socket.on('search', this.handleSearch) socket.on('search', this.handleSearch)
@ -190,12 +251,17 @@ export default {
window.scrollTo(0, 0) window.scrollTo(0, 0)
this.currentTab = newTab this.currentTab = newTab
// this.lastTab = newTab
}, },
checkIfPerformNewMainSearch(searchTerm) { checkIfPerformNewMainSearch(searchTerm) {
let needToPerformNewMainSearch = searchTerm !== this.results.query const hasTermChanged = searchTerm !== this.results.query
if (needToPerformNewMainSearch) { if (hasTermChanged) {
this.performNewMainSearch(searchTerm) // this.performNewMainSearch(searchTerm)
this.$root.$emit('updateSearchLoadingState', true)
this.performSearch(searchTerm)
this.currentTab = this.tabs[0]
// this.currentTab = this.lastTab
} }
}, },
performNewMainSearch(term) { performNewMainSearch(term) {
@ -205,15 +271,6 @@ export default {
this.$root.$emit('updateSearchLoadingState', true) this.$root.$emit('updateSearchLoadingState', true)
this.currentTab = this.tabs[0] this.currentTab = this.tabs[0]
}, },
// ! Updates search only if the search term is the same as before AND we're not in the ALL tab. Wtf
checkIfUpdateResults(term) {
let needToUpdateSearch = term === this.results.query && this.currentTab.searchType !== 'all'
if (needToUpdateSearch) {
this.results[this.currentTab.searchType + 'Tab'] = { ...resetObj }
this.search(this.currentTab.searchType)
}
},
search(type) { search(type) {
socket.emit('search', { socket.emit('search', {
term: this.results.query, term: this.results.query,
@ -233,6 +290,8 @@ export default {
} }
}, },
saveMainSearchResult(searchResult) { saveMainSearchResult(searchResult) {
console.log('show main search results')
return
// Hide loading placeholder // Hide loading placeholder
this.$root.$emit('updateSearchLoadingState', false) this.$root.$emit('updateSearchLoadingState', false)
@ -287,8 +346,17 @@ export default {
this.search(newTab.searchType) this.search(newTab.searchType)
} }
},
beforeRouteEnter(to, from, next) {
next(vm => {
vm.checkIfPerformNewMainSearch(to.query.term)
})
},
beforeRouteUpdate(to, from, next) {
this.checkIfPerformNewMainSearch(to.query.term)
next()
} }
} })
</script> </script>
<style> <style>

View File

@ -104,7 +104,10 @@ const routes = [
{ {
path: '/search', path: '/search',
name: 'Search', name: 'Search',
component: Search component: Search,
meta: {
notKeepAlive: true
}
}, },
// 404 client side // 404 client side
{ {

47
src/use/main-search.js Normal file
View File

@ -0,0 +1,47 @@
import { ref, computed } from '@vue/composition-api'
import { socket } from '@/utils/socket'
const searchResult = ref({})
const lastTermSearched = ref(null)
function performSearch(searchTerm) {
if (searchTerm === lastTermSearched.value) return
// TODO Handle multiple, subsequent calls
// TODO Caching
socket.emit('mainSearch', { term: searchTerm })
socket.on('mainSearch', data => {
lastTermSearched.value = searchTerm
searchResult.value = data
socket.off('mainSearch')
})
}
export function useMainSearch() {
return {
searchResult,
performSearch
}
}
// socket.on('mainSearch', saveMainSearchResult)
// saveMainSearchResult(searchResult) {
// // Hide loading placeholder
// this.$root.$emit('updateSearchLoadingState', false)
// this.results.query = searchResult.QUERY
// this.results.allTab = searchResult
// this.results.allTab.TRACK.hasLoaded = true
// this.results.allTab.ALBUM.hasLoaded = true
// this.results.allTab.ARTIST.hasLoaded = true
// this.results.allTab.PLAYLIST.hasLoaded = true
// this.results.trackTab = { ...resetObj }
// this.results.albumTab = { ...resetObj }
// this.results.artistTab = { ...resetObj }
// this.results.playlistTab = { ...resetObj }
// },