Skip to content

Commit 9e00645

Browse files
ui: Fix refresh and re-route behaviour (apache#7846)
Signed-off-by: Rohit Yadav <[email protected]> Co-authored-by: dahn <[email protected]> (cherry picked from commit 6f7725a) Signed-off-by: Rohit Yadav <[email protected]>
1 parent a62de78 commit 9e00645

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

ui/src/utils/request.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ const sourceToken = {
176176
},
177177
cancel: () => {
178178
if (!source) sourceToken.init()
179-
source.cancel()
179+
if (source) {
180+
source.cancel()
181+
} else {
182+
console.log('Source token failed to be cancelled')
183+
}
180184
}
181185
}
182186

ui/src/views/AutogenView.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ import { ref, reactive, toRaw } from 'vue'
437437
import { api } from '@/api'
438438
import { mixinDevice } from '@/utils/mixin.js'
439439
import { genericCompare } from '@/utils/sort.js'
440+
import { sourceToken } from '@/utils/request'
440441
import store from '@/store'
441442
import eventBus from '@/config/eventBus'
442443
@@ -616,6 +617,9 @@ export default {
616617
next()
617618
},
618619
beforeRouteLeave (to, from, next) {
620+
console.log('DEBUG - Due to route change, ignoring results for any on-going API request', this.apiName)
621+
sourceToken.cancel()
622+
sourceToken.init()
619623
this.currentPath = this.$route.fullPath
620624
next()
621625
},
@@ -885,19 +889,30 @@ export default {
885889
break
886890
}
887891
}
888-
this.itemCount = 0
892+
var apiItemCount = 0
889893
for (const key in json[responseName]) {
890894
if (key === 'count') {
891-
this.itemCount = json[responseName].count
895+
apiItemCount = json[responseName].count
892896
continue
893897
}
894898
objectName = key
895899
break
896900
}
901+
902+
if ('id' in this.$route.params && this.$route.params.id !== params.id) {
903+
console.log('DEBUG - Discarding API response as its `id` does not match the uuid on the browser path')
904+
return
905+
}
906+
if (this.dataView && apiItemCount > 1) {
907+
console.log('DEBUG - Discarding API response as got more than one item in data view', this.$route.params, this.items)
908+
return
909+
}
910+
897911
this.items = json[responseName][objectName]
898912
if (!this.items || this.items.length === 0) {
899913
this.items = []
900914
}
915+
this.itemCount = apiItemCount
901916
902917
if (['listTemplates', 'listIsos'].includes(this.apiName) && this.items.length > 1) {
903918
this.items = [...new Map(this.items.map(x => [x.id, x])).values()]
@@ -946,6 +961,10 @@ export default {
946961
}
947962
}
948963
}).catch(error => {
964+
if (!error || !error.message) {
965+
console.log('API request likely got cancelled due to route change:', this.apiName)
966+
return
967+
}
949968
if ([401].includes(error.response.status)) {
950969
return
951970
}

0 commit comments

Comments
 (0)