Skip to content

Commit

Permalink
Merge pull request #237 from alan-wu/permalink-fix
Browse files Browse the repository at this point in the history
Retry on permalink failure.
  • Loading branch information
alan-wu authored Dec 10, 2024
2 parents 8532d4c + 109025e commit 277dbab
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions pages/apps/maps/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ const restoreStateWithUUID = async (route, $axios, sparcApi) => {
let state = undefined
let successMessage = undefined
let failMessage = undefined
if (route.query.id) {
uuid = route.query.id
const maxRetry = 3
const getState = async (uuid) => {
await $axios.post(`${sparcApi}/map/getstate`, {
uuid: uuid,
})
Expand All @@ -254,7 +254,15 @@ const restoreStateWithUUID = async (route, $axios, sparcApi) => {
`Sorry! We can not retrieve the saved stated. Please check later or consider submitting a bug report.`
})
}
if (route.query.id) {
uuid = route.query.id
for (let attempt = 0; attempt < maxRetry && !successMessage; attempt++) {
await getState(uuid)
}
}
if (successMessage) {
failMessage = undefined
}
return [uuid, state, successMessage, failMessage]
}
Expand Down Expand Up @@ -413,20 +421,38 @@ export default {
updateUUID: function () {
let url = this.options.sparcApi + `map/getshareid`
let state = this._instance.getState()
fetch(url, {
let maxRetry = 3
const getShareLink = (attempt) => {
fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({ state: state }),
})
.then((response) => response.json())
.then((response) => {
if (response.ok) {
return response.json()
}
throw new Error('Unsuccessful attempt to get shareid')
})
.then((data) => {
this.uuid = data.uuid
this.$router.replace({ query: { id: data.uuid } }).then(() => {
this.shareLink = `${this.options.rootUrl}${this.$route.fullPath}`
})
})
.catch((error) => {
console.log(`Unable to create permalink: attempt ${attempt} of ${maxRetry}`)
if (maxRetry > attempt) {
getShareLink(attempt + 1)
} else {
this.shareLink = `We have encountered an error, please try again.`
failMessage("We are unable to create a permalink at this moment, please try again later.")
}
})
}
getShareLink(1)
},
facetsUpdated: function () {
if (this.facets.length > 0 && this._instance) this._instance.openSearch(this.facets, "")
Expand Down

0 comments on commit 277dbab

Please sign in to comment.