@@ -14,13 +14,12 @@ import cloneDeep from "lodash.clonedeep";
14
14
import isEqual from "lodash.isequal" ;
15
15
16
16
import { ref , watch , nextTick , computed } from 'vue-demi'
17
- import { useRoute , useRouter } from 'vue-router'
18
17
import { GenericDictionnary , VDUSConfiguration , VuetifyOptions } from "./utils/VDUSTypes"
19
18
20
19
/*
21
20
DOC here on params and return value
22
21
*/
23
- export default function useDatatableUrlSync ( form : GenericDictionnary , fetchDatas : Function , options : GenericDictionnary , formSchema ?: GenericDictionnary , initializeForm ?: Function , configurations ?: VDUSConfiguration ) {
22
+ export default function useDatatableUrlSync ( route : any , router : any , form : GenericDictionnary , fetchDatas : Function , options : GenericDictionnary , formSchema ?: GenericDictionnary , initializeForm ?: Function , configurations ?: VDUSConfiguration ) {
24
23
25
24
// ----------------------------- DEFAULTING PARAMS ------------------------------
26
25
configurations = {
@@ -42,9 +41,6 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
42
41
}
43
42
44
43
// ----------------------------- DATA ------------------------------
45
- const route = useRoute ( ) ;
46
- const router = useRouter ( )
47
-
48
44
let ignoredQueryParams : GenericDictionnary = { } ;
49
45
let localQuery : GenericDictionnary = { } ;
50
46
const loading = ref < boolean > ( false ) ;
@@ -57,8 +53,8 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
57
53
// ----------------------------- COMPUTED ---------------------------
58
54
59
55
const vuetifyOptions = computed ( {
60
- get : ( ) :VuetifyOptions => {
61
- let vuetifyOptions :VuetifyOptions = {
56
+ get : ( ) : VuetifyOptions => {
57
+ const vuetifyOptions : VuetifyOptions = {
62
58
page : 1 ,
63
59
itemsPerPage : 10 ,
64
60
sortBy : [ ] ,
@@ -69,27 +65,27 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
69
65
mustSort : false
70
66
} ;
71
67
72
- vuetifyOptions . page = options . page ?? getDefaultValueForParam ( "page" , formSchema ) ;
73
- vuetifyOptions . itemsPerPage = options . page_size ?? getDefaultValueForParam ( "page_size" , formSchema ) ;
68
+ vuetifyOptions . page = options . value . page ?? getDefaultValueForParam ( "page" , formSchema ) ;
69
+ vuetifyOptions . itemsPerPage = options . value . page_size ?? getDefaultValueForParam ( "page_size" , formSchema ) ;
74
70
75
- let ordering :Array < string > =
76
- Array . isArray ( options . ordering ) &&
77
- options . ordering . length > 0
78
- ? options . ordering
71
+ const ordering : Array < string > =
72
+ Array . isArray ( options . value . ordering ) &&
73
+ options . value . ordering . length > 0
74
+ ? options . value . ordering
79
75
: getDefaultValueForParam ( "ordering" , formSchema ) ;
80
76
81
- let { sortBy, sortDesc } = getSortsArrayFromOrdering ( ordering ) ;
77
+ const { sortBy, sortDesc } = getSortsArrayFromOrdering ( ordering ) ;
82
78
83
79
vuetifyOptions . sortBy = sortBy ;
84
80
vuetifyOptions . sortDesc = sortDesc ;
85
81
86
82
return vuetifyOptions ;
87
83
} ,
88
- set : ( newOptions :VuetifyOptions ) => {
84
+ set : ( newOptions : VuetifyOptions ) => {
89
85
// As we do not define options by default, to avoid reload from other component we doesn't want, we need to set data because they are not reactive
90
- options . page = newOptions . page ;
91
- options . page_size = newOptions . itemsPerPage ;
92
- options . ordering = getOrderingFromSortArray ( newOptions . sortBy , newOptions . sortDesc )
86
+ options . value . page = newOptions . page ;
87
+ options . value . page_size = newOptions . itemsPerPage ;
88
+ options . value . ordering = getOrderingFromSortArray ( newOptions . sortBy , newOptions . sortDesc )
93
89
}
94
90
} )
95
91
@@ -121,8 +117,8 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
121
117
isFilter is true if it's a filter attribute taht changed. This allow us to reset the page number to 1
122
118
This allow to fetch data in back end and apply the filter in the url query
123
119
*/
124
- const triggerSearchIfNeeded = ( isFilter :boolean , triggerFunction :Function ) => {
125
- let newLocalQuery : GenericDictionnary = {
120
+ const triggerSearchIfNeeded = ( isFilter : boolean , triggerFunction : Function ) => {
121
+ const newLocalQuery : GenericDictionnary = {
126
122
...generateQueryFromObject ( form . value , formSchema , true ) ,
127
123
...generateQueryFromObject ( options . value , formSchema , true )
128
124
} ;
@@ -152,7 +148,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
152
148
153
149
// As we have a local copy of the query without the prefix to help readability and developper experience
154
150
// We need to set back the prefix before pushing
155
- const newLocalQueryWithPrefix :GenericDictionnary = { } ;
151
+ const newLocalQueryWithPrefix : GenericDictionnary = { } ;
156
152
Object . entries ( newLocalQuery ) . forEach ( ( [ key , value ] ) => {
157
153
newLocalQueryWithPrefix [ configurations ?. prefix + key ] = value ;
158
154
} ) ;
@@ -170,7 +166,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
170
166
try {
171
167
loading . value = true ;
172
168
173
- const queryAsObject :GenericDictionnary = {
169
+ const queryAsObject : GenericDictionnary = {
174
170
...generateQueryFromObject ( form . value , formSchema , false ) ,
175
171
...generateQueryFromObject ( options . value , formSchema , false ) ,
176
172
...configurations ?. extraQueryParams || { }
@@ -197,10 +193,10 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
197
193
Doc
198
194
*/
199
195
const getLocalCopyOfQueryUrl = ( ) => {
200
- let newLocalQuery : GenericDictionnary = { } ;
196
+ const newLocalQuery : GenericDictionnary = { } ;
201
197
Object . keys ( route . query ) . forEach ( key => {
202
198
// We remove the prefix for the local copy to be agnostic of it in the code
203
- let keyWithoutPrefix = key . replace ( configurations ?. prefix || "" , "" ) ;
199
+ const keyWithoutPrefix = key . replace ( configurations ?. prefix || "" , "" ) ;
204
200
205
201
// typeof undefined is important
206
202
if (
@@ -256,7 +252,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
256
252
ignoredQueryParams = { } ;
257
253
258
254
// localQuery is the memory of the query in url to know when reload data.
259
- let newLocalQuery = getLocalCopyOfQueryUrl ( ) ;
255
+ const newLocalQuery = getLocalCopyOfQueryUrl ( ) ;
260
256
261
257
//To keep for debug please
262
258
// console.log(
@@ -285,7 +281,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
285
281
286
282
localQuery = newLocalQuery ;
287
283
288
- let { newForm, newOptions } = readFormAndOptionsFromLocalQuery (
284
+ const { newForm, newOptions } = readFormAndOptionsFromLocalQuery (
289
285
localQuery ,
290
286
form . value ,
291
287
options . value ,
0 commit comments