@@ -7,35 +7,58 @@ export const singleFormEndpoint = "/open/forms/{slug}"
7
7
export const useFormsStore = defineStore ( "forms" , ( ) => {
8
8
const contentStore = useContentStore ( "slug" )
9
9
const allLoaded = ref ( false )
10
- const currentPage = ref ( 1 )
10
+ const loadAllRequest = ref ( null )
11
11
12
12
const loadAll = ( workspaceId ) => {
13
+ if ( loadAllRequest . value ) {
14
+ return loadAllRequest . value
15
+ }
16
+ if ( ! workspaceId ) {
17
+ return
18
+ }
13
19
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 => {
19
24
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
+
28
47
allLoaded . value = true
29
48
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
38
60
}
61
+
39
62
const loadForm = ( slug ) => {
40
63
contentStore . startLoading ( )
41
64
return opnFetch ( singleFormEndpoint . replace ( "{slug}" , slug ) )
0 commit comments