Skip to content

Commit 6f7725a

Browse files
yadvrDaanHoogland
andauthored
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>
1 parent b19db10 commit 6f7725a

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
@@ -194,7 +194,11 @@ const sourceToken = {
194194
},
195195
cancel: () => {
196196
if (!source) sourceToken.init()
197-
source.cancel()
197+
if (source) {
198+
source.cancel()
199+
} else {
200+
console.log('Source token failed to be cancelled')
201+
}
198202
}
199203
}
200204

ui/src/views/AutogenView.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ import { ref, reactive, toRaw } from 'vue'
442442
import { api } from '@/api'
443443
import { mixinDevice } from '@/utils/mixin.js'
444444
import { genericCompare } from '@/utils/sort.js'
445+
import { sourceToken } from '@/utils/request'
445446
import store from '@/store'
446447
import eventBus from '@/config/eventBus'
447448
@@ -621,6 +622,9 @@ export default {
621622
next()
622623
},
623624
beforeRouteLeave (to, from, next) {
625+
console.log('DEBUG - Due to route change, ignoring results for any on-going API request', this.apiName)
626+
sourceToken.cancel()
627+
sourceToken.init()
624628
this.currentPath = this.$route.fullPath
625629
next()
626630
},
@@ -920,19 +924,30 @@ export default {
920924
break
921925
}
922926
}
923-
this.itemCount = 0
927+
var apiItemCount = 0
924928
for (const key in json[responseName]) {
925929
if (key === 'count') {
926-
this.itemCount = json[responseName].count
930+
apiItemCount = json[responseName].count
927931
continue
928932
}
929933
objectName = key
930934
break
931935
}
936+
937+
if ('id' in this.$route.params && this.$route.params.id !== params.id) {
938+
console.log('DEBUG - Discarding API response as its `id` does not match the uuid on the browser path')
939+
return
940+
}
941+
if (this.dataView && apiItemCount > 1) {
942+
console.log('DEBUG - Discarding API response as got more than one item in data view', this.$route.params, this.items)
943+
return
944+
}
945+
932946
this.items = json[responseName][objectName]
933947
if (!this.items || this.items.length === 0) {
934948
this.items = []
935949
}
950+
this.itemCount = apiItemCount
936951
937952
if (['listTemplates', 'listIsos'].includes(this.apiName) && this.items.length > 1) {
938953
this.items = [...new Map(this.items.map(x => [x.id, x])).values()]
@@ -982,6 +997,10 @@ export default {
982997
}
983998
}
984999
}).catch(error => {
1000+
if (!error || !error.message) {
1001+
console.log('API request likely got cancelled due to route change:', this.apiName)
1002+
return
1003+
}
9851004
if ([401].includes(error.response.status)) {
9861005
return
9871006
}

0 commit comments

Comments
 (0)