Skip to content

Commit 245d3b3

Browse files
committed
convert js to ts
1 parent 1463850 commit 245d3b3

File tree

5 files changed

+52
-58
lines changed

5 files changed

+52
-58
lines changed

src/lib-components/useDatatableUrlSync.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,14 @@ import {
99
import cloneDeep from "lodash.clonedeep";
1010
import isEqual from "lodash.isequal";
1111

12-
import { ref, onMounted, watch, toRef, reactive } from 'vue'
12+
import { ref, watch } from 'vue'
1313
import { useRoute, useRouter } from 'vue-router'
14-
15-
type GenericDictionnary = {
16-
[key: string]: any
17-
}
18-
type VDUSConfiguration = {
19-
prefix: string,
20-
debounceTime: number,
21-
serveurDefaultPageSize: number,
22-
extraQueryParams: GenericDictionnary
23-
}
14+
import {GenericDictionnary, VDUSConfiguration} from "./utils/VDUSTypes"
2415

2516
/*
2617
DOC here on params and return value
2718
*/
28-
export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas: Function, options: GenericDictionnary, formSchema?: GenericDictionnary, initializeForm?: Function, configurations?:VDUSConfiguration) {
19+
export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas: Function, options: GenericDictionnary, formSchema?: GenericDictionnary, initializeForm?: Function, configurations?:VDUSConfiguration) {
2920

3021
// Set configurations
3122
configurations = {
@@ -59,7 +50,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
5950
}, configurations?.debounceTime || 0);
6051

6152

62-
watch(form, (newForm) => {
53+
watch(form, () => {
6354
debounceSearch(true);
6455
}, { deep: true })
6556

@@ -229,7 +220,7 @@ export default function useDatatableUrlSync(form: GenericDictionnary, fetchDatas
229220
return;
230221
}
231222

232-
const removedParams = getRemovedKeyBetweenTwoObject(
223+
const removedParams: Array<string> = getRemovedKeyBetweenTwoObject(
233224
localQuery,
234225
newLocalQuery
235226
);

src/lib-components/utils/VDUSTypes.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type GenericDictionnary = {
2+
[key: string]: any
3+
}
4+
type VDUSConfiguration = {
5+
prefix: string,
6+
debounceTime: number,
7+
serveurDefaultPageSize: number,
8+
extraQueryParams: GenericDictionnary
9+
}
10+
11+
export {GenericDictionnary, VDUSConfiguration}

src/lib-components/utils/helpers.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/lib-components/utils/helpers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const elementToArrayOfInt = (element: any):Array<number> => {
2+
return ["number", "string"].includes(typeof element)
3+
? [parseInt(element)]
4+
: element.map((item:any) => parseInt(item));
5+
};
6+
7+
export const elementToArrayOfString = (element: any) => {
8+
return element ? (typeof element === "string" ? [element] : element) : [];
9+
};
10+
11+
export const extractBooleanValue = (value: any, defaultValue:Boolean = true) => {
12+
return value ? value.toString() === "true" : defaultValue;
13+
};
14+
15+
export const extractIntegerValue = (value:any, defaultValue:number = 0) => {
16+
const parsed = parseInt(value);
17+
return isNaN(parsed) ? defaultValue : parsed;
18+
};

src/lib-components/utils/listPaginatedTools.js renamed to src/lib-components/utils/listPaginatedTools.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import {
55
extractIntegerValue
66
} from "./helpers";
77
import isEqual from "lodash.isequal";
8+
import {GenericDictionnary} from "./VDUSTypes"
89

910
/*
1011
This function take a object in parameter that is often a form of filtering field
1112
all this field are filtered before being used to be transformed as a query url
1213
if localName is true it will no replace the param key with the real used for backend query
1314
if localName is false the name will be replaced by the correct one sended to backend
1415
*/
15-
function generateQueryFromObject(object, schema, localName = true) {
16-
let queryUrl = {};
16+
function generateQueryFromObject(object: GenericDictionnary, schema?: GenericDictionnary, localName: Boolean = true): GenericDictionnary {
17+
let queryUrl: GenericDictionnary = {};
1718
for (let [key, value] of Object.entries(object)) {
1819
// We do not want to send a default value
1920
if (isValueDefault(value, key, schema)) {
@@ -32,7 +33,7 @@ function generateQueryFromObject(object, schema, localName = true) {
3233
return queryUrl;
3334
}
3435

35-
function getDefaultValueForParam(param, schema) {
36+
function getDefaultValueForParam(param: string, schema?: GenericDictionnary): any {
3637
if (schema && schema[param]) {
3738
// if there is a defautl value we change the condition to is non equality
3839
if (schema[param].default) {
@@ -51,9 +52,9 @@ function getDefaultValueForParam(param, schema) {
5152
return null;
5253
}
5354

54-
function isValueDefault(value, param, schema) {
55+
function isValueDefault(value: any, param: string, schema?: GenericDictionnary): boolean {
5556
// Default is string
56-
let isValueDefault = value === "";
57+
let isValueDefault: boolean = value === "";
5758

5859
if (schema && schema[param]) {
5960
// if there is a defautl value we change the condition to is non equality
@@ -84,14 +85,14 @@ function isValueDefault(value, param, schema) {
8485
Transform query parameter from vue router to two javascript objects representing the filtering form and the options
8586
*/
8687
function readFormAndOptionsFromLocalQuery(
87-
query,
88-
form,
89-
options,
90-
schema,
91-
removedParam = []
92-
) {
93-
let newOptions = {};
94-
let newForm = {};
88+
query: GenericDictionnary,
89+
form: GenericDictionnary,
90+
options: GenericDictionnary,
91+
schema?: GenericDictionnary,
92+
removedParam: Array<string> = []
93+
): {newOptions: GenericDictionnary, newForm: GenericDictionnary} {
94+
let newOptions: GenericDictionnary = {};
95+
let newForm: GenericDictionnary = {};
9596
for (let param in query) {
9697
if (typeof form[param] !== "undefined") {
9798
newForm[param] = convertParamIfTypeInSchema(query, param, schema);
@@ -113,7 +114,7 @@ function readFormAndOptionsFromLocalQuery(
113114
return { newOptions, newForm };
114115
}
115116

116-
function convertParamIfTypeInSchema(query, param, schema, prefix = "") {
117+
function convertParamIfTypeInSchema(query: GenericDictionnary, param: string, schema?: GenericDictionnary, prefix: string = ""): any {
117118
if (!schema || !schema[param] || !schema[param].type) {
118119
return query[prefix + param];
119120
}
@@ -133,9 +134,9 @@ function convertParamIfTypeInSchema(query, param, schema, prefix = "") {
133134
return query[prefix + param];
134135
}
135136

136-
function getRemovedKeyBetweenTwoObject(originalObject, newObject) {
137-
const originalObjectKeys = Object.keys(originalObject);
138-
const newObjectKeys = Object.keys(newObject);
137+
function getRemovedKeyBetweenTwoObject(originalObject: GenericDictionnary, newObject: GenericDictionnary): Array<string> {
138+
const originalObjectKeys: Array<string> = Object.keys(originalObject);
139+
const newObjectKeys: Array<string> = Object.keys(newObject);
139140
return originalObjectKeys.filter(
140141
originalKey => !newObjectKeys.includes(originalKey)
141142
);

0 commit comments

Comments
 (0)