fix: prevented duplicate keys when searching by scroll

This commit is contained in:
Roberto Tonino 2020-11-28 18:30:35 +01:00
parent 6789ea8be4
commit d738494595
4 changed files with 29 additions and 37 deletions

File diff suppressed because one or more lines are too long

View File

@ -34,6 +34,7 @@
<script> <script>
import { computed, onMounted, reactive, ref, toRefs, watch, defineComponent } from '@vue/composition-api' import { computed, onMounted, reactive, ref, toRefs, watch, defineComponent } from '@vue/composition-api'
import { uniqWith } from 'lodash-es'
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'
@ -139,22 +140,19 @@ export default defineComponent({
}) })
const { searchResult, performMainSearch } = useMainSearch() const { searchResult, performMainSearch } = useMainSearch()
const { result, performSearch } = useSearch() const { result, performSearch } = useSearch()
const isQueryEmpty = computed(() => state.results.query === '')
const searchedTerm = computed(() => ctx.root.$route.query.term) const searchedTerm = computed(() => ctx.root.$route.query.term)
const isQueryEmpty = computed(() => state.results.query === '')
const isSearching = ref(false) const isSearching = ref(false)
const isMainSearchCached = computed(() => Object.keys(searchResult.value).length !== 0) const isMainSearchCached = computed(() => Object.keys(searchResult.value).length !== 0)
const isNewQuery = computed(() => searchResult.value.QUERY !== searchedTerm.value) const isNewSearch = computed(() => searchResult.value.QUERY !== searchedTerm.value)
console.log('onSetup', lastTab.value)
if (isMainSearchCached.value && !isNewQuery.value) { if (isMainSearchCached.value && !isNewSearch.value) {
console.log('main search cached!')
onMounted(() => { onMounted(() => {
handleMainSearch(searchResult.value) handleMainSearch(searchResult.value)
}) })
} }
if (searchedTerm.value && (!isMainSearchCached.value || isNewQuery.value)) { if (searchedTerm.value && (!isMainSearchCached.value || isNewSearch.value)) {
console.log('need to perform main search')
performMainSearch(searchedTerm.value) performMainSearch(searchedTerm.value)
isSearching.value = true isSearching.value = true
} }
@ -171,13 +169,7 @@ export default defineComponent({
state.results.allTab.ARTIST.hasLoaded = true state.results.allTab.ARTIST.hasLoaded = true
state.results.allTab.PLAYLIST.hasLoaded = true state.results.allTab.PLAYLIST.hasLoaded = true
// state.results.trackTab = { ...resetObj }
// state.results.albumTab = { ...resetObj }
// state.results.artistTab = { ...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({
@ -185,7 +177,6 @@ export default defineComponent({
type: state.currentTab.searchType type: state.currentTab.searchType
}) })
} 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')
} }
} }
@ -197,14 +188,15 @@ export default defineComponent({
watch(result, newValue => { watch(result, newValue => {
const { next: nextResult, total, type, data: newData } = newValue const { next: nextResult, total, type, data: newData } = newValue
const currentTabKey = type + 'Tab' const currentTabKey = `${type}Tab`
let next = total let next = total
// console.log({ currentTabKey, test: state.currentTab.searchType })
if (nextResult) { if (nextResult) {
next = parseInt(nextResult.match(/index=(\d*)/)[1]) next = parseInt(nextResult.match(/index=(\d*)/)[1])
} /* else { }
next = total // console.log({ next, total, type, newData })
} */
if (state.results[currentTabKey].total !== total) { if (state.results[currentTabKey].total !== total) {
state.results[currentTabKey].total = total state.results[currentTabKey].total = total
@ -212,7 +204,14 @@ export default defineComponent({
if (state.results[currentTabKey].next !== next) { if (state.results[currentTabKey].next !== next) {
state.results[currentTabKey].next = next state.results[currentTabKey].next = next
state.results[currentTabKey].data = state.results[currentTabKey].data.concat(newData)
// Preventing duplicate entries by filtering them by ID
const rawData = state.results[currentTabKey].data.concat(newData)
const filteredData = uniqWith(rawData, (obj1, obj2) => {
return obj1.id === obj2.id
})
state.results[currentTabKey].data = filteredData
} }
state.results[currentTabKey].hasLoaded = true state.results[currentTabKey].hasLoaded = true
@ -226,11 +225,7 @@ export default defineComponent({
isQueryEmpty, isQueryEmpty,
searchResult, searchResult,
performMainSearch, performMainSearch,
performSearch, performSearch
// Remove
isNewQuery,
searchedTerm,
isMainSearchCached
} }
}, },
computed: { computed: {
@ -304,7 +299,6 @@ export default defineComponent({
this.scrolledSearch(needToSearch) this.scrolledSearch(needToSearch)
}, },
currentTab(newTab, old) { currentTab(newTab, old) {
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({

View File

@ -4,7 +4,6 @@ import { socket } from '@/utils/socket'
const searchResult = ref({}) const searchResult = ref({})
function performMainSearch(searchTerm) { function performMainSearch(searchTerm) {
console.log('Perform main search')
socket.emit('mainSearch', { term: searchTerm }) socket.emit('mainSearch', { term: searchTerm })
socket.on('mainSearch', data => { socket.on('mainSearch', data => {

View File

@ -1,10 +1,9 @@
import { ref, computed } from '@vue/composition-api' import { ref } from '@vue/composition-api'
import { socket } from '@/utils/socket' import { socket } from '@/utils/socket'
const result = ref({}) const result = ref({})
function performSearch({ term, type, start = 0, nb = 30 }) { function performSearch({ term, type, start = 0, nb = 30 }) {
console.log('perform search!')
socket.emit('search', { socket.emit('search', {
term, term,
type, type,