@@ -4,7 +4,7 @@ import type { Page, PageProps, Errors } from '@inertiajs/core';
4
4
import { FilterMatchMode } from '@primevue/core/api' ;
5
5
import debounce from 'lodash-es/debounce' ;
6
6
import { PageState , DataTablePageEvent } from 'primevue' ;
7
- import { PrimeVueDataFilters } from '@/types' ;
7
+ import type { PrimeVueDataFilters , InertiaRouterFetchCallbacks } from '@/types' ;
8
8
import qs from 'qs' ;
9
9
10
10
interface PaginatedFilteredSortedQueryParams {
@@ -58,7 +58,7 @@ export function usePaginatedData(
58
58
filterCallback ( ) ;
59
59
} , 300 ) ;
60
60
61
- function setUrlParams ( ) {
61
+ function setUrlParams ( ) : void {
62
62
const queryString = window . location . search ;
63
63
const params = qs . parse ( queryString , {
64
64
ignoreQueryPrefix : true ,
@@ -73,19 +73,15 @@ export function usePaginatedData(
73
73
urlParams . value = { ...params } ;
74
74
}
75
75
76
- function scrollToTop ( ) {
76
+ function scrollToTop ( ) : void {
77
77
window . scrollTo ( {
78
78
top : 0 ,
79
79
behavior : 'smooth' ,
80
80
} ) ;
81
81
}
82
82
83
- function fetchData ( options : {
84
- onSuccess ?: ( page : Page < PageProps > ) => void ,
85
- onError ?: ( errors : Errors ) => void ,
86
- onFinish ?: ( ) => void ,
87
- } = { } ) {
88
- const { onSuccess : successCallback , onError : errorCallback , onFinish : finishCallback } = options ;
83
+ function fetchData ( options : InertiaRouterFetchCallbacks = { } ) : Promise < Page < PageProps > > {
84
+ const { onSuccess, onError, onFinish } = options ;
89
85
90
86
return new Promise ( ( resolve , reject ) => {
91
87
processing . value = true ;
@@ -104,32 +100,23 @@ export function usePaginatedData(
104
100
replace : true ,
105
101
only : Array . isArray ( propDataToFetch ) ? propDataToFetch : [ propDataToFetch ] ,
106
102
onSuccess : ( page ) => {
107
- if ( typeof successCallback === 'function' ) {
108
- successCallback ( page ) ;
109
- }
110
-
103
+ onSuccess ?.( page ) ;
111
104
resolve ( page ) ;
112
105
} ,
113
106
onError : ( errors ) => {
114
- if ( typeof errorCallback === 'function' ) {
115
- errorCallback ( errors ) ;
116
- }
117
-
107
+ onError ?.( errors ) ;
118
108
reject ( errors ) ;
119
109
} ,
120
110
onFinish : ( ) => {
121
111
setUrlParams ( ) ;
122
112
processing . value = false ;
123
-
124
- if ( typeof finishCallback === 'function' ) {
125
- finishCallback ( ) ;
126
- }
113
+ onFinish ?.( ) ;
127
114
} ,
128
115
} ) ;
129
116
} ) ;
130
117
}
131
118
132
- function paginate ( event : PageState | DataTablePageEvent ) {
119
+ function paginate ( event : PageState | DataTablePageEvent ) : void {
133
120
if ( event . rows !== pagination . value . rows ) {
134
121
pagination . value . page = 1 ;
135
122
} else {
@@ -143,45 +130,56 @@ export function usePaginatedData(
143
130
} ) ;
144
131
}
145
132
146
- function filter ( ) {
133
+ function filter ( ) : void {
147
134
pagination . value . page = 1 ;
148
135
fetchData ( ) . then ( ( ) => {
149
136
scrollToTop ( ) ;
150
137
} ) ;
151
138
}
152
139
153
- function reset ( ) {
140
+ function reset ( options : InertiaRouterFetchCallbacks = { } ) : Promise < Page < PageProps > > {
154
141
const defaultFilters = structuredClone ( initialFilters ) ;
155
-
156
142
Object . keys ( defaultFilters ) . forEach ( ( key ) => {
157
143
filters . value [ key ] . value = defaultFilters [ key ] . value ;
158
- filters . value [ key ] . matchMode = defaultFilters [ key ] . matchMode ;
159
144
} ) ;
145
+ sorting . value = { field : '' , order : 1 } ;
146
+ pagination . value = { page : 1 , rows : initialRows } ;
160
147
161
- sorting . value = {
162
- field : '' ,
163
- order : 1 ,
164
- } ;
165
-
166
- pagination . value = {
167
- page : 1 ,
168
- rows : initialRows ,
169
- } ;
170
-
171
- fetchData ( ) ;
148
+ return fetchData ( options ) ;
172
149
}
173
150
174
- function hardReset ( ) {
175
- router . visit ( window . location . pathname , {
176
- method : 'get' ,
177
- preserveUrl : false ,
178
- showProgress : true ,
179
- replace : true ,
180
- only : Array . isArray ( propDataToFetch ) ? propDataToFetch : [ propDataToFetch ] ,
151
+ function hardReset (
152
+ options : {
153
+ onSuccess ?: ( page : Page < PageProps > ) => void ;
154
+ onError ?: ( errors : Errors ) => void ;
155
+ onFinish ?: ( ) => void ;
156
+ } = { }
157
+ ) : Promise < Page < PageProps > > {
158
+ const { onSuccess, onError, onFinish } = options ;
159
+
160
+ return new Promise ( ( resolve , reject ) => {
161
+ router . visit ( window . location . pathname , {
162
+ method : 'get' ,
163
+ preserveUrl : false ,
164
+ showProgress : true ,
165
+ replace : true ,
166
+ only : Array . isArray ( propDataToFetch ) ? propDataToFetch : [ propDataToFetch ] ,
167
+ onSuccess ( page ) {
168
+ onSuccess ?.( page ) ;
169
+ resolve ( page ) ;
170
+ } ,
171
+ onError ( errors ) {
172
+ onError ?.( errors ) ;
173
+ reject ( errors ) ;
174
+ } ,
175
+ onFinish ( ) {
176
+ onFinish ?.( ) ;
177
+ } ,
178
+ } ) ;
181
179
} ) ;
182
180
}
183
181
184
- function parseUrlFilterValues ( ) {
182
+ function parseUrlFilterValues ( ) : void {
185
183
Object . keys ( filters . value ) . forEach ( ( key ) => {
186
184
const filter = filters . value [ key ] ;
187
185
@@ -230,7 +228,7 @@ export function usePaginatedData(
230
228
} ) ;
231
229
}
232
230
233
- function parseUrlParams ( urlParamsObj : PaginatedFilteredSortedQueryParams ) {
231
+ function parseUrlParams ( urlParamsObj : PaginatedFilteredSortedQueryParams ) : void {
234
232
filters . value = {
235
233
...structuredClone ( initialFilters ) ,
236
234
...urlParamsObj ?. filters ,
0 commit comments