@capsitech/react-utilities 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Components/SuspenseRoute.d.ts +7 -7
- package/lib/Components/SuspenseRoute.js +29 -29
- package/lib/Components/index.d.ts +1 -1
- package/lib/Components/index.js +1 -1
- package/lib/Hooks/index.d.ts +45 -45
- package/lib/Hooks/index.js +98 -98
- package/lib/Hooks/useInfiniteScroll.d.ts +7 -7
- package/lib/Hooks/useInfiniteScroll.js +22 -22
- package/lib/Hooks/useNetworkState.d.ts +67 -67
- package/lib/Hooks/useNetworkState.js +41 -41
- package/lib/Hooks/useShortcuts.d.ts +4 -4
- package/lib/Hooks/useShortcuts.js +91 -91
- package/lib/Utilities/ApiUtility.axios.d.ts +60 -60
- package/lib/Utilities/ApiUtility.axios.js +305 -305
- package/lib/Utilities/BrowserInfo.d.ts +74 -74
- package/lib/Utilities/BrowserInfo.js +153 -153
- package/lib/Utilities/Countries.d.ts +14 -14
- package/lib/Utilities/Countries.js +290 -290
- package/lib/Utilities/CustomEventEmitter.d.ts +12 -12
- package/lib/Utilities/CustomEventEmitter.js +30 -30
- package/lib/Utilities/FastCompare.d.ts +1 -1
- package/lib/Utilities/FastCompare.js +128 -128
- package/lib/Utilities/HideablePromise.d.ts +5 -5
- package/lib/Utilities/HideablePromise.js +10 -10
- package/lib/Utilities/LoadScripts.d.ts +9 -9
- package/lib/Utilities/LoadScripts.js +51 -51
- package/lib/Utilities/MTDFraudPrevention.d.ts +28 -28
- package/lib/Utilities/MTDFraudPrevention.js +157 -157
- package/lib/Utilities/Nationalities.d.ts +5 -5
- package/lib/Utilities/Nationalities.js +245 -245
- package/lib/Utilities/RouteUtils.d.ts +129 -120
- package/lib/Utilities/RouteUtils.js +223 -206
- package/lib/Utilities/TimeZones.d.ts +10 -10
- package/lib/Utilities/TimeZones.js +1069 -1069
- package/lib/Utilities/Types.d.ts +19 -19
- package/lib/Utilities/Types.js +1 -1
- package/lib/Utilities/Utils.d.ts +174 -174
- package/lib/Utilities/Utils.js +331 -331
- package/lib/Utilities/dayjs.d.ts +18 -18
- package/lib/Utilities/dayjs.js +56 -56
- package/lib/Utilities/index.d.ts +14 -14
- package/lib/Utilities/index.js +14 -14
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/package.json +12 -25
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
const blacklistedTargets = ['INPUT', 'TEXTAREA', 'SELECT'];
|
|
3
|
-
const keysReducer = (state, action) => {
|
|
4
|
-
switch (action.type) {
|
|
5
|
-
case 'set-key-down':
|
|
6
|
-
const keydownState = { ...state, [action.key]: true };
|
|
7
|
-
return keydownState;
|
|
8
|
-
case 'set-key-up':
|
|
9
|
-
const keyUpState = { ...state, [action.key]: false };
|
|
10
|
-
return keyUpState;
|
|
11
|
-
case 'reset-keys':
|
|
12
|
-
const resetState = { ...action.data };
|
|
13
|
-
return resetState;
|
|
14
|
-
default:
|
|
15
|
-
return state;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
export const useShortcuts = (shortcutKeys, callback, options) => {
|
|
19
|
-
if (!Array.isArray(shortcutKeys))
|
|
20
|
-
throw new Error('The first parameter to `useShortcuts` must be an ordered array of `KeyboardEvent.key` strings.');
|
|
21
|
-
if (!shortcutKeys.length)
|
|
22
|
-
throw new Error('The first parameter to `useShortcuts` must contain atleast one `KeyboardEvent.key` string.');
|
|
23
|
-
if (!callback || typeof callback !== 'function')
|
|
24
|
-
throw new Error('The second parameter to `useShortcuts` must be a function that will be envoked when the keys are pressed.');
|
|
25
|
-
const { overrideSystem } = options || {};
|
|
26
|
-
const initalKeyMapping = shortcutKeys.reduce((currentKeys, key) => {
|
|
27
|
-
currentKeys[key.toLowerCase()] = false;
|
|
28
|
-
return currentKeys;
|
|
29
|
-
}, {});
|
|
30
|
-
const [keys, setKeys] = React.useReducer(keysReducer, initalKeyMapping);
|
|
31
|
-
const keydownListener = React.useCallback((assignedKey) => (keydownEvent) => {
|
|
32
|
-
const loweredKey = assignedKey.toLowerCase();
|
|
33
|
-
if (keydownEvent.repeat)
|
|
34
|
-
return;
|
|
35
|
-
if (blacklistedTargets.includes(keydownEvent.target.tagName))
|
|
36
|
-
return;
|
|
37
|
-
if (loweredKey !== keydownEvent.key.toLowerCase())
|
|
38
|
-
return;
|
|
39
|
-
if (keys[loweredKey] === undefined)
|
|
40
|
-
return;
|
|
41
|
-
if (overrideSystem) {
|
|
42
|
-
keydownEvent.preventDefault();
|
|
43
|
-
disabledEventPropagation(keydownEvent);
|
|
44
|
-
}
|
|
45
|
-
setKeys({ type: 'set-key-down', key: loweredKey });
|
|
46
|
-
return false;
|
|
47
|
-
}, [keys, overrideSystem]);
|
|
48
|
-
const keyupListener = React.useCallback((assignedKey) => (keyupEvent) => {
|
|
49
|
-
const raisedKey = assignedKey.toLowerCase();
|
|
50
|
-
if (blacklistedTargets.includes(keyupEvent.target.tagName))
|
|
51
|
-
return;
|
|
52
|
-
if (keyupEvent.key.toLowerCase() !== raisedKey)
|
|
53
|
-
return;
|
|
54
|
-
if (keys[raisedKey] === undefined)
|
|
55
|
-
return;
|
|
56
|
-
if (overrideSystem) {
|
|
57
|
-
keyupEvent.preventDefault();
|
|
58
|
-
disabledEventPropagation(keyupEvent);
|
|
59
|
-
}
|
|
60
|
-
setKeys({ type: 'set-key-up', key: raisedKey });
|
|
61
|
-
return false;
|
|
62
|
-
}, [keys, overrideSystem]);
|
|
63
|
-
React.useEffect(() => {
|
|
64
|
-
//console.log('keys', keys);
|
|
65
|
-
if (!Object.values(keys).filter((value) => !value).length) {
|
|
66
|
-
callback(keys);
|
|
67
|
-
setKeys({ type: 'reset-keys', data: initalKeyMapping });
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
setKeys({ type: '' });
|
|
71
|
-
}
|
|
72
|
-
}, [callback, keys]);
|
|
73
|
-
React.useEffect(() => {
|
|
74
|
-
shortcutKeys.forEach((k) => window.addEventListener('keydown', keydownListener(k)));
|
|
75
|
-
return () => shortcutKeys.forEach((k) => window.removeEventListener('keydown', keydownListener(k)));
|
|
76
|
-
}, []);
|
|
77
|
-
React.useEffect(() => {
|
|
78
|
-
shortcutKeys.forEach((k) => window.addEventListener('keyup', keyupListener(k)));
|
|
79
|
-
return () => shortcutKeys.forEach((k) => window.removeEventListener('keyup', keyupListener(k)));
|
|
80
|
-
}, []);
|
|
81
|
-
};
|
|
82
|
-
export function disabledEventPropagation(e) {
|
|
83
|
-
if (e) {
|
|
84
|
-
if (e.stopPropagation) {
|
|
85
|
-
e.stopPropagation();
|
|
86
|
-
}
|
|
87
|
-
else if (window.event) {
|
|
88
|
-
window.event.cancelBubble = true;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
1
|
+
import React from 'react';
|
|
2
|
+
const blacklistedTargets = ['INPUT', 'TEXTAREA', 'SELECT'];
|
|
3
|
+
const keysReducer = (state, action) => {
|
|
4
|
+
switch (action.type) {
|
|
5
|
+
case 'set-key-down':
|
|
6
|
+
const keydownState = { ...state, [action.key]: true };
|
|
7
|
+
return keydownState;
|
|
8
|
+
case 'set-key-up':
|
|
9
|
+
const keyUpState = { ...state, [action.key]: false };
|
|
10
|
+
return keyUpState;
|
|
11
|
+
case 'reset-keys':
|
|
12
|
+
const resetState = { ...action.data };
|
|
13
|
+
return resetState;
|
|
14
|
+
default:
|
|
15
|
+
return state;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export const useShortcuts = (shortcutKeys, callback, options) => {
|
|
19
|
+
if (!Array.isArray(shortcutKeys))
|
|
20
|
+
throw new Error('The first parameter to `useShortcuts` must be an ordered array of `KeyboardEvent.key` strings.');
|
|
21
|
+
if (!shortcutKeys.length)
|
|
22
|
+
throw new Error('The first parameter to `useShortcuts` must contain atleast one `KeyboardEvent.key` string.');
|
|
23
|
+
if (!callback || typeof callback !== 'function')
|
|
24
|
+
throw new Error('The second parameter to `useShortcuts` must be a function that will be envoked when the keys are pressed.');
|
|
25
|
+
const { overrideSystem } = options || {};
|
|
26
|
+
const initalKeyMapping = shortcutKeys.reduce((currentKeys, key) => {
|
|
27
|
+
currentKeys[key.toLowerCase()] = false;
|
|
28
|
+
return currentKeys;
|
|
29
|
+
}, {});
|
|
30
|
+
const [keys, setKeys] = React.useReducer(keysReducer, initalKeyMapping);
|
|
31
|
+
const keydownListener = React.useCallback((assignedKey) => (keydownEvent) => {
|
|
32
|
+
const loweredKey = assignedKey.toLowerCase();
|
|
33
|
+
if (keydownEvent.repeat)
|
|
34
|
+
return;
|
|
35
|
+
if (blacklistedTargets.includes(keydownEvent.target.tagName))
|
|
36
|
+
return;
|
|
37
|
+
if (loweredKey !== keydownEvent.key.toLowerCase())
|
|
38
|
+
return;
|
|
39
|
+
if (keys[loweredKey] === undefined)
|
|
40
|
+
return;
|
|
41
|
+
if (overrideSystem) {
|
|
42
|
+
keydownEvent.preventDefault();
|
|
43
|
+
disabledEventPropagation(keydownEvent);
|
|
44
|
+
}
|
|
45
|
+
setKeys({ type: 'set-key-down', key: loweredKey });
|
|
46
|
+
return false;
|
|
47
|
+
}, [keys, overrideSystem]);
|
|
48
|
+
const keyupListener = React.useCallback((assignedKey) => (keyupEvent) => {
|
|
49
|
+
const raisedKey = assignedKey.toLowerCase();
|
|
50
|
+
if (blacklistedTargets.includes(keyupEvent.target.tagName))
|
|
51
|
+
return;
|
|
52
|
+
if (keyupEvent.key.toLowerCase() !== raisedKey)
|
|
53
|
+
return;
|
|
54
|
+
if (keys[raisedKey] === undefined)
|
|
55
|
+
return;
|
|
56
|
+
if (overrideSystem) {
|
|
57
|
+
keyupEvent.preventDefault();
|
|
58
|
+
disabledEventPropagation(keyupEvent);
|
|
59
|
+
}
|
|
60
|
+
setKeys({ type: 'set-key-up', key: raisedKey });
|
|
61
|
+
return false;
|
|
62
|
+
}, [keys, overrideSystem]);
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
//console.log('keys', keys);
|
|
65
|
+
if (!Object.values(keys).filter((value) => !value).length) {
|
|
66
|
+
callback(keys);
|
|
67
|
+
setKeys({ type: 'reset-keys', data: initalKeyMapping });
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
setKeys({ type: '' });
|
|
71
|
+
}
|
|
72
|
+
}, [callback, keys]);
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
shortcutKeys.forEach((k) => window.addEventListener('keydown', keydownListener(k)));
|
|
75
|
+
return () => shortcutKeys.forEach((k) => window.removeEventListener('keydown', keydownListener(k)));
|
|
76
|
+
}, []);
|
|
77
|
+
React.useEffect(() => {
|
|
78
|
+
shortcutKeys.forEach((k) => window.addEventListener('keyup', keyupListener(k)));
|
|
79
|
+
return () => shortcutKeys.forEach((k) => window.removeEventListener('keyup', keyupListener(k)));
|
|
80
|
+
}, []);
|
|
81
|
+
};
|
|
82
|
+
export function disabledEventPropagation(e) {
|
|
83
|
+
if (e) {
|
|
84
|
+
if (e.stopPropagation) {
|
|
85
|
+
e.stopPropagation();
|
|
86
|
+
}
|
|
87
|
+
else if (window.event) {
|
|
88
|
+
window.event.cancelBubble = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
-
export interface IApiResponse<T = any> {
|
|
3
|
-
status: boolean;
|
|
4
|
-
result: T;
|
|
5
|
-
message?: string;
|
|
6
|
-
errors?: {
|
|
7
|
-
number: number;
|
|
8
|
-
message: string;
|
|
9
|
-
suggestion: string;
|
|
10
|
-
exception: any;
|
|
11
|
-
}[];
|
|
12
|
-
}
|
|
13
|
-
export interface IApiListResult<T = any> {
|
|
14
|
-
totalRecords: number;
|
|
15
|
-
items: T[];
|
|
16
|
-
moreRecords?: boolean;
|
|
17
|
-
continuationToken?: string;
|
|
18
|
-
}
|
|
19
|
-
export interface IApiListResultWithTotal<TItem = any, TTotal = any> extends IApiListResult<TItem> {
|
|
20
|
-
total: TTotal;
|
|
21
|
-
}
|
|
22
|
-
export declare enum HttpMethods {
|
|
23
|
-
get = "get",
|
|
24
|
-
post = "post"
|
|
25
|
-
}
|
|
26
|
-
export declare enum FileDownloadStatus {
|
|
27
|
-
downloading = "downloading",
|
|
28
|
-
done = "done",
|
|
29
|
-
error = "error"
|
|
30
|
-
}
|
|
31
|
-
export type DownloadCallback = (status: FileDownloadStatus, error?: any) => void;
|
|
32
|
-
export interface IAxiosRequestConfigWithoutParams extends Omit<AxiosRequestConfig, 'params' | 'paramsSerializer'> {
|
|
33
|
-
contentType?: string;
|
|
34
|
-
}
|
|
35
|
-
declare class ApiUtilityBase {
|
|
36
|
-
accessToken?: string;
|
|
37
|
-
handleError: (error: string, errors?: any) => void;
|
|
38
|
-
getResponse: <T = any>(endpoint: string, params?: any, options?: IAxiosRequestConfigWithoutParams) => Promise<AxiosResponse<T, any>>;
|
|
39
|
-
get: <T = IApiResponse
|
|
40
|
-
getResult: <T = any>(endpoint: string, params?: any, throwErrorOn401?: boolean, options?: IAxiosRequestConfigWithoutParams) => Promise<T | null>;
|
|
41
|
-
post: <T = IApiResponse
|
|
42
|
-
put: <T = IApiResponse
|
|
43
|
-
appendFormDataValues: (form: FormData, values: any, preFix?: string) => void;
|
|
44
|
-
postForm: <T = IApiResponse
|
|
45
|
-
putForm: <T = IApiResponse
|
|
46
|
-
delete: <T = IApiResponse
|
|
47
|
-
private getFileName;
|
|
48
|
-
downloadFile: (endpoint: string, params?: any, method?: HttpMethods, options?: IAxiosRequestConfigWithoutParams) => Promise<{
|
|
49
|
-
status: FileDownloadStatus;
|
|
50
|
-
error?: any;
|
|
51
|
-
}>;
|
|
52
|
-
getAuthHeader: (contentType?: string) => any;
|
|
53
|
-
handleResponse: <T = IApiResponse
|
|
54
|
-
handleAxiosError: <T = IApiResponse
|
|
55
|
-
handleErrorResponse: (message: any, errors?: any) => void;
|
|
56
|
-
_axiosOptions: (options?: IAxiosRequestConfigWithoutParams) => AxiosRequestConfig;
|
|
57
|
-
getBaseUrl: () => string | undefined;
|
|
58
|
-
}
|
|
59
|
-
export declare const ApiUtility: ApiUtilityBase;
|
|
60
|
-
export {};
|
|
1
|
+
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
export interface IApiResponse<T = any> {
|
|
3
|
+
status: boolean;
|
|
4
|
+
result: T;
|
|
5
|
+
message?: string;
|
|
6
|
+
errors?: {
|
|
7
|
+
number: number;
|
|
8
|
+
message: string;
|
|
9
|
+
suggestion: string;
|
|
10
|
+
exception: any;
|
|
11
|
+
}[];
|
|
12
|
+
}
|
|
13
|
+
export interface IApiListResult<T = any> {
|
|
14
|
+
totalRecords: number;
|
|
15
|
+
items: T[];
|
|
16
|
+
moreRecords?: boolean;
|
|
17
|
+
continuationToken?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface IApiListResultWithTotal<TItem = any, TTotal = any> extends IApiListResult<TItem> {
|
|
20
|
+
total: TTotal;
|
|
21
|
+
}
|
|
22
|
+
export declare enum HttpMethods {
|
|
23
|
+
get = "get",
|
|
24
|
+
post = "post"
|
|
25
|
+
}
|
|
26
|
+
export declare enum FileDownloadStatus {
|
|
27
|
+
downloading = "downloading",
|
|
28
|
+
done = "done",
|
|
29
|
+
error = "error"
|
|
30
|
+
}
|
|
31
|
+
export type DownloadCallback = (status: FileDownloadStatus, error?: any) => void;
|
|
32
|
+
export interface IAxiosRequestConfigWithoutParams extends Omit<AxiosRequestConfig, 'params' | 'paramsSerializer'> {
|
|
33
|
+
contentType?: string;
|
|
34
|
+
}
|
|
35
|
+
declare class ApiUtilityBase {
|
|
36
|
+
accessToken?: string;
|
|
37
|
+
handleError: (error: string, errors?: any) => void;
|
|
38
|
+
getResponse: <T = any>(endpoint: string, params?: any, options?: IAxiosRequestConfigWithoutParams) => Promise<AxiosResponse<T, any>>;
|
|
39
|
+
get: <T = IApiResponse>(endpoint: string, params?: any, throwErrorOn401?: boolean, options?: IAxiosRequestConfigWithoutParams) => Promise<T>;
|
|
40
|
+
getResult: <T = any>(endpoint: string, params?: any, throwErrorOn401?: boolean, options?: IAxiosRequestConfigWithoutParams) => Promise<T | null>;
|
|
41
|
+
post: <T = IApiResponse>(endpoint: string, body: any, contentType?: string, options?: IAxiosRequestConfigWithoutParams) => Promise<T>;
|
|
42
|
+
put: <T = IApiResponse>(endpoint: string, body: any, contentType?: string, options?: IAxiosRequestConfigWithoutParams) => Promise<T>;
|
|
43
|
+
appendFormDataValues: (form: FormData, values: any, preFix?: string) => void;
|
|
44
|
+
postForm: <T = IApiResponse>(endpoint: string, params: any, options?: IAxiosRequestConfigWithoutParams) => Promise<IApiResponse<any> | T>;
|
|
45
|
+
putForm: <T = IApiResponse>(endpoint: string, params: any, options?: IAxiosRequestConfigWithoutParams) => Promise<IApiResponse<any> | T>;
|
|
46
|
+
delete: <T = IApiResponse>(endpoint: string, contentType?: string, options?: IAxiosRequestConfigWithoutParams) => Promise<T>;
|
|
47
|
+
private getFileName;
|
|
48
|
+
downloadFile: (endpoint: string, params?: any, method?: HttpMethods, options?: IAxiosRequestConfigWithoutParams) => Promise<{
|
|
49
|
+
status: FileDownloadStatus;
|
|
50
|
+
error?: any;
|
|
51
|
+
}>;
|
|
52
|
+
getAuthHeader: (contentType?: string) => any;
|
|
53
|
+
handleResponse: <T = IApiResponse>(response: AxiosResponse<T>, throwErrorOn401?: boolean) => T;
|
|
54
|
+
handleAxiosError: <T = IApiResponse>(error: AxiosError, throwErrorOn401?: boolean) => T;
|
|
55
|
+
handleErrorResponse: (message: any, errors?: any) => void;
|
|
56
|
+
_axiosOptions: (options?: IAxiosRequestConfigWithoutParams) => AxiosRequestConfig;
|
|
57
|
+
getBaseUrl: () => string | undefined;
|
|
58
|
+
}
|
|
59
|
+
export declare const ApiUtility: ApiUtilityBase;
|
|
60
|
+
export {};
|