@m4l/core 0.0.3 → 0.0.4

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.
@@ -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/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
- import { createContext, useContext, useState } from "react";
21
- import "react/jsx-runtime";
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, 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 {
@@ -1564,4 +1641,77 @@ const initialState = {
1564
1641
  getModuleLabel: () => "No dictionary context"
1565
1642
  };
1566
1643
  const ModuleDictionaryContext = createContext(initialState);
1567
- export { EmitEvents, EnvironmentContext, FlagsContext, HostToolsContext, ModuleDictionaryContext, NetworkContext, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useFlags, useHostTools, useLocalStorage, useModuleDictionary, useNetwork, voidFunction };
1644
+ function ModuleDictionaryProvider(props) {
1645
+ const {
1646
+ children,
1647
+ componentsDictionary,
1648
+ moduleId,
1649
+ moduleName = "module_name",
1650
+ currentLang = "us",
1651
+ isAuth = true
1652
+ } = props;
1653
+ const {
1654
+ addFlag
1655
+ } = useFlags();
1656
+ const [moduleDictionary, setModuleDictionary] = useState(void 0);
1657
+ const {
1658
+ domain_token
1659
+ } = useEnvironment();
1660
+ const {
1661
+ startProgress,
1662
+ stopProgress
1663
+ } = useHostTools();
1664
+ const {
1665
+ networkOperation
1666
+ } = useNetwork();
1667
+ useLayoutEffect(() => {
1668
+ let mounted = true;
1669
+ startProgress();
1670
+ networkOperation({
1671
+ method: "GET",
1672
+ endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
1673
+ parms: __spreadValues({
1674
+ comps: componentsDictionary
1675
+ }, isAuth ? {} : {
1676
+ domain_token
1677
+ })
1678
+ }).then((response) => {
1679
+ if (mounted) {
1680
+ setModuleDictionary(__spreadValues({}, response));
1681
+ addFlag("dictionary_loaded");
1682
+ }
1683
+ }).finally(() => {
1684
+ stopProgress();
1685
+ });
1686
+ return function cleanUp() {
1687
+ mounted = false;
1688
+ };
1689
+ }, [currentLang]);
1690
+ const getLabel = useCallback((key) => {
1691
+ if (moduleDictionary === void 0)
1692
+ return "No dictionary";
1693
+ if (key === void 0)
1694
+ return "No key";
1695
+ const parts = key.split(".");
1696
+ if (parts.length === 2) {
1697
+ if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
1698
+ return moduleDictionary[parts[0]][parts[1]];
1699
+ }
1700
+ } else if (parts.length === 1) {
1701
+ if (moduleDictionary.data && moduleDictionary.data[key]) {
1702
+ return moduleDictionary.data[key];
1703
+ }
1704
+ }
1705
+ return `No dictionary:${key}`;
1706
+ }, [moduleDictionary]);
1707
+ const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
1708
+ return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
1709
+ value: {
1710
+ moduleDictionary,
1711
+ getLabel,
1712
+ getModuleLabel
1713
+ },
1714
+ children
1715
+ });
1716
+ }
1717
+ export { EmitEvents, EnvironmentContext, EnvironmentProvider, FlagsContext, FlagsProvider, HostToolsContext, HostToolsProvider, ModuleDictionaryContext, ModuleDictionaryProvider, NetworkContext, NetworkProvider, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useFlags, useHostTools, useLocalStorage, useModuleDictionary, useNetwork, voidFunction };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.3",
4
+ "version": "0.0.4",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {