ui: Fix refresh and re-route behaviour (#7846)

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
Co-authored-by: dahn <daan.hoogland@gmail.com>
This commit is contained in:
Rohit Yadav 2023-08-25 17:15:35 +05:30 committed by GitHub
parent b19db10ea6
commit 6f7725ab35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -194,7 +194,11 @@ const sourceToken = {
},
cancel: () => {
if (!source) sourceToken.init()
source.cancel()
if (source) {
source.cancel()
} else {
console.log('Source token failed to be cancelled')
}
}
}

View File

@ -442,6 +442,7 @@ import { ref, reactive, toRaw } from 'vue'
import { api } from '@/api'
import { mixinDevice } from '@/utils/mixin.js'
import { genericCompare } from '@/utils/sort.js'
import { sourceToken } from '@/utils/request'
import store from '@/store'
import eventBus from '@/config/eventBus'
@ -621,6 +622,9 @@ export default {
next()
},
beforeRouteLeave (to, from, next) {
console.log('DEBUG - Due to route change, ignoring results for any on-going API request', this.apiName)
sourceToken.cancel()
sourceToken.init()
this.currentPath = this.$route.fullPath
next()
},
@ -920,19 +924,30 @@ export default {
break
}
}
this.itemCount = 0
var apiItemCount = 0
for (const key in json[responseName]) {
if (key === 'count') {
this.itemCount = json[responseName].count
apiItemCount = json[responseName].count
continue
}
objectName = key
break
}
if ('id' in this.$route.params && this.$route.params.id !== params.id) {
console.log('DEBUG - Discarding API response as its `id` does not match the uuid on the browser path')
return
}
if (this.dataView && apiItemCount > 1) {
console.log('DEBUG - Discarding API response as got more than one item in data view', this.$route.params, this.items)
return
}
this.items = json[responseName][objectName]
if (!this.items || this.items.length === 0) {
this.items = []
}
this.itemCount = apiItemCount
if (['listTemplates', 'listIsos'].includes(this.apiName) && this.items.length > 1) {
this.items = [...new Map(this.items.map(x => [x.id, x])).values()]
@ -982,6 +997,10 @@ export default {
}
}
}).catch(error => {
if (!error || !error.message) {
console.log('API request likely got cancelled due to route change:', this.apiName)
return
}
if ([401].includes(error.response.status)) {
return
}