@m4l/core 0.0.21 → 0.0.22

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
- import { createContext } from "react";
2
- import "react/jsx-runtime";
1
+ import { createContext, useState } from "react";
2
+ import { jsx } from "react/jsx-runtime";
3
3
  const initialValue = {
4
4
  isLocalhost: true,
5
5
  host: "",
@@ -10,4 +10,15 @@ const initialValue = {
10
10
  environment: ""
11
11
  };
12
12
  const EnvironmentContext = createContext(initialValue);
13
- export { EnvironmentContext as E };
13
+ function EnvironmentProvider(props) {
14
+ const {
15
+ children,
16
+ ...other
17
+ } = props;
18
+ const [finalEnvironment] = useState(other);
19
+ return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
20
+ value: finalEnvironment,
21
+ children
22
+ });
23
+ }
24
+ export { EnvironmentContext as E, EnvironmentProvider as a };
@@ -1,6 +1,6 @@
1
- import { createContext } from "react";
1
+ import { createContext, useState } from "react";
2
2
  import { v as voidFunction } from "./voidFunction.js";
3
- import "react/jsx-runtime";
3
+ import { jsx } from "react/jsx-runtime";
4
4
  const initialValue = {
5
5
  toast: () => 0,
6
6
  startProgress: voidFunction,
@@ -10,4 +10,15 @@ const initialValue = {
10
10
  events_emit: voidFunction
11
11
  };
12
12
  const HostToolsContext = createContext(initialValue);
13
- export { HostToolsContext as H };
13
+ function HostToolsProvider(props) {
14
+ const {
15
+ children,
16
+ ...hostTools
17
+ } = props;
18
+ const [finalTools] = useState(hostTools);
19
+ return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
20
+ value: finalTools,
21
+ children
22
+ });
23
+ }
24
+ export { HostToolsContext as H, HostToolsProvider as a };
@@ -1,9 +1,26 @@
1
- import { createContext } from "react";
2
- import "./EnvironmentContext.js";
3
- import "./HostToolsContext.js";
4
- import "react/jsx-runtime";
1
+ import { createContext, useCallback } from "react";
2
+ import { u as useEnvironment } from "./useEnvironment.js";
3
+ import { u as useHostTools } from "./useHostTools.js";
4
+ import { jsx } from "react/jsx-runtime";
5
5
  const initialValue = {
6
6
  networkOperation: () => Promise.resolve()
7
7
  };
8
8
  const NetworkContext = createContext(initialValue);
9
- export { NetworkContext as N };
9
+ function NetworkProvider(props) {
10
+ const {
11
+ children,
12
+ axiosOperation
13
+ } = props;
14
+ const environment = useEnvironment();
15
+ const hostTools = useHostTools();
16
+ const networkOperation = useCallback(async (networkProps) => {
17
+ return axiosOperation(networkProps, environment, hostTools);
18
+ }, [axiosOperation]);
19
+ return /* @__PURE__ */ jsx(NetworkContext.Provider, {
20
+ value: {
21
+ networkOperation
22
+ },
23
+ children
24
+ });
25
+ }
26
+ export { NetworkContext as N, NetworkProvider as a };
@@ -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
+ }
@@ -0,0 +1,5 @@
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';
@@ -0,0 +1,6 @@
1
+ export { useLocalStorage } from './useLocalStorage';
2
+ export { useHostTools } from './useHostTools';
3
+ export { useNetwork } from './useNetwork';
4
+ export { useEnvironment } from './useEnvironment';
5
+ export { useFlags, useFlagsPresent } 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
@@ -1,4 +1,6 @@
1
+ export * from './contexts';
1
2
  export { useEnvironment } from './hooks/useEnvironment';
3
+ export { useFlags } from './hooks/useFlags';
2
4
  export { useHostTools } from './hooks/useHostTools';
3
5
  export { useLocalStorage } from './hooks/useLocalStorage';
4
6
  export { useNetwork } from './hooks/useNetwork';
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
+ export { E as EnvironmentContext, a as EnvironmentProvider } from "./EnvironmentContext.js";
2
+ export { H as HostToolsContext, a as HostToolsProvider } from "./HostToolsContext.js";
3
+ export { N as NetworkContext, a as NetworkProvider } from "./NetworkContext.js";
4
+ export { E as EmitEvents, F as FlagsContext, a as FlagsProvider, M as ModuleDictionaryContext, b as ModuleDictionaryProvider, c as getLocalStorage, g as getPropertyByString, s as setLocalStorage, u as useFlags } from "./vendor.js";
1
5
  export { u as useEnvironment } from "./useEnvironment.js";
2
6
  export { u as useHostTools } from "./useHostTools.js";
3
7
  export { u as useLocalStorage } from "./useLocalStorage.js";
4
8
  export { u as useNetwork } from "./useNetwork.js";
5
- export { E as EmitEvents, a as getLocalStorage, g as getPropertyByString, s as setLocalStorage } from "./vendor.js";
6
9
  export { v as voidFunction } from "./voidFunction.js";
7
10
  export { a as axiosOperation } from "./axios.js";
8
11
  import "react";
9
- import "./EnvironmentContext.js";
10
12
  import "react/jsx-runtime";
11
- import "./HostToolsContext.js";
12
- import "./NetworkContext.js";
13
13
  import "./snakecase-keys.js";
package/dist/vendor.js CHANGED
@@ -1,9 +1,131 @@
1
+ import { createContext, useState, useCallback, useContext, useLayoutEffect } from "react";
2
+ import "./HostToolsContext.js";
3
+ import "./NetworkContext.js";
4
+ import "./EnvironmentContext.js";
1
5
  import "./axios.js";
2
- import "./useEnvironment.js";
3
- import "./useHostTools.js";
6
+ import { v as voidFunction } from "./voidFunction.js";
7
+ import { jsx } from "react/jsx-runtime";
8
+ import { u as useHostTools } from "./useHostTools.js";
9
+ import { u as useNetwork } from "./useNetwork.js";
10
+ import { u as useEnvironment } from "./useEnvironment.js";
4
11
  import "./useLocalStorage.js";
5
- import "./useNetwork.js";
6
- import "./voidFunction.js";
12
+ const initialState$1 = {
13
+ flags: [],
14
+ clearFlags: voidFunction,
15
+ addFlag: voidFunction
16
+ };
17
+ const FlagsContext = createContext(initialState$1);
18
+ function FlagsProvider({
19
+ children
20
+ }) {
21
+ const [flags, setFlags] = useState([]);
22
+ const clearFlags = useCallback(() => {
23
+ setFlags([]);
24
+ }, []);
25
+ const addFlag = useCallback((newFlag) => {
26
+ setFlags((oldFlags) => {
27
+ if (oldFlags.findIndex((f) => f === newFlag) < 0) {
28
+ return [...oldFlags, newFlag];
29
+ }
30
+ return [...oldFlags];
31
+ });
32
+ }, []);
33
+ return /* @__PURE__ */ jsx(FlagsContext.Provider, {
34
+ value: {
35
+ flags,
36
+ addFlag,
37
+ clearFlags
38
+ },
39
+ children
40
+ });
41
+ }
42
+ const useFlags = () => {
43
+ const context = useContext(FlagsContext);
44
+ if (!context)
45
+ throw new Error("useFlags context must be use inside FlagsProvider");
46
+ return context;
47
+ };
48
+ const initialState = {
49
+ getLabel: () => "..",
50
+ getModuleLabel: () => "No dictionary context"
51
+ };
52
+ const ModuleDictionaryContext = createContext(initialState);
53
+ function ModuleDictionaryProvider(props) {
54
+ const {
55
+ children,
56
+ componentsDictionary,
57
+ moduleId,
58
+ moduleName = "module_name",
59
+ currentLang = "us",
60
+ isAuth = true
61
+ } = props;
62
+ const {
63
+ addFlag
64
+ } = useFlags();
65
+ const [moduleDictionary, setModuleDictionary] = useState(void 0);
66
+ const {
67
+ domain_token
68
+ } = useEnvironment();
69
+ const {
70
+ startProgress,
71
+ stopProgress
72
+ } = useHostTools();
73
+ const {
74
+ networkOperation
75
+ } = useNetwork();
76
+ useLayoutEffect(() => {
77
+ let mounted = true;
78
+ startProgress();
79
+ networkOperation({
80
+ method: "GET",
81
+ endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
82
+ parms: {
83
+ comps: componentsDictionary,
84
+ ...isAuth ? {} : {
85
+ domain_token
86
+ }
87
+ }
88
+ }).then((response) => {
89
+ if (mounted) {
90
+ setModuleDictionary({
91
+ ...response
92
+ });
93
+ addFlag("dictionary_loaded");
94
+ }
95
+ }).finally(() => {
96
+ stopProgress();
97
+ });
98
+ return function cleanUp() {
99
+ mounted = false;
100
+ };
101
+ }, [currentLang]);
102
+ const getLabel = useCallback((key) => {
103
+ if (moduleDictionary === void 0)
104
+ return "No dictionary";
105
+ if (key === void 0)
106
+ return "No key";
107
+ const parts = key.split(".");
108
+ if (parts.length === 2) {
109
+ if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
110
+ return moduleDictionary[parts[0]][parts[1]];
111
+ }
112
+ } else if (parts.length === 1) {
113
+ if (moduleDictionary.data && moduleDictionary.data[key]) {
114
+ return moduleDictionary.data[key];
115
+ }
116
+ }
117
+ return `No dictionary:${key}`;
118
+ }, [moduleDictionary]);
119
+ const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
120
+ return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
121
+ value: {
122
+ moduleDictionary,
123
+ getLabel,
124
+ getModuleLabel
125
+ },
126
+ children
127
+ });
128
+ }
7
129
  var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
8
130
  EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
9
131
  EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
@@ -150,4 +272,4 @@ const dist_es2015 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineP
150
272
  snakeCase
151
273
  }, Symbol.toStringTag, { value: "Module" }));
152
274
  const require$$1 = /* @__PURE__ */ getAugmentedNamespace(dist_es2015);
153
- export { EmitEvents as E, getLocalStorage as a, getPropertyByString as g, mapObj as m, require$$1 as r, setLocalStorage as s };
275
+ export { EmitEvents as E, FlagsContext as F, ModuleDictionaryContext as M, FlagsProvider as a, ModuleDictionaryProvider as b, getLocalStorage as c, getPropertyByString as g, mapObj as m, require$$1 as r, setLocalStorage as s, useFlags as u };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.21",
4
+ "version": "0.0.22",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {