@m4l/core 0.1.23 → 0.1.24
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/contexts/DomainContext/index.js +51 -0
- package/contexts/DomainCountryContext/index.js +52 -0
- package/contexts/EnvironmentContext/index.js +15 -0
- package/contexts/FlagsContext/index.js +28 -0
- package/contexts/HostToolsContext/index.js +15 -0
- package/contexts/ModuleDictionaryContext/index.js +94 -0
- package/contexts/ModulePrivilegesContext/index.js +67 -0
- package/contexts/ModuleSkeletonContext/index.js +25 -0
- package/contexts/NetworkContext/index.js +23 -0
- package/hooks/useDomain/index.js +16 -0
- package/hooks/useEnvironment/index.js +9 -0
- package/hooks/useFlags/index.js +23 -0
- package/hooks/useHostTools/index.js +9 -0
- package/hooks/useLocalStorage/index.js +54 -0
- package/hooks/useModuleDictionary/index.js +9 -0
- package/hooks/useModulePrivileges/index.js +9 -0
- package/hooks/useModuleSkeleton/index.js +9 -0
- package/hooks/useNetwork/index.js +9 -0
- package/hooks/usePaginate/index.js +85 -0
- package/index.js +26 -4
- package/package.json +1 -1
- package/types/index.js +13 -0
- package/utils/axiosOperation.js +95 -0
- package/utils/index.js +35 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createContext, useState, useEffect } from "react";
|
|
2
|
+
import { u as useFlags } from "../../hooks/useFlags/index.js";
|
|
3
|
+
import { u as useEnvironment } from "../../hooks/useEnvironment/index.js";
|
|
4
|
+
import { u as useNetwork } from "../../hooks/useNetwork/index.js";
|
|
5
|
+
import { C as CommonFlags } from "../../types/index.js";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
const DomainContext = createContext(null);
|
|
8
|
+
function DomainProvider(props) {
|
|
9
|
+
const {
|
|
10
|
+
children
|
|
11
|
+
} = props;
|
|
12
|
+
const {
|
|
13
|
+
domain_token
|
|
14
|
+
} = useEnvironment();
|
|
15
|
+
const {
|
|
16
|
+
addFlag
|
|
17
|
+
} = useFlags();
|
|
18
|
+
const [domain, setDomain] = useState({
|
|
19
|
+
company_logo_small_url: "",
|
|
20
|
+
company_logo_normal_url: "",
|
|
21
|
+
name: ""
|
|
22
|
+
});
|
|
23
|
+
const {
|
|
24
|
+
networkOperation
|
|
25
|
+
} = useNetwork();
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
let mounted = true;
|
|
28
|
+
networkOperation({
|
|
29
|
+
method: "GET",
|
|
30
|
+
endPoint: `na/info/${domain_token}`
|
|
31
|
+
}).then((response) => {
|
|
32
|
+
if (mounted) {
|
|
33
|
+
setDomain({
|
|
34
|
+
...response.data
|
|
35
|
+
});
|
|
36
|
+
addFlag(CommonFlags.FLAG_DOMAIN_LOADED);
|
|
37
|
+
}
|
|
38
|
+
}).finally(() => {
|
|
39
|
+
});
|
|
40
|
+
return function cleanUp() {
|
|
41
|
+
mounted = false;
|
|
42
|
+
};
|
|
43
|
+
}, []);
|
|
44
|
+
return /* @__PURE__ */ jsx(DomainContext.Provider, {
|
|
45
|
+
value: {
|
|
46
|
+
...domain
|
|
47
|
+
},
|
|
48
|
+
children
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export { DomainContext as D, DomainProvider as a };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createContext, useState, useCallback, useEffect } from "react";
|
|
2
|
+
import "../EnvironmentContext/index.js";
|
|
3
|
+
import "../FlagsContext/index.js";
|
|
4
|
+
import { u as useHostTools } from "../../hooks/useHostTools/index.js";
|
|
5
|
+
import "../DomainContext/index.js";
|
|
6
|
+
import "../ModuleDictionaryContext/index.js";
|
|
7
|
+
import "../ModulePrivilegesContext/index.js";
|
|
8
|
+
import "../ModuleSkeletonContext/index.js";
|
|
9
|
+
import "../NetworkContext/index.js";
|
|
10
|
+
import { E as EmitEvents } from "../../types/index.js";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
const DomainCountryContext = createContext(null);
|
|
13
|
+
function DomainCountryProvider(props) {
|
|
14
|
+
const {
|
|
15
|
+
children,
|
|
16
|
+
isMicroFrontEnd,
|
|
17
|
+
domainCountryId
|
|
18
|
+
} = props;
|
|
19
|
+
const {
|
|
20
|
+
events_add_listener,
|
|
21
|
+
events_remove_listener,
|
|
22
|
+
events_emit
|
|
23
|
+
} = useHostTools();
|
|
24
|
+
const [finaDomainCountryId, setFinalDomainCountry] = useState(domainCountryId);
|
|
25
|
+
const onUpdateDomainCountry = useCallback((newDomainCountryId) => {
|
|
26
|
+
setFinalDomainCountry(newDomainCountryId);
|
|
27
|
+
}, []);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (isMicroFrontEnd) {
|
|
30
|
+
events_add_listener(EmitEvents.EMMIT_EVENT_HOST_DOMAIN_COUNTRY_CHANGE, onUpdateDomainCountry);
|
|
31
|
+
}
|
|
32
|
+
return () => {
|
|
33
|
+
if (isMicroFrontEnd) {
|
|
34
|
+
events_remove_listener(EmitEvents.EMMIT_EVENT_HOST_DOMAIN_COUNTRY_CHANGE, onUpdateDomainCountry);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
const changeDomainCountryId = useCallback((newDomainCountryId) => {
|
|
39
|
+
if (!isMicroFrontEnd) {
|
|
40
|
+
setFinalDomainCountry(newDomainCountryId);
|
|
41
|
+
events_emit(EmitEvents.EMMIT_EVENT_HOST_DOMAIN_COUNTRY_CHANGE, newDomainCountryId);
|
|
42
|
+
}
|
|
43
|
+
}, []);
|
|
44
|
+
return /* @__PURE__ */ jsx(DomainCountryContext.Provider, {
|
|
45
|
+
value: {
|
|
46
|
+
domainCountryId: finaDomainCountryId,
|
|
47
|
+
changeDomainCountryId
|
|
48
|
+
},
|
|
49
|
+
children
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export { DomainCountryContext as D, DomainCountryProvider as a };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
const EnvironmentContext = createContext(null);
|
|
4
|
+
function EnvironmentProvider(props) {
|
|
5
|
+
const {
|
|
6
|
+
children,
|
|
7
|
+
...other
|
|
8
|
+
} = props;
|
|
9
|
+
const [finalEnvironment] = useState(other);
|
|
10
|
+
return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
|
|
11
|
+
value: finalEnvironment,
|
|
12
|
+
children
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export { EnvironmentContext as E, EnvironmentProvider as a };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createContext, useState, useCallback } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
const FlagsContext = createContext(null);
|
|
4
|
+
function FlagsProvider({
|
|
5
|
+
children
|
|
6
|
+
}) {
|
|
7
|
+
const [flags, setFlags] = useState([]);
|
|
8
|
+
const clearFlags = useCallback(() => {
|
|
9
|
+
setFlags([]);
|
|
10
|
+
}, []);
|
|
11
|
+
const addFlag = useCallback((newFlag) => {
|
|
12
|
+
setFlags((oldFlags) => {
|
|
13
|
+
if (oldFlags.findIndex((f) => f === newFlag) < 0) {
|
|
14
|
+
return [...oldFlags, newFlag];
|
|
15
|
+
}
|
|
16
|
+
return [...oldFlags];
|
|
17
|
+
});
|
|
18
|
+
}, []);
|
|
19
|
+
return /* @__PURE__ */ jsx(FlagsContext.Provider, {
|
|
20
|
+
value: {
|
|
21
|
+
flags,
|
|
22
|
+
addFlag,
|
|
23
|
+
clearFlags
|
|
24
|
+
},
|
|
25
|
+
children
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export { FlagsContext as F, FlagsProvider as a };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
const HostToolsContext = createContext(null);
|
|
4
|
+
function HostToolsProvider(props) {
|
|
5
|
+
const {
|
|
6
|
+
children,
|
|
7
|
+
...hostTools
|
|
8
|
+
} = props;
|
|
9
|
+
const [finalTools] = useState(hostTools);
|
|
10
|
+
return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
|
|
11
|
+
value: finalTools,
|
|
12
|
+
children
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export { HostToolsContext as H, HostToolsProvider as a };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createContext, useState, useEffect, useCallback } from "react";
|
|
2
|
+
import { u as useEnvironment } from "../../hooks/useEnvironment/index.js";
|
|
3
|
+
import { u as useFlags } from "../../hooks/useFlags/index.js";
|
|
4
|
+
import { u as useHostTools } from "../../hooks/useHostTools/index.js";
|
|
5
|
+
import "../DomainContext/index.js";
|
|
6
|
+
import "../ModulePrivilegesContext/index.js";
|
|
7
|
+
import "../ModuleSkeletonContext/index.js";
|
|
8
|
+
import { u as useNetwork } from "../../hooks/useNetwork/index.js";
|
|
9
|
+
import "../DomainCountryContext/index.js";
|
|
10
|
+
import { C as CommonFlags } from "../../types/index.js";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
const ModuleDictionaryContext = createContext(null);
|
|
13
|
+
function ModuleDictionaryProvider(props) {
|
|
14
|
+
const {
|
|
15
|
+
children,
|
|
16
|
+
componentsDictionary,
|
|
17
|
+
moduleId,
|
|
18
|
+
moduleName = "module_name",
|
|
19
|
+
currentLang = "en",
|
|
20
|
+
isAuth = true
|
|
21
|
+
} = props;
|
|
22
|
+
const {
|
|
23
|
+
addFlag
|
|
24
|
+
} = useFlags();
|
|
25
|
+
const [moduleDictionary, setModuleDictionary] = useState(void 0);
|
|
26
|
+
const {
|
|
27
|
+
domain_token
|
|
28
|
+
} = useEnvironment();
|
|
29
|
+
const {
|
|
30
|
+
startProgress,
|
|
31
|
+
stopProgress
|
|
32
|
+
} = useHostTools();
|
|
33
|
+
const {
|
|
34
|
+
networkOperation
|
|
35
|
+
} = useNetwork();
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
let mounted = true;
|
|
38
|
+
startProgress();
|
|
39
|
+
networkOperation({
|
|
40
|
+
method: "GET",
|
|
41
|
+
endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
|
|
42
|
+
parms: {
|
|
43
|
+
comps: componentsDictionary,
|
|
44
|
+
...isAuth ? {} : {
|
|
45
|
+
domain_token,
|
|
46
|
+
lang: currentLang
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}).then((response) => {
|
|
50
|
+
if (mounted) {
|
|
51
|
+
setModuleDictionary({
|
|
52
|
+
...response
|
|
53
|
+
});
|
|
54
|
+
addFlag(CommonFlags.FLAG_DICTIONARY_LOADED);
|
|
55
|
+
}
|
|
56
|
+
}).finally(() => {
|
|
57
|
+
stopProgress();
|
|
58
|
+
});
|
|
59
|
+
return function cleanUp() {
|
|
60
|
+
mounted = false;
|
|
61
|
+
};
|
|
62
|
+
}, [currentLang]);
|
|
63
|
+
const getLabel = useCallback((key) => {
|
|
64
|
+
if (moduleDictionary === void 0)
|
|
65
|
+
return "";
|
|
66
|
+
if (key === "")
|
|
67
|
+
return "No key";
|
|
68
|
+
let ret = void 0;
|
|
69
|
+
const parts = key.split(".");
|
|
70
|
+
try {
|
|
71
|
+
if (parts.length === 1) {
|
|
72
|
+
ret = moduleDictionary.data[key];
|
|
73
|
+
}
|
|
74
|
+
if (parts.length === 2) {
|
|
75
|
+
ret = moduleDictionary[parts[0]][parts[1]];
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
}
|
|
79
|
+
if (ret) {
|
|
80
|
+
return ret;
|
|
81
|
+
}
|
|
82
|
+
return `N_D:[${key}]`;
|
|
83
|
+
}, [moduleDictionary]);
|
|
84
|
+
const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
|
|
85
|
+
return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
|
|
86
|
+
value: {
|
|
87
|
+
moduleDictionary,
|
|
88
|
+
getLabel,
|
|
89
|
+
getModuleLabel
|
|
90
|
+
},
|
|
91
|
+
children
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export { ModuleDictionaryContext as M, ModuleDictionaryProvider as a };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createContext, useState, useEffect, useCallback } from "react";
|
|
2
|
+
import "../EnvironmentContext/index.js";
|
|
3
|
+
import { u as useFlags } from "../../hooks/useFlags/index.js";
|
|
4
|
+
import { u as useHostTools } from "../../hooks/useHostTools/index.js";
|
|
5
|
+
import "../DomainContext/index.js";
|
|
6
|
+
import "../ModuleDictionaryContext/index.js";
|
|
7
|
+
import "../ModuleSkeletonContext/index.js";
|
|
8
|
+
import { u as useNetwork } from "../../hooks/useNetwork/index.js";
|
|
9
|
+
import "../DomainCountryContext/index.js";
|
|
10
|
+
import { C as CommonFlags } from "../../types/index.js";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
const ModulePrivilegesContext = createContext(null);
|
|
13
|
+
function ModulePrivilegesProvider(props) {
|
|
14
|
+
const {
|
|
15
|
+
children,
|
|
16
|
+
queryPrivileges
|
|
17
|
+
} = props;
|
|
18
|
+
const {
|
|
19
|
+
addFlag
|
|
20
|
+
} = useFlags();
|
|
21
|
+
const [privileges, setPrivileges] = useState({});
|
|
22
|
+
const {
|
|
23
|
+
startProgress,
|
|
24
|
+
stopProgress
|
|
25
|
+
} = useHostTools();
|
|
26
|
+
const {
|
|
27
|
+
networkOperation
|
|
28
|
+
} = useNetwork();
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
let mounted = true;
|
|
31
|
+
if (queryPrivileges.length === 0) {
|
|
32
|
+
addFlag(CommonFlags.FLAG_PRIVILEGES_LOADED);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
startProgress();
|
|
36
|
+
networkOperation({
|
|
37
|
+
method: "GET",
|
|
38
|
+
endPoint: `auth/login`,
|
|
39
|
+
parms: {
|
|
40
|
+
privileges: queryPrivileges
|
|
41
|
+
}
|
|
42
|
+
}).then((response) => {
|
|
43
|
+
if (mounted) {
|
|
44
|
+
setPrivileges({
|
|
45
|
+
...response.data
|
|
46
|
+
});
|
|
47
|
+
addFlag(CommonFlags.FLAG_PRIVILEGES_LOADED);
|
|
48
|
+
}
|
|
49
|
+
}).finally(() => {
|
|
50
|
+
stopProgress();
|
|
51
|
+
});
|
|
52
|
+
return function cleanUp() {
|
|
53
|
+
mounted = false;
|
|
54
|
+
};
|
|
55
|
+
}, []);
|
|
56
|
+
const hasPrivilege = useCallback((key) => {
|
|
57
|
+
return key in privileges;
|
|
58
|
+
}, [privileges]);
|
|
59
|
+
return /* @__PURE__ */ jsx(ModulePrivilegesContext.Provider, {
|
|
60
|
+
value: {
|
|
61
|
+
hasPrivilege,
|
|
62
|
+
privileges
|
|
63
|
+
},
|
|
64
|
+
children
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export { ModulePrivilegesContext as M, ModulePrivilegesProvider as a };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import "../EnvironmentContext/index.js";
|
|
3
|
+
import { a as useFlagsPresent } from "../../hooks/useFlags/index.js";
|
|
4
|
+
import "../HostToolsContext/index.js";
|
|
5
|
+
import "../DomainContext/index.js";
|
|
6
|
+
import "../ModuleDictionaryContext/index.js";
|
|
7
|
+
import "../ModulePrivilegesContext/index.js";
|
|
8
|
+
import "../NetworkContext/index.js";
|
|
9
|
+
import "../DomainCountryContext/index.js";
|
|
10
|
+
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
const ModuleSkeletonContext = createContext(null);
|
|
12
|
+
function ModuleSkeletonProvider(props) {
|
|
13
|
+
const {
|
|
14
|
+
children,
|
|
15
|
+
flags
|
|
16
|
+
} = props;
|
|
17
|
+
const isSkeleton = !useFlagsPresent(flags);
|
|
18
|
+
return /* @__PURE__ */ jsx(ModuleSkeletonContext.Provider, {
|
|
19
|
+
value: {
|
|
20
|
+
isSkeleton
|
|
21
|
+
},
|
|
22
|
+
children
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export { ModuleSkeletonContext as M, ModuleSkeletonProvider as a };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createContext, useCallback } from "react";
|
|
2
|
+
import { u as useEnvironment } from "../../hooks/useEnvironment/index.js";
|
|
3
|
+
import { u as useHostTools } from "../../hooks/useHostTools/index.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
const NetworkContext = createContext(null);
|
|
6
|
+
function NetworkProvider(props) {
|
|
7
|
+
const {
|
|
8
|
+
children,
|
|
9
|
+
axiosOperation
|
|
10
|
+
} = props;
|
|
11
|
+
const environment = useEnvironment();
|
|
12
|
+
const hostTools = useHostTools();
|
|
13
|
+
const networkOperation = useCallback(async (networkProps) => {
|
|
14
|
+
return axiosOperation(networkProps, environment, hostTools);
|
|
15
|
+
}, [axiosOperation]);
|
|
16
|
+
return /* @__PURE__ */ jsx(NetworkContext.Provider, {
|
|
17
|
+
value: {
|
|
18
|
+
networkOperation
|
|
19
|
+
},
|
|
20
|
+
children
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export { NetworkContext as N, NetworkProvider as a };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { D as DomainContext } from "../../contexts/DomainContext/index.js";
|
|
3
|
+
import { D as DomainCountryContext } from "../../contexts/DomainCountryContext/index.js";
|
|
4
|
+
const useDomain = () => {
|
|
5
|
+
const context = useContext(DomainContext);
|
|
6
|
+
if (!context)
|
|
7
|
+
throw new Error("useDomain context must be use inside DomainContext");
|
|
8
|
+
return context;
|
|
9
|
+
};
|
|
10
|
+
const useDomainCountry = () => {
|
|
11
|
+
const context = useContext(DomainCountryContext);
|
|
12
|
+
if (!context)
|
|
13
|
+
throw new Error("useDomainCountry context must be use inside DomainCountryContext");
|
|
14
|
+
return context;
|
|
15
|
+
};
|
|
16
|
+
export { useDomainCountry as a, useDomain as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { E as EnvironmentContext } from "../../contexts/EnvironmentContext/index.js";
|
|
3
|
+
const useEnvironment = () => {
|
|
4
|
+
const context = useContext(EnvironmentContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useEnvironment context must be use inside EnvironmentContext");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
export { useEnvironment as u };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useContext, useState, useEffect } from "react";
|
|
2
|
+
import { F as FlagsContext } from "../../contexts/FlagsContext/index.js";
|
|
3
|
+
const useFlags = () => {
|
|
4
|
+
const context = useContext(FlagsContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useFlags context must be use inside FlagsProvider");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
function isFlagsPresent(compareFlags, flags) {
|
|
10
|
+
const filterFlags = compareFlags.filter((findFlag) => flags.findIndex((sFlag) => sFlag === findFlag) !== -1);
|
|
11
|
+
return filterFlags.length === compareFlags.length;
|
|
12
|
+
}
|
|
13
|
+
const useFlagsPresent = (compareFlags) => {
|
|
14
|
+
const context = useFlags();
|
|
15
|
+
const [isPresent, setIsPresent] = useState(isFlagsPresent(compareFlags, context.flags));
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (isFlagsPresent(compareFlags, context.flags)) {
|
|
18
|
+
setIsPresent(true);
|
|
19
|
+
}
|
|
20
|
+
}, [context.flags, compareFlags]);
|
|
21
|
+
return isPresent;
|
|
22
|
+
};
|
|
23
|
+
export { useFlagsPresent as a, useFlags as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { H as HostToolsContext } from "../../contexts/HostToolsContext/index.js";
|
|
3
|
+
const useHostTools = () => {
|
|
4
|
+
const context = useContext(HostToolsContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useHostTools context must be use inside HostToolsContext");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
export { useHostTools as u };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
function useLocalStorage(key, initialValue) {
|
|
3
|
+
const [value, setValue] = useState(() => {
|
|
4
|
+
try {
|
|
5
|
+
const item = window.localStorage.getItem(key);
|
|
6
|
+
return item !== null ? JSON.parse(item) : initialValue;
|
|
7
|
+
} catch (e) {
|
|
8
|
+
return initialValue;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const setValueInLocalStorage = (newValue) => {
|
|
12
|
+
try {
|
|
13
|
+
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
14
|
+
setValue(newValue);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
return [value, setValueInLocalStorage];
|
|
19
|
+
}
|
|
20
|
+
function useLocalStorageWithListener(key, defaultValue) {
|
|
21
|
+
const [value, setValue] = useState(() => {
|
|
22
|
+
try {
|
|
23
|
+
const storedValue = localStorage.getItem(key);
|
|
24
|
+
return storedValue === null ? defaultValue : JSON.parse(storedValue);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return defaultValue;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const listener = (e) => {
|
|
31
|
+
if (e.storageArea === localStorage && e.key === key) {
|
|
32
|
+
try {
|
|
33
|
+
if (e.newValue) {
|
|
34
|
+
setValue(JSON.parse(e.newValue));
|
|
35
|
+
}
|
|
36
|
+
} catch (_e) {
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
window.addEventListener("storage", listener);
|
|
41
|
+
return () => {
|
|
42
|
+
window.removeEventListener("storage", listener);
|
|
43
|
+
};
|
|
44
|
+
}, [key, defaultValue]);
|
|
45
|
+
const setValueInLocalStorage = (newValue) => {
|
|
46
|
+
setValue((currentValue) => {
|
|
47
|
+
const result = typeof newValue === "function" ? newValue(currentValue) : newValue;
|
|
48
|
+
localStorage.setItem(key, JSON.stringify(result));
|
|
49
|
+
return result;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
return [value, setValueInLocalStorage];
|
|
53
|
+
}
|
|
54
|
+
export { useLocalStorageWithListener as a, useLocalStorage as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { M as ModuleDictionaryContext } from "../../contexts/ModuleDictionaryContext/index.js";
|
|
3
|
+
const useModuleDictionary = () => {
|
|
4
|
+
const context = useContext(ModuleDictionaryContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useModuleDictionary context must be use inside ModuleDictionaryProvider");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
export { useModuleDictionary as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { M as ModulePrivilegesContext } from "../../contexts/ModulePrivilegesContext/index.js";
|
|
3
|
+
const useModulePrivileges = () => {
|
|
4
|
+
const context = useContext(ModulePrivilegesContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useModulePrivileges context must be use inside ModulePrivilegesContext");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
export { useModulePrivileges as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { M as ModuleSkeletonContext } from "../../contexts/ModuleSkeletonContext/index.js";
|
|
3
|
+
const useModuleSkeleton = () => {
|
|
4
|
+
const context = useContext(ModuleSkeletonContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useModuleSkeleton context must be use inside ModuleSkeletonContext");
|
|
7
|
+
return context.isSkeleton;
|
|
8
|
+
};
|
|
9
|
+
export { useModuleSkeleton as u };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { N as NetworkContext } from "../../contexts/NetworkContext/index.js";
|
|
3
|
+
const useNetwork = () => {
|
|
4
|
+
const context = useContext(NetworkContext);
|
|
5
|
+
if (!context)
|
|
6
|
+
throw new Error("useNetwork context must be use inside NetworkContext");
|
|
7
|
+
return context;
|
|
8
|
+
};
|
|
9
|
+
export { useNetwork as u };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useState, useRef, useCallback, useEffect } from "react";
|
|
2
|
+
import { u as useHostTools } from "../useHostTools/index.js";
|
|
3
|
+
import { u as useNetwork } from "../useNetwork/index.js";
|
|
4
|
+
const initialPagerState = {
|
|
5
|
+
page: 0,
|
|
6
|
+
rowsPerPage: 25,
|
|
7
|
+
totalRecords: 0
|
|
8
|
+
};
|
|
9
|
+
const usePaginate = (props) => {
|
|
10
|
+
const {
|
|
11
|
+
endPoint,
|
|
12
|
+
timeout = 5e3,
|
|
13
|
+
queryParams,
|
|
14
|
+
fireOnChangeParms,
|
|
15
|
+
rowsPerPage = initialPagerState.rowsPerPage
|
|
16
|
+
} = props;
|
|
17
|
+
const [refresh, setRefresh] = useState(fireOnChangeParms ? 1 : 0);
|
|
18
|
+
const [rows, setRows] = useState([]);
|
|
19
|
+
const [pagerState, setPagerState] = useState({ ...initialPagerState, rowsPerPage });
|
|
20
|
+
const refPagerState = useRef({ ...initialPagerState, rowsPerPage });
|
|
21
|
+
const [firstQueryParams, setFirstQueryParams] = useState(true);
|
|
22
|
+
const { startProgress, stopProgress } = useHostTools();
|
|
23
|
+
const { networkOperation } = useNetwork();
|
|
24
|
+
const Refresh = useCallback(() => {
|
|
25
|
+
setRefresh((oldValue) => oldValue + 1);
|
|
26
|
+
}, []);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (firstQueryParams) {
|
|
29
|
+
setFirstQueryParams(false);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (fireOnChangeParms) {
|
|
33
|
+
Refresh();
|
|
34
|
+
}
|
|
35
|
+
}, [queryParams]);
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
let mounted = true;
|
|
38
|
+
if (refresh === 0)
|
|
39
|
+
return;
|
|
40
|
+
startProgress();
|
|
41
|
+
networkOperation({
|
|
42
|
+
method: "GET",
|
|
43
|
+
endPoint,
|
|
44
|
+
timeout,
|
|
45
|
+
parms: {
|
|
46
|
+
...queryParams,
|
|
47
|
+
page: refPagerState.current.page,
|
|
48
|
+
limit: refPagerState.current.rowsPerPage
|
|
49
|
+
}
|
|
50
|
+
}).then((response) => {
|
|
51
|
+
if (mounted) {
|
|
52
|
+
setRows(response.data);
|
|
53
|
+
refPagerState.current.page = response.pager.page;
|
|
54
|
+
setPagerState((oldPagerState) => ({
|
|
55
|
+
...oldPagerState,
|
|
56
|
+
page: response.pager.page,
|
|
57
|
+
totalRecords: response.pager.total
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}).finally(() => {
|
|
61
|
+
if (mounted) {
|
|
62
|
+
stopProgress();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return function cleanUp() {
|
|
66
|
+
stopProgress();
|
|
67
|
+
mounted = false;
|
|
68
|
+
};
|
|
69
|
+
}, [refresh]);
|
|
70
|
+
const onPageChange = (newPage) => {
|
|
71
|
+
refPagerState.current.page = newPage;
|
|
72
|
+
setRefresh((oldValue) => oldValue + 1);
|
|
73
|
+
};
|
|
74
|
+
const onRowsPerPageChange = (newRowsPerPage) => {
|
|
75
|
+
refPagerState.current.rowsPerPage = newRowsPerPage;
|
|
76
|
+
setPagerState((oldPagerState) => ({ ...oldPagerState, rowsPerPage: newRowsPerPage }));
|
|
77
|
+
setRefresh((oldValue) => oldValue + 1);
|
|
78
|
+
};
|
|
79
|
+
const clearRows = () => {
|
|
80
|
+
setRows([]);
|
|
81
|
+
setPagerState(initialPagerState);
|
|
82
|
+
};
|
|
83
|
+
return { onPageChange, onRowsPerPageChange, pagerState, rows, clearRows, Refresh };
|
|
84
|
+
};
|
|
85
|
+
export { initialPagerState as i, usePaginate as u };
|
package/index.js
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export
|
|
1
|
+
import "react";
|
|
2
|
+
export { E as EnvironmentContext, a as EnvironmentProvider } from "./contexts/EnvironmentContext/index.js";
|
|
3
|
+
export { F as FlagsContext, a as FlagsProvider } from "./contexts/FlagsContext/index.js";
|
|
4
|
+
export { H as HostToolsContext, a as HostToolsProvider } from "./contexts/HostToolsContext/index.js";
|
|
5
|
+
export { D as DomainContext, a as DomainProvider } from "./contexts/DomainContext/index.js";
|
|
6
|
+
export { M as ModuleDictionaryContext, a as ModuleDictionaryProvider } from "./contexts/ModuleDictionaryContext/index.js";
|
|
7
|
+
export { M as ModulePrivilegesContext, a as ModulePrivilegesProvider } from "./contexts/ModulePrivilegesContext/index.js";
|
|
8
|
+
export { M as ModuleSkeletonContext, a as ModuleSkeletonProvider } from "./contexts/ModuleSkeletonContext/index.js";
|
|
9
|
+
export { N as NetworkContext, a as NetworkProvider } from "./contexts/NetworkContext/index.js";
|
|
10
|
+
export { D as DomainCountryContext, a as DomainCountryProvider } from "./contexts/DomainCountryContext/index.js";
|
|
11
|
+
export { u as useEnvironment } from "./hooks/useEnvironment/index.js";
|
|
12
|
+
export { u as useFlags, a as useFlagsPresent } from "./hooks/useFlags/index.js";
|
|
13
|
+
export { u as useHostTools } from "./hooks/useHostTools/index.js";
|
|
14
|
+
export { u as useDomain, a as useDomainCountry } from "./hooks/useDomain/index.js";
|
|
15
|
+
export { u as useLocalStorage, a as useLocalStorageWithListener } from "./hooks/useLocalStorage/index.js";
|
|
16
|
+
export { u as useModuleDictionary } from "./hooks/useModuleDictionary/index.js";
|
|
17
|
+
export { u as useModulePrivileges } from "./hooks/useModulePrivileges/index.js";
|
|
18
|
+
export { u as useModuleSkeleton } from "./hooks/useModuleSkeleton/index.js";
|
|
19
|
+
export { u as useNetwork } from "./hooks/useNetwork/index.js";
|
|
20
|
+
export { i as initialPagerState, u as usePaginate } from "./hooks/usePaginate/index.js";
|
|
21
|
+
export { C as CommonFlags, E as EmitEvents } from "./types/index.js";
|
|
22
|
+
export { a as getLocalStorage, g as getPropertyByString, s as setLocalStorage, v as voidFunction } from "./utils/index.js";
|
|
23
|
+
export { a as axiosOperation } from "./utils/axiosOperation.js";
|
|
24
|
+
import "react/jsx-runtime";
|
|
25
|
+
import "axios";
|
|
26
|
+
import "qs";
|
package/package.json
CHANGED
package/types/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
|
|
2
|
+
EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
|
|
3
|
+
EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
|
|
4
|
+
EmitEvents2["EMMIT_EVENT_HOST_DOMAIN_COUNTRY_CHANGE"] = "domain_country_change";
|
|
5
|
+
return EmitEvents2;
|
|
6
|
+
})(EmitEvents || {});
|
|
7
|
+
var CommonFlags = /* @__PURE__ */ ((CommonFlags2) => {
|
|
8
|
+
CommonFlags2["FLAG_PRIVILEGES_LOADED"] = "privileges_loaded";
|
|
9
|
+
CommonFlags2["FLAG_DICTIONARY_LOADED"] = "dictionary_loaded";
|
|
10
|
+
CommonFlags2["FLAG_DOMAIN_LOADED"] = "domain_loaded";
|
|
11
|
+
return CommonFlags2;
|
|
12
|
+
})(CommonFlags || {});
|
|
13
|
+
export { CommonFlags as C, EmitEvents as E };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import qs from "qs";
|
|
3
|
+
import { E as EmitEvents } from "../types/index.js";
|
|
4
|
+
function getResponse(endPoint, response, hostTools) {
|
|
5
|
+
const { toast } = hostTools;
|
|
6
|
+
if (response && response.data && typeof response.data === "object") {
|
|
7
|
+
if (response.data.error && response.data.error?.code && response.data.error?.msg !== void 0) {
|
|
8
|
+
return Promise.reject({ ...response.data.error, status: response.status });
|
|
9
|
+
}
|
|
10
|
+
if (response.data.message) {
|
|
11
|
+
toast(response.data.message, { type: "success", autoClose: 1e4 });
|
|
12
|
+
}
|
|
13
|
+
return response.data;
|
|
14
|
+
}
|
|
15
|
+
return Promise.reject({
|
|
16
|
+
code: 1,
|
|
17
|
+
msg: `Incorrect endpoint: ${endPoint}`,
|
|
18
|
+
status: response.status
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function getError(error, hostTools, checkUnAuthorized = true) {
|
|
22
|
+
const { toast } = hostTools;
|
|
23
|
+
console.log("Ingres\xF3");
|
|
24
|
+
let err = {
|
|
25
|
+
message: "",
|
|
26
|
+
status: 1,
|
|
27
|
+
code: 0
|
|
28
|
+
};
|
|
29
|
+
if (error?.code !== void 0 && err.status !== void 0 && error.message !== void 0) {
|
|
30
|
+
err = { ...err, ...error };
|
|
31
|
+
}
|
|
32
|
+
if (error?.response) {
|
|
33
|
+
if (error.response.data && typeof error.response.data === "object" && error.response.data.error && error.response.data.error?.code && error.response.data.error?.message !== void 0) {
|
|
34
|
+
err = { ...error.response.data.error, status: error.response.status };
|
|
35
|
+
} else {
|
|
36
|
+
err.message = error.message;
|
|
37
|
+
err.status = error.response.status;
|
|
38
|
+
err.code = 0;
|
|
39
|
+
}
|
|
40
|
+
} else if (error?.request) {
|
|
41
|
+
err.message = `${error?.code} ${error.message}`;
|
|
42
|
+
err.code = -1;
|
|
43
|
+
} else {
|
|
44
|
+
err.message = `${error?.code} ${error.message}`;
|
|
45
|
+
err.status = 0;
|
|
46
|
+
err.code = -2;
|
|
47
|
+
}
|
|
48
|
+
if (checkUnAuthorized && error?.response?.status === 401) {
|
|
49
|
+
hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
|
|
50
|
+
}
|
|
51
|
+
if (error?.response?.data?.error?.message) {
|
|
52
|
+
err.message = error?.response?.data?.error.message;
|
|
53
|
+
}
|
|
54
|
+
if (checkUnAuthorized) {
|
|
55
|
+
toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: "error" });
|
|
56
|
+
}
|
|
57
|
+
return err;
|
|
58
|
+
}
|
|
59
|
+
const axiosOperation = async (props, enviroment, hostTools) => {
|
|
60
|
+
const {
|
|
61
|
+
method,
|
|
62
|
+
endPoint,
|
|
63
|
+
timeout = 5e3,
|
|
64
|
+
parms = {},
|
|
65
|
+
data = {},
|
|
66
|
+
isRemote = true,
|
|
67
|
+
checkUnAuthorized = true,
|
|
68
|
+
headers
|
|
69
|
+
} = props;
|
|
70
|
+
let baseURL;
|
|
71
|
+
if (isRemote) {
|
|
72
|
+
baseURL = enviroment.host_api_remote;
|
|
73
|
+
} else {
|
|
74
|
+
baseURL = enviroment.host_api_local;
|
|
75
|
+
}
|
|
76
|
+
hostTools.startProgress();
|
|
77
|
+
return axios({
|
|
78
|
+
baseURL,
|
|
79
|
+
withCredentials: isRemote,
|
|
80
|
+
method,
|
|
81
|
+
url: `/${endPoint}`,
|
|
82
|
+
data,
|
|
83
|
+
params: {},
|
|
84
|
+
paramsSerializer: () => {
|
|
85
|
+
return qs.stringify(parms, { encode: true });
|
|
86
|
+
},
|
|
87
|
+
headers,
|
|
88
|
+
timeout
|
|
89
|
+
}).then((response) => {
|
|
90
|
+
return getResponse(endPoint, response, hostTools);
|
|
91
|
+
}).catch((error) => Promise.reject(getError(error, hostTools, checkUnAuthorized))).finally(() => {
|
|
92
|
+
hostTools.stopProgress();
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
export { axiosOperation as a };
|
package/utils/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import "axios";
|
|
2
|
+
import "qs";
|
|
3
|
+
function voidFunction() {
|
|
4
|
+
}
|
|
5
|
+
function getPropertyByString(object, key) {
|
|
6
|
+
let value = object;
|
|
7
|
+
const props = key.split(".");
|
|
8
|
+
for (let index = 0; index < props.length; index += 1) {
|
|
9
|
+
if (value[props[index]] === void 0)
|
|
10
|
+
break;
|
|
11
|
+
value = value[props[index]];
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
function getLocalStorage(key, initialValue) {
|
|
16
|
+
try {
|
|
17
|
+
const item = window.localStorage.getItem(key);
|
|
18
|
+
return item !== null ? JSON.parse(item) : initialValue;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
return initialValue;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function setLocalStorage(key, newValue, mixCurrentLocalStorage = false) {
|
|
24
|
+
try {
|
|
25
|
+
let item = null;
|
|
26
|
+
if (mixCurrentLocalStorage) {
|
|
27
|
+
item = window.localStorage.getItem(key);
|
|
28
|
+
}
|
|
29
|
+
let value = item !== null ? JSON.parse(item) : {};
|
|
30
|
+
value = { ...value, ...newValue };
|
|
31
|
+
window.localStorage.setItem(key, JSON.stringify(value));
|
|
32
|
+
} catch (_e) {
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export { getLocalStorage as a, getPropertyByString as g, setLocalStorage as s, voidFunction as v };
|