@m4l/core 0.0.2 → 0.0.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.
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import { FlagsContextProps, FlagsProviderProps } from './types';
3
+ declare const FlagsContext: import("react").Context<FlagsContextProps>;
4
+ declare function FlagsProvider({ children }: FlagsProviderProps): JSX.Element;
5
+ export { FlagsProvider, FlagsContext };
@@ -0,0 +1,10 @@
1
+ import { ReactNode } from 'react';
2
+ export interface FlagsProviderProps {
3
+ children: ReactNode;
4
+ }
5
+ export declare type Flag = string;
6
+ export interface FlagsContextProps {
7
+ flags: Array<Flag>;
8
+ clearFlags: () => void;
9
+ addFlag: (flag: Flag) => void;
10
+ }
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import { ModuleDictionaryContextProps, ModuleDictionaryProviderProps } from './types';
3
+ declare const ModuleDictionaryContext: import("react").Context<ModuleDictionaryContextProps>;
4
+ declare function ModuleDictionaryProvider(props: ModuleDictionaryProviderProps): JSX.Element;
5
+ export { ModuleDictionaryProvider, ModuleDictionaryContext };
@@ -0,0 +1,15 @@
1
+ import { ReactNode } from 'react';
2
+ import type { GetLabelType, ModuleDictionary } from 'src/types/dictionary';
3
+ export interface ModuleDictionaryProviderProps {
4
+ currentLang?: string;
5
+ isAuth?: boolean;
6
+ moduleId: number;
7
+ moduleName?: string;
8
+ componentsDictionary: string[];
9
+ children: ReactNode;
10
+ }
11
+ export interface ModuleDictionaryContextProps {
12
+ moduleDictionary?: ModuleDictionary;
13
+ getLabel: GetLabelType;
14
+ getModuleLabel: () => string;
15
+ }
@@ -1,3 +1,5 @@
1
- export { EnvironmentContext } from './EnvironmentContext';
2
- export { HostToolsContext } from './HostToolsContext';
3
- export { NetworkContext } from './NetworkContext';
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';
@@ -2,3 +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';
6
+ export { useModuleDictionary } from './useModuleDictionary';
@@ -0,0 +1,3 @@
1
+ import type { Flag, FlagsContextProps } from '../../contexts/FlagsContext/types';
2
+ export declare const useFlags: () => FlagsContextProps;
3
+ export declare const useFlagsPresent: (compareFlags: Array<Flag>) => boolean;
@@ -0,0 +1 @@
1
+ export declare const useModuleDictionary: () => import("../../contexts/ModuleDictionaryContext/types").ModuleDictionaryContextProps;
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
- 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,6 +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
+ }
1572
+ const initialState$1 = {
1573
+ flags: [],
1574
+ clearFlags: voidFunction,
1575
+ addFlag: voidFunction
1576
+ };
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
+ }
1519
1602
  function useLocalStorage(key, initialValue2) {
1520
1603
  const [value, setValue] = useState(() => {
1521
1604
  try {
@@ -1541,4 +1624,94 @@ const useNetwork = () => {
1541
1624
  throw new Error("useNetwork context must be use inside NetworkContext");
1542
1625
  return context;
1543
1626
  };
1544
- export { EmitEvents, EnvironmentContext, HostToolsContext, NetworkContext, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useHostTools, useLocalStorage, useNetwork, voidFunction };
1627
+ const useFlags = () => {
1628
+ const context = useContext(FlagsContext);
1629
+ if (!context)
1630
+ throw new Error("useFlags context must be use inside FlagsProvider");
1631
+ return context;
1632
+ };
1633
+ const useModuleDictionary = () => {
1634
+ const context = useContext(ModuleDictionaryContext);
1635
+ if (!context)
1636
+ throw new Error("useModuleDictionary context must be use inside ModuleDictionaryProvider");
1637
+ return context;
1638
+ };
1639
+ const initialState = {
1640
+ getLabel: () => "..",
1641
+ getModuleLabel: () => "No dictionary context"
1642
+ };
1643
+ const ModuleDictionaryContext = createContext(initialState);
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 };
@@ -0,0 +1,13 @@
1
+ export declare type GetLabelType = (key: string) => string;
2
+ export interface ComponentDictionary {
3
+ [key: string]: string;
4
+ }
5
+ export interface Dictionary {
6
+ [key: string]: ComponentDictionary | boolean | undefined | string;
7
+ }
8
+ export interface DataDictionary extends ComponentDictionary {
9
+ module_name: string;
10
+ }
11
+ export interface ModuleDictionary extends Dictionary {
12
+ data: DataDictionary;
13
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.2",
4
+ "version": "0.0.5",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {
@@ -33,7 +33,6 @@
33
33
  "@typescript-eslint/parser": "^5.27.1",
34
34
  "@vitejs/plugin-react": "^1.3.0",
35
35
  "axios": "^0.27.2",
36
- "c8": "^7.11.3",
37
36
  "eslint": "^8.17.0",
38
37
  "eslint-config-prettier": "^8.5.0",
39
38
  "eslint-import-resolver-alias": "^1.1.2",