@m4l/core 0.0.3 → 0.0.6
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/dist/contexts/index.d.ts +5 -5
- package/dist/hooks/index.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +167 -3
- package/package.json +1 -1
package/dist/contexts/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { EnvironmentContext } from './EnvironmentContext';
|
|
2
|
-
export { HostToolsContext } from './HostToolsContext';
|
|
3
|
-
export { NetworkContext } from './NetworkContext';
|
|
4
|
-
export { FlagsContext } from './FlagsContext';
|
|
5
|
-
export { ModuleDictionaryContext } from './ModuleDictionaryContext';
|
|
1
|
+
export { EnvironmentContext, EnvironmentProvider } from './EnvironmentContext';
|
|
2
|
+
export { HostToolsContext, HostToolsProvider } from './HostToolsContext';
|
|
3
|
+
export { NetworkContext, NetworkProvider } from './NetworkContext';
|
|
4
|
+
export { FlagsContext, FlagsProvider } from './FlagsContext';
|
|
5
|
+
export { ModuleDictionaryContext, ModuleDictionaryProvider } from './ModuleDictionaryContext';
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ export { useLocalStorage } from './useLocalStorage/useLocalStorage';
|
|
|
2
2
|
export { useHostTools } from './useHostTools';
|
|
3
3
|
export { useNetwork } from './useNetwork';
|
|
4
4
|
export { useEnvironment } from './useEnvironment';
|
|
5
|
-
export { useFlags } from './useFlags';
|
|
5
|
+
export { useFlags, useFlagsPresent } from './useFlags';
|
|
6
6
|
export { useModuleDictionary } from './useModuleDictionary';
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ export * from './contexts';
|
|
|
2
2
|
export * from './hooks';
|
|
3
3
|
export { EmitEvents } from './types';
|
|
4
4
|
export type { Maybe, HostToolsType, NetworkProps, EnvironmentType } from './types';
|
|
5
|
+
export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
|
|
5
6
|
export * from './utils';
|
package/dist/index.js
CHANGED
|
@@ -17,8 +17,20 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
var __objRest = (source2, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source2)
|
|
23
|
+
if (__hasOwnProp.call(source2, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source2[prop];
|
|
25
|
+
if (source2 != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source2)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source2, prop))
|
|
28
|
+
target[prop] = source2[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
import { createContext, useState, useContext, useCallback, useEffect, useLayoutEffect } from "react";
|
|
33
|
+
import { jsx } from "react/jsx-runtime";
|
|
22
34
|
const initialValue$2 = {
|
|
23
35
|
isLocalhost: true,
|
|
24
36
|
host: "",
|
|
@@ -29,6 +41,18 @@ const initialValue$2 = {
|
|
|
29
41
|
environment: ""
|
|
30
42
|
};
|
|
31
43
|
const EnvironmentContext = createContext(initialValue$2);
|
|
44
|
+
function EnvironmentProvider(props) {
|
|
45
|
+
const _a = props, {
|
|
46
|
+
children
|
|
47
|
+
} = _a, other = __objRest(_a, [
|
|
48
|
+
"children"
|
|
49
|
+
]);
|
|
50
|
+
const [finalEnvironment] = useState(other);
|
|
51
|
+
return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
|
|
52
|
+
value: finalEnvironment,
|
|
53
|
+
children
|
|
54
|
+
});
|
|
55
|
+
}
|
|
32
56
|
function voidFunction() {
|
|
33
57
|
}
|
|
34
58
|
function getPropertyByString(object, propString) {
|
|
@@ -1500,6 +1524,18 @@ const initialValue$1 = {
|
|
|
1500
1524
|
events_emit: voidFunction
|
|
1501
1525
|
};
|
|
1502
1526
|
const HostToolsContext = createContext(initialValue$1);
|
|
1527
|
+
function HostToolsProvider(props) {
|
|
1528
|
+
const _a = props, {
|
|
1529
|
+
children
|
|
1530
|
+
} = _a, hostTools = __objRest(_a, [
|
|
1531
|
+
"children"
|
|
1532
|
+
]);
|
|
1533
|
+
const [finalTools] = useState(hostTools);
|
|
1534
|
+
return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
|
|
1535
|
+
value: finalTools,
|
|
1536
|
+
children
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1503
1539
|
const useEnvironment = () => {
|
|
1504
1540
|
const context = useContext(EnvironmentContext);
|
|
1505
1541
|
if (!context)
|
|
@@ -1516,12 +1552,53 @@ const initialValue = {
|
|
|
1516
1552
|
networkOperation: () => Promise.resolve()
|
|
1517
1553
|
};
|
|
1518
1554
|
const NetworkContext = createContext(initialValue);
|
|
1555
|
+
function NetworkProvider(props) {
|
|
1556
|
+
const {
|
|
1557
|
+
children,
|
|
1558
|
+
axiosOperation: axiosOperation2
|
|
1559
|
+
} = props;
|
|
1560
|
+
const environment = useEnvironment();
|
|
1561
|
+
const hostTools = useHostTools();
|
|
1562
|
+
const networkOperation = useCallback(async (networkProps) => {
|
|
1563
|
+
return axiosOperation2(networkProps, environment, hostTools);
|
|
1564
|
+
}, [axiosOperation2]);
|
|
1565
|
+
return /* @__PURE__ */ jsx(NetworkContext.Provider, {
|
|
1566
|
+
value: {
|
|
1567
|
+
networkOperation
|
|
1568
|
+
},
|
|
1569
|
+
children
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1519
1572
|
const initialState$1 = {
|
|
1520
1573
|
flags: [],
|
|
1521
1574
|
clearFlags: voidFunction,
|
|
1522
1575
|
addFlag: voidFunction
|
|
1523
1576
|
};
|
|
1524
1577
|
const FlagsContext = createContext(initialState$1);
|
|
1578
|
+
function FlagsProvider({
|
|
1579
|
+
children
|
|
1580
|
+
}) {
|
|
1581
|
+
const [flags, setFlags] = useState([]);
|
|
1582
|
+
const clearFlags = useCallback(() => {
|
|
1583
|
+
setFlags([]);
|
|
1584
|
+
}, []);
|
|
1585
|
+
const addFlag = useCallback((newFlag) => {
|
|
1586
|
+
setFlags((oldFlags) => {
|
|
1587
|
+
if (oldFlags.findIndex((f) => f === newFlag) < 0) {
|
|
1588
|
+
return [...oldFlags, newFlag];
|
|
1589
|
+
}
|
|
1590
|
+
return [...oldFlags];
|
|
1591
|
+
});
|
|
1592
|
+
}, []);
|
|
1593
|
+
return /* @__PURE__ */ jsx(FlagsContext.Provider, {
|
|
1594
|
+
value: {
|
|
1595
|
+
flags,
|
|
1596
|
+
addFlag,
|
|
1597
|
+
clearFlags
|
|
1598
|
+
},
|
|
1599
|
+
children
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1525
1602
|
function useLocalStorage(key, initialValue2) {
|
|
1526
1603
|
const [value, setValue] = useState(() => {
|
|
1527
1604
|
try {
|
|
@@ -1553,6 +1630,20 @@ const useFlags = () => {
|
|
|
1553
1630
|
throw new Error("useFlags context must be use inside FlagsProvider");
|
|
1554
1631
|
return context;
|
|
1555
1632
|
};
|
|
1633
|
+
function isFlagsPresent(compareFlags, flags) {
|
|
1634
|
+
const filterFlags = compareFlags.filter((findFlag) => flags.findIndex((sFlag) => sFlag === findFlag) !== -1);
|
|
1635
|
+
return filterFlags.length === compareFlags.length;
|
|
1636
|
+
}
|
|
1637
|
+
const useFlagsPresent = (compareFlags) => {
|
|
1638
|
+
const context = useFlags();
|
|
1639
|
+
const [isPresent, setIsPresent] = useState(isFlagsPresent(compareFlags, context.flags));
|
|
1640
|
+
useEffect(() => {
|
|
1641
|
+
if (isFlagsPresent(compareFlags, context.flags)) {
|
|
1642
|
+
setIsPresent(true);
|
|
1643
|
+
}
|
|
1644
|
+
}, [context.flags, compareFlags]);
|
|
1645
|
+
return isPresent;
|
|
1646
|
+
};
|
|
1556
1647
|
const useModuleDictionary = () => {
|
|
1557
1648
|
const context = useContext(ModuleDictionaryContext);
|
|
1558
1649
|
if (!context)
|
|
@@ -1564,4 +1655,77 @@ const initialState = {
|
|
|
1564
1655
|
getModuleLabel: () => "No dictionary context"
|
|
1565
1656
|
};
|
|
1566
1657
|
const ModuleDictionaryContext = createContext(initialState);
|
|
1567
|
-
|
|
1658
|
+
function ModuleDictionaryProvider(props) {
|
|
1659
|
+
const {
|
|
1660
|
+
children,
|
|
1661
|
+
componentsDictionary,
|
|
1662
|
+
moduleId,
|
|
1663
|
+
moduleName = "module_name",
|
|
1664
|
+
currentLang = "us",
|
|
1665
|
+
isAuth = true
|
|
1666
|
+
} = props;
|
|
1667
|
+
const {
|
|
1668
|
+
addFlag
|
|
1669
|
+
} = useFlags();
|
|
1670
|
+
const [moduleDictionary, setModuleDictionary] = useState(void 0);
|
|
1671
|
+
const {
|
|
1672
|
+
domain_token
|
|
1673
|
+
} = useEnvironment();
|
|
1674
|
+
const {
|
|
1675
|
+
startProgress,
|
|
1676
|
+
stopProgress
|
|
1677
|
+
} = useHostTools();
|
|
1678
|
+
const {
|
|
1679
|
+
networkOperation
|
|
1680
|
+
} = useNetwork();
|
|
1681
|
+
useLayoutEffect(() => {
|
|
1682
|
+
let mounted = true;
|
|
1683
|
+
startProgress();
|
|
1684
|
+
networkOperation({
|
|
1685
|
+
method: "GET",
|
|
1686
|
+
endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
|
|
1687
|
+
parms: __spreadValues({
|
|
1688
|
+
comps: componentsDictionary
|
|
1689
|
+
}, isAuth ? {} : {
|
|
1690
|
+
domain_token
|
|
1691
|
+
})
|
|
1692
|
+
}).then((response) => {
|
|
1693
|
+
if (mounted) {
|
|
1694
|
+
setModuleDictionary(__spreadValues({}, response));
|
|
1695
|
+
addFlag("dictionary_loaded");
|
|
1696
|
+
}
|
|
1697
|
+
}).finally(() => {
|
|
1698
|
+
stopProgress();
|
|
1699
|
+
});
|
|
1700
|
+
return function cleanUp() {
|
|
1701
|
+
mounted = false;
|
|
1702
|
+
};
|
|
1703
|
+
}, [currentLang]);
|
|
1704
|
+
const getLabel = useCallback((key) => {
|
|
1705
|
+
if (moduleDictionary === void 0)
|
|
1706
|
+
return "No dictionary";
|
|
1707
|
+
if (key === void 0)
|
|
1708
|
+
return "No key";
|
|
1709
|
+
const parts = key.split(".");
|
|
1710
|
+
if (parts.length === 2) {
|
|
1711
|
+
if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
|
|
1712
|
+
return moduleDictionary[parts[0]][parts[1]];
|
|
1713
|
+
}
|
|
1714
|
+
} else if (parts.length === 1) {
|
|
1715
|
+
if (moduleDictionary.data && moduleDictionary.data[key]) {
|
|
1716
|
+
return moduleDictionary.data[key];
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
return `No dictionary:${key}`;
|
|
1720
|
+
}, [moduleDictionary]);
|
|
1721
|
+
const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
|
|
1722
|
+
return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
|
|
1723
|
+
value: {
|
|
1724
|
+
moduleDictionary,
|
|
1725
|
+
getLabel,
|
|
1726
|
+
getModuleLabel
|
|
1727
|
+
},
|
|
1728
|
+
children
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
export { EmitEvents, EnvironmentContext, EnvironmentProvider, FlagsContext, FlagsProvider, HostToolsContext, HostToolsProvider, ModuleDictionaryContext, ModuleDictionaryProvider, NetworkContext, NetworkProvider, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useFlags, useFlagsPresent, useHostTools, useLocalStorage, useModuleDictionary, useNetwork, voidFunction };
|