Skip to content

Commit 6626fa5

Browse files
committed
fix(vue2): router as param because not supported in vue 2
1 parent ad7177e commit 6626fa5

File tree

6 files changed

+57
-59
lines changed

6 files changed

+57
-59
lines changed

src/useDatatableUrlSync.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import cloneDeep from "lodash.clonedeep";
1414
import isEqual from "lodash.isequal";
1515

1616
import { ref, watch, nextTick, computed } from 'vue-demi'
17-
import { useRoute, useRouter } from 'vue-router'
1817
import {GenericDictionnary, VDUSConfiguration, VuetifyOptions} from "./utils/VDUSTypes"
1918

2019
/*
2120
DOC here on params and return value
2221
*/
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) {
2423

2524
// ----------------------------- DEFAULTING PARAMS ------------------------------
2625
configurations = {
@@ -42,9 +41,6 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
4241
}
4342

4443
// ----------------------------- DATA ------------------------------
45-
const route = useRoute();
46-
const router = useRouter()
47-
4844
let ignoredQueryParams: GenericDictionnary = {};
4945
let localQuery: GenericDictionnary = {};
5046
const loading = ref<boolean>(false);
@@ -57,8 +53,8 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
5753
// ----------------------------- COMPUTED ---------------------------
5854

5955
const vuetifyOptions = computed({
60-
get: ():VuetifyOptions => {
61-
let vuetifyOptions:VuetifyOptions = {
56+
get: (): VuetifyOptions => {
57+
const vuetifyOptions: VuetifyOptions = {
6258
page: 1,
6359
itemsPerPage: 10,
6460
sortBy: [],
@@ -69,27 +65,27 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
6965
mustSort: false
7066
};
7167

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);
7470

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
7975
: getDefaultValueForParam("ordering", formSchema);
8076

81-
let { sortBy, sortDesc } = getSortsArrayFromOrdering(ordering);
77+
const { sortBy, sortDesc } = getSortsArrayFromOrdering(ordering);
8278

8379
vuetifyOptions.sortBy = sortBy;
8480
vuetifyOptions.sortDesc = sortDesc;
8581

8682
return vuetifyOptions;
8783
},
88-
set: (newOptions:VuetifyOptions) => {
84+
set: (newOptions: VuetifyOptions) => {
8985
// 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)
9389
}
9490
})
9591

@@ -121,8 +117,8 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
121117
isFilter is true if it's a filter attribute taht changed. This allow us to reset the page number to 1
122118
This allow to fetch data in back end and apply the filter in the url query
123119
*/
124-
const triggerSearchIfNeeded = (isFilter:boolean, triggerFunction:Function) => {
125-
let newLocalQuery: GenericDictionnary = {
120+
const triggerSearchIfNeeded = (isFilter: boolean, triggerFunction: Function) => {
121+
const newLocalQuery: GenericDictionnary = {
126122
...generateQueryFromObject(form.value, formSchema, true),
127123
...generateQueryFromObject(options.value, formSchema, true)
128124
};
@@ -152,7 +148,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
152148

153149
// As we have a local copy of the query without the prefix to help readability and developper experience
154150
// We need to set back the prefix before pushing
155-
const newLocalQueryWithPrefix:GenericDictionnary = {};
151+
const newLocalQueryWithPrefix: GenericDictionnary = {};
156152
Object.entries(newLocalQuery).forEach(([key, value]) => {
157153
newLocalQueryWithPrefix[configurations?.prefix + key] = value;
158154
});
@@ -170,7 +166,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
170166
try {
171167
loading.value = true;
172168

173-
const queryAsObject:GenericDictionnary = {
169+
const queryAsObject: GenericDictionnary = {
174170
...generateQueryFromObject(form.value, formSchema, false),
175171
...generateQueryFromObject(options.value, formSchema, false),
176172
...configurations?.extraQueryParams || {}
@@ -197,10 +193,10 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
197193
Doc
198194
*/
199195
const getLocalCopyOfQueryUrl = () => {
200-
let newLocalQuery: GenericDictionnary = {};
196+
const newLocalQuery: GenericDictionnary = {};
201197
Object.keys(route.query).forEach(key => {
202198
// 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 || "", "");
204200

205201
// typeof undefined is important
206202
if (
@@ -256,7 +252,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
256252
ignoredQueryParams = {};
257253

258254
// localQuery is the memory of the query in url to know when reload data.
259-
let newLocalQuery = getLocalCopyOfQueryUrl();
255+
const newLocalQuery = getLocalCopyOfQueryUrl();
260256

261257
//To keep for debug please
262258
// console.log(
@@ -285,7 +281,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
285281

286282
localQuery = newLocalQuery;
287283

288-
let { newForm, newOptions } = readFormAndOptionsFromLocalQuery(
284+
const { newForm, newOptions } = readFormAndOptionsFromLocalQuery(
289285
localQuery,
290286
form.value,
291287
options.value,

src/utils/VDUSTypes.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
type GenericDictionnary = {
2-
[key: string]: any
2+
[key: string]: any;
33
}
44
type VDUSConfiguration = {
5-
prefix: string,
6-
debounceTime: number,
7-
serveurDefaultPageSize: number,
8-
extraQueryParams: GenericDictionnary
5+
prefix: string;
6+
debounceTime: number;
7+
serveurDefaultPageSize: number;
8+
extraQueryParams: GenericDictionnary;
99
}
1010

1111
type VuetifySortArraysObject = {
12-
sortBy: Array<string>,
13-
sortDesc: Array<boolean>
12+
sortBy: Array<string>;
13+
sortDesc: Array<boolean>;
1414
}
1515

1616
type VuetifyOptions = {
17-
page: number,
18-
itemsPerPage: number,
19-
sortBy: Array<string>,
20-
sortDesc: Array<boolean>,
21-
groupBy: Array<string>,
22-
groupDesc: Array<boolean>,
23-
multiSort: boolean,
24-
mustSort: boolean
17+
page: number;
18+
itemsPerPage: number;
19+
sortBy: Array<string>;
20+
sortDesc: Array<boolean>;
21+
groupBy: Array<string>;
22+
groupDesc: Array<boolean>;
23+
multiSort: boolean;
24+
mustSort: boolean;
2525
}
2626

2727
export {GenericDictionnary, VDUSConfiguration, VuetifySortArraysObject, VuetifyOptions}

src/utils/helpers.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import {VuetifySortArraysObject} from "./VDUSTypes";
22

3-
export const elementToArrayOfInt = (element: any):Array<number> => {
3+
export const elementToArrayOfInt = (element: any): Array<number> => {
44
return ["number", "string"].includes(typeof element)
55
? [parseInt(element)]
6-
: element.map((item:any) => parseInt(item));
6+
: element.map((item: any) => parseInt(item));
77
};
88

99
export const elementToArrayOfString = (element: any): Array<string> => {
1010
return element ? (typeof element === "string" ? [element] : element) : [];
1111
};
1212

13-
export const extractBooleanValue = (value: any, defaultValue:boolean = true):boolean => {
13+
export const extractBooleanValue = (value: any, defaultValue = true): boolean => {
1414
return value ? value.toString() === "true" : defaultValue;
1515
};
1616

17-
export const extractIntegerValue = (value:any, defaultValue:number = 0):number => {
17+
export const extractIntegerValue = (value: any, defaultValue = 0): number => {
1818
const parsed = parseInt(value);
1919
return isNaN(parsed) ? defaultValue : parsed;
2020
};
2121

22-
export const getSortsArrayFromOrdering = (ordering:Array<string>):VuetifySortArraysObject => {
22+
export const getSortsArrayFromOrdering = (ordering: Array<string>): VuetifySortArraysObject => {
2323
if (!ordering) {
2424
return { sortBy: [], sortDesc: [] };
2525
}
26-
let sortBy:Array<string> = [];
27-
let sortDesc:Array<boolean> = [];
26+
const sortBy: Array<string> = [];
27+
const sortDesc: Array<boolean> = [];
2828

2929
ordering.forEach(orderItem => {
30-
let isDesc:boolean = false;
30+
let isDesc = false;
3131
if (orderItem.startsWith("-")) {
3232
orderItem = orderItem.replace("-", "");
3333
isDesc = true;
@@ -39,10 +39,10 @@ export const getSortsArrayFromOrdering = (ordering:Array<string>):VuetifySortArr
3939
return { sortBy, sortDesc };
4040
}
4141

42-
export const getOrderingFromSortArray = (sortBy:Array<string>, sortDesc:Array<boolean>):Array<string> => {
43-
let ordering:Array<string> = [];
42+
export const getOrderingFromSortArray = (sortBy: Array<string>, sortDesc: Array<boolean>): Array<string> => {
43+
const ordering: Array<string> = [];
4444
sortBy.forEach((orderItem, index) => {
45-
let isDesc:boolean = true;
45+
let isDesc = true;
4646
if (sortDesc.length > index) {
4747
isDesc = sortDesc[index];
4848
}

vue2-example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"core-js": "^3.6.5",
1313
"vue": "^2.6.11",
1414
"vue-datatable-url-sync": "^1.0.3",
15-
"vue-router": "^3.2.0",
15+
"vue-router": "^3.5.1",
1616
"vuetify": "^2.4.0"
1717
},
1818
"devDependencies": {

vue2-example/src/components/HelloWorld.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
:items="fakeData"
1717
:options.sync="vuetifyOptions"
1818
class="elevation-1"
19-
:search="search"
19+
:search="form.search"
2020
></v-data-table>
2121
</v-col>
2222
</v-row>
@@ -26,6 +26,7 @@
2626
<script lang="ts">
2727
import Vue from 'vue';
2828
import useDatatableUrlSync from 'vue-datatable-url-sync';
29+
//import useDatatableUrlSync from './useDatatableUrlSync';
2930
import { GenericDictionnary } from 'vue-datatable-url-sync/src/utils/VDUSTypes';
3031
import fakeData from "./data";
3132
import { ref } from '@vue/composition-api'
@@ -39,7 +40,7 @@ export default Vue.extend({
3940
],
4041
fakeData
4142
}),
42-
setup () {
43+
setup (_, ctx) {
4344
// --------------------- DATA ------------------------------------
4445
const form = ref({
4546
search: ""
@@ -53,12 +54,13 @@ export default Vue.extend({
5354
5455
// --------------------- METHODS ------------------------------------
5556
const fetchDatas = (queryParams: string, queryAsObject: GenericDictionnary) => {
56-
console.log("icicicicic", queryParams, queryAsObject)
57-
// items.value = fakeData
57+
// Vuetify do all the front filtering for us so fetchDatas can be a dummy function.
58+
// If you are doing a backend filtering you have to adapt this function to fetch the data correctly filtered and paginated by your backend
59+
console.log(queryParams, queryAsObject)
5860
}
5961
6062
// --------------------- CREATED ------------------------------------
61-
const {vuetifyOptions} = useDatatableUrlSync(form, fetchDatas, options)
63+
const {vuetifyOptions} = useDatatableUrlSync(ctx.root.$route, ctx.root.$router, form, fetchDatas, options)
6264
6365
// --------------------- INSTANCE ------------------------------------
6466
return {

vue2-example/src/shims-tsx.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare global {
77
// tslint:disable no-empty-interface
88
interface ElementClass extends Vue {}
99
interface IntrinsicElements {
10-
[elem: string]: any
10+
[elem: string]: any;
1111
}
1212
}
1313
}

0 commit comments

Comments
 (0)