@m4l/core 0.0.16 → 0.0.19

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,4 @@
1
- export * from './contexts';
2
- export * from './hooks';
1
+ export { useEnvironment } from './hooks/useEnvironment';
3
2
  export { EmitEvents } from './types';
4
3
  export type { Maybe, HostToolsType, NetworkProps, EnvironmentType, AxiosOperation, EventFunListener, } from './types';
5
4
  export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { createContext, useState, useContext, useCallback, useEffect, useLayoutEffect } from "react";
2
- import { jsx } from "react/jsx-runtime";
1
+ import { createContext, useContext } 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
- const initialValue$2 = {
5
+ const initialValue = {
6
6
  isLocalhost: true,
7
7
  host: "",
8
8
  domain_token: "",
@@ -11,240 +11,20 @@ const initialValue$2 = {
11
11
  host_static_assets: "",
12
12
  environment: ""
13
13
  };
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
- }
26
- function voidFunction() {
27
- }
28
- const initialValue$1 = {
29
- toast: () => 0,
30
- startProgress: voidFunction,
31
- stopProgress: voidFunction,
32
- events_add_listener: voidFunction,
33
- events_remove_listener: voidFunction,
34
- events_emit: voidFunction
35
- };
36
- 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
- }
14
+ const EnvironmentContext = createContext(initialValue);
48
15
  const useEnvironment = () => {
49
16
  const context = useContext(EnvironmentContext);
50
17
  if (!context)
51
18
  throw new Error("useEnvironment context must be use inside EnvironmentContext");
52
19
  return context;
53
20
  };
54
- const useHostTools = () => {
55
- const context = useContext(HostToolsContext);
56
- if (!context)
57
- throw new Error("useHostTools context must be use inside HostToolsContext");
58
- return context;
59
- };
60
- const initialValue = {
61
- networkOperation: () => Promise.resolve()
62
- };
63
- 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
- function useLocalStorage(key, initialValue2) {
112
- const [value, setValue] = useState(() => {
113
- try {
114
- const item = window.localStorage.getItem(key);
115
- return item !== null ? JSON.parse(item) : initialValue2;
116
- } catch (e) {
117
- return initialValue2;
118
- }
119
- });
120
- const setValueInLocalStorage = (newValue) => {
121
- try {
122
- window.localStorage.setItem(key, JSON.stringify(newValue));
123
- setValue(newValue);
124
- } catch (e) {
125
- console.error(e);
126
- }
127
- };
128
- return [value, setValueInLocalStorage];
129
- }
130
- const useNetwork = () => {
131
- const context = useContext(NetworkContext);
132
- if (!context)
133
- throw new Error("useNetwork context must be use inside NetworkContext");
134
- return context;
135
- };
136
- const useFlags = () => {
137
- const context = useContext(FlagsContext);
138
- if (!context)
139
- throw new Error("useFlags context must be use inside FlagsProvider");
140
- return context;
141
- };
142
- function isFlagsPresent(compareFlags, flags) {
143
- const filterFlags = compareFlags.filter((findFlag) => flags.findIndex((sFlag) => sFlag === findFlag) !== -1);
144
- return filterFlags.length === compareFlags.length;
145
- }
146
- const useFlagsPresent = (compareFlags) => {
147
- const context = useFlags();
148
- const [isPresent, setIsPresent] = useState(isFlagsPresent(compareFlags, context.flags));
149
- useEffect(() => {
150
- if (isFlagsPresent(compareFlags, context.flags)) {
151
- setIsPresent(true);
152
- }
153
- }, [context.flags, compareFlags]);
154
- return isPresent;
155
- };
156
- const useModuleDictionary = () => {
157
- const context = useContext(ModuleDictionaryContext);
158
- if (!context)
159
- throw new Error("useModuleDictionary context must be use inside ModuleDictionaryProvider");
160
- return context;
161
- };
162
- const initialState = {
163
- getLabel: () => "..",
164
- getModuleLabel: () => "No dictionary context"
165
- };
166
- const ModuleDictionaryContext = createContext(initialState);
167
- function ModuleDictionaryProvider(props) {
168
- const {
169
- children,
170
- componentsDictionary,
171
- moduleId,
172
- moduleName = "module_name",
173
- currentLang = "us",
174
- isAuth = true
175
- } = props;
176
- const {
177
- addFlag
178
- } = useFlags();
179
- const [moduleDictionary, setModuleDictionary] = useState(void 0);
180
- const {
181
- domain_token
182
- } = useEnvironment();
183
- const {
184
- startProgress,
185
- stopProgress
186
- } = useHostTools();
187
- const {
188
- networkOperation
189
- } = useNetwork();
190
- useLayoutEffect(() => {
191
- let mounted = true;
192
- startProgress();
193
- networkOperation({
194
- method: "GET",
195
- endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
196
- parms: {
197
- comps: componentsDictionary,
198
- ...isAuth ? {} : {
199
- domain_token
200
- }
201
- }
202
- }).then((response) => {
203
- if (mounted) {
204
- setModuleDictionary({
205
- ...response
206
- });
207
- addFlag("dictionary_loaded");
208
- }
209
- }).finally(() => {
210
- stopProgress();
211
- });
212
- return function cleanUp() {
213
- mounted = false;
214
- };
215
- }, [currentLang]);
216
- const getLabel = useCallback((key) => {
217
- if (moduleDictionary === void 0)
218
- return "No dictionary";
219
- if (key === void 0)
220
- return "No key";
221
- const parts = key.split(".");
222
- if (parts.length === 2) {
223
- if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
224
- return moduleDictionary[parts[0]][parts[1]];
225
- }
226
- } else if (parts.length === 1) {
227
- if (moduleDictionary.data && moduleDictionary.data[key]) {
228
- return moduleDictionary.data[key];
229
- }
230
- }
231
- return `No dictionary:${key}`;
232
- }, [moduleDictionary]);
233
- const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
234
- return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
235
- value: {
236
- moduleDictionary,
237
- getLabel,
238
- getModuleLabel
239
- },
240
- children
241
- });
242
- }
243
21
  var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
244
22
  EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
245
23
  EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
246
24
  return EmitEvents2;
247
25
  })(EmitEvents || {});
26
+ function voidFunction() {
27
+ }
248
28
  function getPropertyByString(object, propString) {
249
29
  let value = object;
250
30
  const props = propString.split(".");
@@ -350,4 +130,4 @@ const axiosOperation = async (props, enviroment, hostTools) => {
350
130
  hostTools.stopProgress();
351
131
  });
352
132
  };
353
- export { EmitEvents, EnvironmentContext, EnvironmentProvider, FlagsContext, FlagsProvider, HostToolsContext, HostToolsProvider, ModuleDictionaryContext, ModuleDictionaryProvider, NetworkContext, NetworkProvider, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, useFlags, useFlagsPresent, useHostTools, useLocalStorage, useModuleDictionary, useNetwork, voidFunction };
133
+ export { EmitEvents, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, voidFunction };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.16",
4
+ "version": "0.0.19",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {
@@ -1,5 +0,0 @@
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 };
@@ -1,10 +0,0 @@
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
- }
@@ -1,6 +0,0 @@
1
- /// <reference types="react" />
2
- import { HostToolsType } from '../../types';
3
- import type { HostToolsProviderProps } from './types';
4
- declare const HostToolsContext: import("react").Context<HostToolsType>;
5
- declare function HostToolsProvider(props: HostToolsProviderProps): JSX.Element;
6
- export { HostToolsProvider, HostToolsContext };
@@ -1,6 +0,0 @@
1
- import { ReactNode } from 'react';
2
- import { HostToolsType } from '../../types';
3
- export interface HostToolsProviderProps extends HostToolsType {
4
- children: ReactNode;
5
- }
6
- export declare type HostToolsContextType = HostToolsType;
@@ -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
- /// <reference types="react" />
2
- import type { NetworkProviderProps } from './types';
3
- declare const NetworkContext: import("react").Context<import("./types").NetworkType>;
4
- declare function NetworkProvider(props: NetworkProviderProps): JSX.Element;
5
- export { NetworkProvider, NetworkContext };
@@ -1,11 +0,0 @@
1
- import { ReactNode } from 'react';
2
- import { AxiosOperation, NetworkProps } from '../../types';
3
- export declare type EventFunListener = (...args: any[]) => void;
4
- export interface NetworkType {
5
- networkOperation: (props: NetworkProps) => Promise<any>;
6
- }
7
- export interface NetworkProviderProps {
8
- axiosOperation: AxiosOperation;
9
- children: ReactNode;
10
- }
11
- export declare type NetworkContextType = NetworkType;
@@ -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,3 +0,0 @@
1
- import type { Flag, FlagsContextProps } from '../../contexts/FlagsContext/types';
2
- export declare const useFlags: () => FlagsContextProps;
3
- export declare const useFlagsPresent: (compareFlags: Array<Flag>) => boolean;
@@ -1,2 +0,0 @@
1
- export declare const useHostTools: () => import("../..").HostToolsType;
2
- export default useHostTools;
@@ -1 +0,0 @@
1
- export declare function useLocalStorage<ValueType>(key: string, initialValue: ValueType): any[];
@@ -1 +0,0 @@
1
- export declare const useModuleDictionary: () => import("../../contexts/ModuleDictionaryContext/types").ModuleDictionaryContextProps;
@@ -1,2 +0,0 @@
1
- export declare const useNetwork: () => import("../../contexts/NetworkContext/types").NetworkType;
2
- export default useNetwork;