Improve resources loading (#692)

* improve load forms

* improve load submissions

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Chirag Chhatrala
2025-02-11 21:44:44 +05:30
committed by GitHub
parent 853760484c
commit 1f9a1f835f
2 changed files with 71 additions and 33 deletions

View File

@@ -7,35 +7,58 @@ export const singleFormEndpoint = "/open/forms/{slug}"
export const useFormsStore = defineStore("forms", () => {
const contentStore = useContentStore("slug")
const allLoaded = ref(false)
const currentPage = ref(1)
const loadAllRequest = ref(null)
const loadAll = (workspaceId) => {
if (loadAllRequest.value) {
return loadAllRequest.value
}
if (!workspaceId) {
return
}
contentStore.startLoading()
return opnFetch(formsEndpoint.replace("{workspaceId}", workspaceId), {
query: { page: currentPage.value },
})
.then((response) => {
if (currentPage.value === 1) {
loadAllRequest.value = new Promise((resolve, reject) => {
opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page: 1 } })
.then(firstResponse => {
contentStore.resetState()
contentStore.save(response.data)
} else {
contentStore.save(response.data)
}
if (currentPage.value < response.meta.last_page) {
currentPage.value++
loadAll(workspaceId)
} else {
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
contentStore.stopLoading()
currentPage.value = 1
}
})
.catch((error) => {
contentStore.stopLoading()
currentPage.value = 1
throw error
})
loadAllRequest.value = null
resolve()
})
.catch(err => {
contentStore.stopLoading()
loadAllRequest.value = null
reject(err)
})
})
return loadAllRequest.value
}
const loadForm = (slug) => {
contentStore.startLoading()
return opnFetch(singleFormEndpoint.replace("{slug}", slug))