Skip to content

Commit 1f9a1f8

Browse files
Improve resources loading (#692)
* improve load forms * improve load submissions --------- Co-authored-by: Julien Nahum <julien@nahum.net>
1 parent 8537604 commit 1f9a1f8

File tree

2 files changed

+71
-33
lines changed

2 files changed

+71
-33
lines changed

client/components/open/forms/components/FormSubmissions.vue

+26-11
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ export default {
185185
186186
data() {
187187
return {
188-
currentPage: 1,
189188
fullyLoaded: false,
190189
showColumnsModal: false,
191190
properties: [],
@@ -290,18 +289,34 @@ export default {
290289
return
291290
}
292291
this.recordStore.startLoading()
293-
opnFetch('/open/forms/' + this.form.id + '/submissions?page=' + this.currentPage).then((resData) => {
294-
this.recordStore.save(resData.data.map((record) => record.data))
295-
this.dataChanged()
296-
if (this.currentPage < resData.meta.last_page) {
297-
this.currentPage += 1
298-
this.getSubmissionsData()
299-
} else {
300-
this.recordStore.stopLoading()
301-
this.fullyLoaded = true
292+
293+
opnFetch('/open/forms/' + this.form.id + '/submissions?page=1').then((firstResponse) => {
294+
this.recordStore.save(firstResponse.data.map((record) => record.data))
295+
296+
const lastPage = firstResponse.meta.last_page
297+
298+
if (lastPage > 1) {
299+
// Create an array of promises for remaining pages
300+
const remainingPages = Array.from({ length: lastPage - 1 }, (_, i) => {
301+
const page = i + 2 // Start from page 2
302+
return opnFetch('/open/forms/' + this.form.id + '/submissions?page=' + page)
303+
})
304+
305+
// Fetch all remaining pages in parallel
306+
return Promise.all(remainingPages)
302307
}
308+
return []
309+
}).then(responses => {
310+
// Save all responses data
311+
responses.forEach(response => {
312+
this.recordStore.save(response.data.map((record) => record.data))
313+
})
314+
315+
this.fullyLoaded = true
316+
this.recordStore.stopLoading()
317+
this.dataChanged()
303318
}).catch(() => {
304-
this.recordStore.startLoading()
319+
this.recordStore.stopLoading()
305320
})
306321
},
307322
dataChanged() {

client/stores/forms.js

+45-22
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,58 @@ export const singleFormEndpoint = "/open/forms/{slug}"
77
export const useFormsStore = defineStore("forms", () => {
88
const contentStore = useContentStore("slug")
99
const allLoaded = ref(false)
10-
const currentPage = ref(1)
10+
const loadAllRequest = ref(null)
1111

1212
const loadAll = (workspaceId) => {
13+
if (loadAllRequest.value) {
14+
return loadAllRequest.value
15+
}
16+
if (!workspaceId) {
17+
return
18+
}
1319
contentStore.startLoading()
14-
return opnFetch(formsEndpoint.replace("{workspaceId}", workspaceId), {
15-
query: { page: currentPage.value },
16-
})
17-
.then((response) => {
18-
if (currentPage.value === 1) {
20+
21+
loadAllRequest.value = new Promise((resolve, reject) => {
22+
opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page: 1 } })
23+
.then(firstResponse => {
1924
contentStore.resetState()
20-
contentStore.save(response.data)
21-
} else {
22-
contentStore.save(response.data)
23-
}
24-
if (currentPage.value < response.meta.last_page) {
25-
currentPage.value++
26-
loadAll(workspaceId)
27-
} else {
25+
contentStore.save(firstResponse.data)
26+
27+
const lastPage = firstResponse.meta.last_page
28+
29+
if (lastPage > 1) {
30+
// Create an array of promises for remaining pages
31+
const remainingPages = Array.from({ length: lastPage - 1 }, (_, i) => {
32+
const page = i + 2 // Start from page 2
33+
return opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page } })
34+
})
35+
36+
// Fetch all remaining pages in parallel
37+
return Promise.all(remainingPages)
38+
}
39+
return []
40+
})
41+
.then(responses => {
42+
// Save all responses data
43+
responses.forEach(response => {
44+
contentStore.save(response.data)
45+
})
46+
2847
allLoaded.value = true
2948
contentStore.stopLoading()
30-
currentPage.value = 1
31-
}
32-
})
33-
.catch((error) => {
34-
contentStore.stopLoading()
35-
currentPage.value = 1
36-
throw error
37-
})
49+
loadAllRequest.value = null
50+
resolve()
51+
})
52+
.catch(err => {
53+
contentStore.stopLoading()
54+
loadAllRequest.value = null
55+
reject(err)
56+
})
57+
})
58+
59+
return loadAllRequest.value
3860
}
61+
3962
const loadForm = (slug) => {
4063
contentStore.startLoading()
4164
return opnFetch(singleFormEndpoint.replace("{slug}", slug))

0 commit comments

Comments
 (0)