@m4l/core 0.0.17 → 0.0.20

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/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- export * from './contexts';
2
1
  export { useEnvironment } from './hooks/useEnvironment';
2
+ export { useFlags } from './hooks/useFlags';
3
+ export { useHostTools } from './hooks/useHostTools';
4
+ export { useLocalStorage } from './hooks/useLocalStorage';
5
+ export { useNetwork } from './hooks/useNetwork';
3
6
  export { EmitEvents } from './types';
4
7
  export type { Maybe, HostToolsType, NetworkProps, EnvironmentType, AxiosOperation, EventFunListener, } from './types';
5
8
  export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { createContext, useState, useContext, useCallback, useLayoutEffect } from "react";
2
- import { jsx } from "react/jsx-runtime";
1
+ import { createContext, useContext, useState } from "react";
2
+ import "react/jsx-runtime";
3
3
  import { a as axios } from "./axios.js";
4
4
  import { s as snakecaseKeys } from "./snakecase-keys.js";
5
5
  const initialValue$2 = {
@@ -12,19 +12,26 @@ const initialValue$2 = {
12
12
  environment: ""
13
13
  };
14
14
  const EnvironmentContext = createContext(initialValue$2);
15
- function EnvironmentProvider(props) {
16
- const {
17
- children,
18
- ...other
19
- } = props;
20
- const [finalEnvironment] = useState(other);
21
- return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
22
- value: finalEnvironment,
23
- children
24
- });
25
- }
15
+ const useEnvironment = () => {
16
+ const context = useContext(EnvironmentContext);
17
+ if (!context)
18
+ throw new Error("useEnvironment context must be use inside EnvironmentContext");
19
+ return context;
20
+ };
26
21
  function voidFunction() {
27
22
  }
23
+ const initialState = {
24
+ flags: [],
25
+ clearFlags: voidFunction,
26
+ addFlag: voidFunction
27
+ };
28
+ const FlagsContext = createContext(initialState);
29
+ const useFlags = () => {
30
+ const context = useContext(FlagsContext);
31
+ if (!context)
32
+ throw new Error("useFlags context must be use inside FlagsProvider");
33
+ return context;
34
+ };
28
35
  const initialValue$1 = {
29
36
  toast: () => 0,
30
37
  startProgress: voidFunction,
@@ -34,173 +41,41 @@ const initialValue$1 = {
34
41
  events_emit: voidFunction
35
42
  };
36
43
  const HostToolsContext = createContext(initialValue$1);
37
- function HostToolsProvider(props) {
38
- const {
39
- children,
40
- ...hostTools
41
- } = props;
42
- const [finalTools] = useState(hostTools);
43
- return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
44
- value: finalTools,
45
- children
46
- });
47
- }
48
- const useEnvironment = () => {
49
- const context = useContext(EnvironmentContext);
50
- if (!context)
51
- throw new Error("useEnvironment context must be use inside EnvironmentContext");
52
- return context;
53
- };
54
44
  const useHostTools = () => {
55
45
  const context = useContext(HostToolsContext);
56
46
  if (!context)
57
47
  throw new Error("useHostTools context must be use inside HostToolsContext");
58
48
  return context;
59
49
  };
50
+ function useLocalStorage(key, initialValue2) {
51
+ const [value, setValue] = useState(() => {
52
+ try {
53
+ const item = window.localStorage.getItem(key);
54
+ return item !== null ? JSON.parse(item) : initialValue2;
55
+ } catch (e) {
56
+ return initialValue2;
57
+ }
58
+ });
59
+ const setValueInLocalStorage = (newValue) => {
60
+ try {
61
+ window.localStorage.setItem(key, JSON.stringify(newValue));
62
+ setValue(newValue);
63
+ } catch (e) {
64
+ console.error(e);
65
+ }
66
+ };
67
+ return [value, setValueInLocalStorage];
68
+ }
60
69
  const initialValue = {
61
70
  networkOperation: () => Promise.resolve()
62
71
  };
63
72
  const NetworkContext = createContext(initialValue);
64
- function NetworkProvider(props) {
65
- const {
66
- children,
67
- axiosOperation: axiosOperation2
68
- } = props;
69
- const environment = useEnvironment();
70
- const hostTools = useHostTools();
71
- const networkOperation = useCallback(async (networkProps) => {
72
- return axiosOperation2(networkProps, environment, hostTools);
73
- }, [axiosOperation2]);
74
- return /* @__PURE__ */ jsx(NetworkContext.Provider, {
75
- value: {
76
- networkOperation
77
- },
78
- children
79
- });
80
- }
81
- const initialState$1 = {
82
- flags: [],
83
- clearFlags: voidFunction,
84
- addFlag: voidFunction
85
- };
86
- const FlagsContext = createContext(initialState$1);
87
- function FlagsProvider({
88
- children
89
- }) {
90
- const [flags, setFlags] = useState([]);
91
- const clearFlags = useCallback(() => {
92
- setFlags([]);
93
- }, []);
94
- const addFlag = useCallback((newFlag) => {
95
- setFlags((oldFlags) => {
96
- if (oldFlags.findIndex((f) => f === newFlag) < 0) {
97
- return [...oldFlags, newFlag];
98
- }
99
- return [...oldFlags];
100
- });
101
- }, []);
102
- return /* @__PURE__ */ jsx(FlagsContext.Provider, {
103
- value: {
104
- flags,
105
- addFlag,
106
- clearFlags
107
- },
108
- children
109
- });
110
- }
111
73
  const useNetwork = () => {
112
74
  const context = useContext(NetworkContext);
113
75
  if (!context)
114
76
  throw new Error("useNetwork context must be use inside NetworkContext");
115
77
  return context;
116
78
  };
117
- const useFlags = () => {
118
- const context = useContext(FlagsContext);
119
- if (!context)
120
- throw new Error("useFlags context must be use inside FlagsProvider");
121
- return context;
122
- };
123
- const initialState = {
124
- getLabel: () => "..",
125
- getModuleLabel: () => "No dictionary context"
126
- };
127
- const ModuleDictionaryContext = createContext(initialState);
128
- function ModuleDictionaryProvider(props) {
129
- const {
130
- children,
131
- componentsDictionary,
132
- moduleId,
133
- moduleName = "module_name",
134
- currentLang = "us",
135
- isAuth = true
136
- } = props;
137
- const {
138
- addFlag
139
- } = useFlags();
140
- const [moduleDictionary, setModuleDictionary] = useState(void 0);
141
- const {
142
- domain_token
143
- } = useEnvironment();
144
- const {
145
- startProgress,
146
- stopProgress
147
- } = useHostTools();
148
- const {
149
- networkOperation
150
- } = useNetwork();
151
- useLayoutEffect(() => {
152
- let mounted = true;
153
- startProgress();
154
- networkOperation({
155
- method: "GET",
156
- endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
157
- parms: {
158
- comps: componentsDictionary,
159
- ...isAuth ? {} : {
160
- domain_token
161
- }
162
- }
163
- }).then((response) => {
164
- if (mounted) {
165
- setModuleDictionary({
166
- ...response
167
- });
168
- addFlag("dictionary_loaded");
169
- }
170
- }).finally(() => {
171
- stopProgress();
172
- });
173
- return function cleanUp() {
174
- mounted = false;
175
- };
176
- }, [currentLang]);
177
- const getLabel = useCallback((key) => {
178
- if (moduleDictionary === void 0)
179
- return "No dictionary";
180
- if (key === void 0)
181
- return "No key";
182
- const parts = key.split(".");
183
- if (parts.length === 2) {
184
- if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
185
- return moduleDictionary[parts[0]][parts[1]];
186
- }
187
- } else if (parts.length === 1) {
188
- if (moduleDictionary.data && moduleDictionary.data[key]) {
189
- return moduleDictionary.data[key];
190
- }
191
- }
192
- return `No dictionary:${key}`;
193
- }, [moduleDictionary]);
194
- const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
195
- return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
196
- value: {
197
- moduleDictionary,
198
- getLabel,
199
- getModuleLabel
200
- },
201
- children
202
- });
203
- }
204
79
  var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
205
80
  EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
206
81
  EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
@@ -311,4 +186,4 @@ const axiosOperation = async (props, enviroment, hostTools) => {
311
186
  hostTools.stopProgress();
312
187
  });
313
188
  };
314
- export { EmitEvents, EnvironmentContext, EnvironmentProvider, FlagsContext, FlagsProvider, HostToolsContext, HostToolsProvider, ModuleDictionaryContext, ModuleDictionaryProvider, NetworkContext, NetworkProvider, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, voidFunction };
189
+ export { EmitEvents, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useFlags, useHostTools, useLocalStorage, 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.17",
4
+ "version": "0.0.20",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {
@@ -1,5 +0,0 @@
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 };
@@ -1,15 +0,0 @@
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,5 +0,0 @@
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';
@@ -1,6 +0,0 @@
1
- export { useLocalStorage } from './useLocalStorage/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';
@@ -1 +0,0 @@
1
- export declare const useModuleDictionary: () => import("../../contexts/ModuleDictionaryContext/types").ModuleDictionaryContextProps;