Improve resources loading (#692)
* improve load forms * improve load submissions --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
parent
853760484c
commit
1f9a1f835f
|
|
@ -185,7 +185,6 @@ export default {
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
currentPage: 1,
|
|
||||||
fullyLoaded: false,
|
fullyLoaded: false,
|
||||||
showColumnsModal: false,
|
showColumnsModal: false,
|
||||||
properties: [],
|
properties: [],
|
||||||
|
|
@ -290,18 +289,34 @@ export default {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.recordStore.startLoading()
|
this.recordStore.startLoading()
|
||||||
opnFetch('/open/forms/' + this.form.id + '/submissions?page=' + this.currentPage).then((resData) => {
|
|
||||||
this.recordStore.save(resData.data.map((record) => record.data))
|
opnFetch('/open/forms/' + this.form.id + '/submissions?page=1').then((firstResponse) => {
|
||||||
this.dataChanged()
|
this.recordStore.save(firstResponse.data.map((record) => record.data))
|
||||||
if (this.currentPage < resData.meta.last_page) {
|
|
||||||
this.currentPage += 1
|
const lastPage = firstResponse.meta.last_page
|
||||||
this.getSubmissionsData()
|
|
||||||
} else {
|
if (lastPage > 1) {
|
||||||
this.recordStore.stopLoading()
|
// Create an array of promises for remaining pages
|
||||||
this.fullyLoaded = true
|
const remainingPages = Array.from({ length: lastPage - 1 }, (_, i) => {
|
||||||
|
const page = i + 2 // Start from page 2
|
||||||
|
return opnFetch('/open/forms/' + this.form.id + '/submissions?page=' + page)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Fetch all remaining pages in parallel
|
||||||
|
return Promise.all(remainingPages)
|
||||||
}
|
}
|
||||||
|
return []
|
||||||
|
}).then(responses => {
|
||||||
|
// Save all responses data
|
||||||
|
responses.forEach(response => {
|
||||||
|
this.recordStore.save(response.data.map((record) => record.data))
|
||||||
|
})
|
||||||
|
|
||||||
|
this.fullyLoaded = true
|
||||||
|
this.recordStore.stopLoading()
|
||||||
|
this.dataChanged()
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
this.recordStore.startLoading()
|
this.recordStore.stopLoading()
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
dataChanged() {
|
dataChanged() {
|
||||||
|
|
|
||||||
|
|
@ -7,35 +7,58 @@ export const singleFormEndpoint = "/open/forms/{slug}"
|
||||||
export const useFormsStore = defineStore("forms", () => {
|
export const useFormsStore = defineStore("forms", () => {
|
||||||
const contentStore = useContentStore("slug")
|
const contentStore = useContentStore("slug")
|
||||||
const allLoaded = ref(false)
|
const allLoaded = ref(false)
|
||||||
const currentPage = ref(1)
|
const loadAllRequest = ref(null)
|
||||||
|
|
||||||
const loadAll = (workspaceId) => {
|
const loadAll = (workspaceId) => {
|
||||||
contentStore.startLoading()
|
if (loadAllRequest.value) {
|
||||||
return opnFetch(formsEndpoint.replace("{workspaceId}", workspaceId), {
|
return loadAllRequest.value
|
||||||
query: { page: currentPage.value },
|
|
||||||
})
|
|
||||||
.then((response) => {
|
|
||||||
if (currentPage.value === 1) {
|
|
||||||
contentStore.resetState()
|
|
||||||
contentStore.save(response.data)
|
|
||||||
} else {
|
|
||||||
contentStore.save(response.data)
|
|
||||||
}
|
}
|
||||||
if (currentPage.value < response.meta.last_page) {
|
if (!workspaceId) {
|
||||||
currentPage.value++
|
return
|
||||||
loadAll(workspaceId)
|
}
|
||||||
} else {
|
contentStore.startLoading()
|
||||||
|
|
||||||
|
loadAllRequest.value = new Promise((resolve, reject) => {
|
||||||
|
opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page: 1 } })
|
||||||
|
.then(firstResponse => {
|
||||||
|
contentStore.resetState()
|
||||||
|
contentStore.save(firstResponse.data)
|
||||||
|
|
||||||
|
const lastPage = firstResponse.meta.last_page
|
||||||
|
|
||||||
|
if (lastPage > 1) {
|
||||||
|
// Create an array of promises for remaining pages
|
||||||
|
const remainingPages = Array.from({ length: lastPage - 1 }, (_, i) => {
|
||||||
|
const page = i + 2 // Start from page 2
|
||||||
|
return opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page } })
|
||||||
|
})
|
||||||
|
|
||||||
|
// Fetch all remaining pages in parallel
|
||||||
|
return Promise.all(remainingPages)
|
||||||
|
}
|
||||||
|
return []
|
||||||
|
})
|
||||||
|
.then(responses => {
|
||||||
|
// Save all responses data
|
||||||
|
responses.forEach(response => {
|
||||||
|
contentStore.save(response.data)
|
||||||
|
})
|
||||||
|
|
||||||
allLoaded.value = true
|
allLoaded.value = true
|
||||||
contentStore.stopLoading()
|
contentStore.stopLoading()
|
||||||
currentPage.value = 1
|
loadAllRequest.value = null
|
||||||
}
|
resolve()
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch(err => {
|
||||||
contentStore.stopLoading()
|
contentStore.stopLoading()
|
||||||
currentPage.value = 1
|
loadAllRequest.value = null
|
||||||
throw error
|
reject(err)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return loadAllRequest.value
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadForm = (slug) => {
|
const loadForm = (slug) => {
|
||||||
contentStore.startLoading()
|
contentStore.startLoading()
|
||||||
return opnFetch(singleFormEndpoint.replace("{slug}", slug))
|
return opnFetch(singleFormEndpoint.replace("{slug}", slug))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue