feat: main search caching
This commit is contained in:
parent
bc4926db5f
commit
bd753865a0
File diff suppressed because one or more lines are too long
@ -33,7 +33,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { computed, defineComponent, reactive, ref, toRefs, watch, watchEffect } from '@vue/composition-api'
|
import { computed, onMounted, reactive, ref, toRefs, watch, defineComponent } from '@vue/composition-api'
|
||||||
|
|
||||||
import BaseLoadingPlaceholder from '@components/globals/BaseLoadingPlaceholder.vue'
|
import BaseLoadingPlaceholder from '@components/globals/BaseLoadingPlaceholder.vue'
|
||||||
import ResultsAll from '@components/search/ResultsAll.vue'
|
import ResultsAll from '@components/search/ResultsAll.vue'
|
||||||
@ -142,29 +142,23 @@ export default defineComponent({
|
|||||||
const isQueryEmpty = computed(() => state.results.query === '')
|
const isQueryEmpty = computed(() => state.results.query === '')
|
||||||
const searchedTerm = computed(() => ctx.root.$route.query.term)
|
const searchedTerm = computed(() => ctx.root.$route.query.term)
|
||||||
const isSearching = ref(false)
|
const isSearching = ref(false)
|
||||||
console.log('onSetup')
|
const isMainSearchCached = computed(() => Object.keys(searchResult.value).length !== 0)
|
||||||
|
console.log('onSetup', lastTab.value)
|
||||||
|
|
||||||
/*
|
if (isMainSearchCached.value) {
|
||||||
Search cases:
|
console.log('main search cached!')
|
||||||
- same term
|
onMounted(() => {
|
||||||
- from search (component is already rendered) -> do nothing
|
handleMainSearch(searchResult.value)
|
||||||
- handled in TheSearchBar ✅
|
})
|
||||||
- from another tab (component is not rendered yet) -> don't perform new search, show previous results (stored in the ref searchResult) and all tab
|
}
|
||||||
- handling in beforeRouteEnter ✅, but need to know that we already have the values ❌
|
|
||||||
- different term
|
|
||||||
- from search (component is already rendered) -> show new results and keep the tab
|
|
||||||
- handling in beforeRouteUpdate ❌, but need to keep the tab, therefore perform a Search or a Main Search according to last tab ❌
|
|
||||||
- from another tab (component is not rendered yet) -> show new results and show all tab
|
|
||||||
- handling in beforeRouteEnter ✅, no need to know that we already have the values ✅
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (searchedTerm.value) {
|
if (searchedTerm.value && !isMainSearchCached.value) {
|
||||||
|
console.log('need to perform main search')
|
||||||
performMainSearch(searchedTerm.value)
|
performMainSearch(searchedTerm.value)
|
||||||
isSearching.value = true
|
isSearching.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main search watcher
|
function handleMainSearch(newValue) {
|
||||||
watch(searchResult, newValue => {
|
|
||||||
// Hide loading placeholder
|
// Hide loading placeholder
|
||||||
isSearching.value = false
|
isSearching.value = false
|
||||||
|
|
||||||
@ -182,17 +176,21 @@ export default defineComponent({
|
|||||||
// state.results.playlistTab = { ...resetObj }
|
// state.results.playlistTab = { ...resetObj }
|
||||||
|
|
||||||
if (lastTab.value && lastTab.value.searchType !== 'all') {
|
if (lastTab.value && lastTab.value.searchType !== 'all') {
|
||||||
|
console.log('in main search, set last tab')
|
||||||
state.currentTab = lastTab.value
|
state.currentTab = lastTab.value
|
||||||
|
|
||||||
performSearch({
|
performSearch({
|
||||||
term: newValue.QUERY,
|
term: newValue.QUERY,
|
||||||
type: state.currentTab.searchType
|
type: state.currentTab.searchType
|
||||||
// start: state.results[`${state.currentTab.searchType}Tab`].next
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
console.log('in main search, all tab')
|
||||||
state.currentTab = state.tabs.find(tab => tab.searchType === 'all')
|
state.currentTab = state.tabs.find(tab => tab.searchType === 'all')
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// Main search watcher
|
||||||
|
watch(searchResult, handleMainSearch)
|
||||||
|
|
||||||
// Search watcher
|
// Search watcher
|
||||||
watch(result, newValue => {
|
watch(result, newValue => {
|
||||||
@ -300,8 +298,8 @@ export default defineComponent({
|
|||||||
|
|
||||||
this.scrolledSearch(needToSearch)
|
this.scrolledSearch(needToSearch)
|
||||||
},
|
},
|
||||||
currentTab(newTab) {
|
currentTab(newTab, old) {
|
||||||
console.log('watch current tab')
|
console.log('watch current tab %s -> %s', old.searchType, newTab.searchType)
|
||||||
if (this.isTabLoaded(newTab)) return
|
if (this.isTabLoaded(newTab)) return
|
||||||
|
|
||||||
this.performSearch({
|
this.performSearch({
|
||||||
@ -311,23 +309,10 @@ export default defineComponent({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// beforeRouteUpdate(to, from, next) {
|
|
||||||
// console.log('beforeRouteUpdate', from)
|
|
||||||
|
|
||||||
// // this.performMainSearch(to.query.term)
|
|
||||||
|
|
||||||
// // if (this.currentTab.searchType !== 'all') {
|
|
||||||
// // this.performSearch({
|
|
||||||
// // term: to.query.term,
|
|
||||||
// // type: this.currentTab.searchType,
|
|
||||||
// // start: this.results[`${this.currentTab.searchType}Tab`].next
|
|
||||||
// // })
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// next()
|
|
||||||
// }
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user