@m4l/core 0.1.14 → 0.1.16

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,54 @@
1
+ import { createContext, useState, useEffect } from "react";
2
+ import { useEnvironment, useFlags, useNetwork } from "@m4l/core";
3
+ import { jsx } from "react/jsx-runtime";
4
+ const initialState = {
5
+ company_logo_small_url: "",
6
+ company_logo_normal_url: "",
7
+ name: ""
8
+ };
9
+ console.log("DomainContext createContext");
10
+ const DomainContext = createContext(initialState);
11
+ function DomainProvider(props) {
12
+ const {
13
+ children
14
+ } = props;
15
+ const {
16
+ domain_token
17
+ } = useEnvironment();
18
+ const {
19
+ addFlag
20
+ } = useFlags();
21
+ const [domain, setDomain] = useState({
22
+ company_logo_small_url: "",
23
+ company_logo_normal_url: "",
24
+ name: ""
25
+ });
26
+ const {
27
+ networkOperation
28
+ } = useNetwork();
29
+ useEffect(() => {
30
+ let mounted = true;
31
+ networkOperation({
32
+ method: "GET",
33
+ endPoint: `na/info/${domain_token}`
34
+ }).then((response) => {
35
+ if (mounted) {
36
+ setDomain({
37
+ ...response.data
38
+ });
39
+ addFlag("domain_loaded");
40
+ }
41
+ }).finally(() => {
42
+ });
43
+ return function cleanUp() {
44
+ mounted = false;
45
+ };
46
+ }, []);
47
+ return /* @__PURE__ */ jsx(DomainContext.Provider, {
48
+ value: {
49
+ ...domain
50
+ },
51
+ children
52
+ });
53
+ }
54
+ export { DomainContext as D, DomainProvider as a };
@@ -3,7 +3,6 @@ export interface Domain {
3
3
  name: string;
4
4
  company_logo_small_url: string;
5
5
  company_logo_normal_url: string;
6
- isLoaded?: boolean;
7
6
  }
8
7
  export interface DomainProviderProps {
9
8
  children: ReactNode;
@@ -6,6 +6,7 @@ const initialState = {
6
6
  clearFlags: voidFunction,
7
7
  addFlag: voidFunction
8
8
  };
9
+ console.log("FlagsContex createContext");
9
10
  const FlagsContext = createContext(initialState);
10
11
  function FlagsProvider({
11
12
  children
@@ -2,7 +2,7 @@ import { createContext, useState, useEffect, useCallback } from "react";
2
2
  import { u as useEnvironment } from "../../hooks/useEnvironment/index.js";
3
3
  import { u as useFlags } from "../../hooks/useFlags/index.js";
4
4
  import { u as useHostTools } from "../../hooks/useHostTools/index.js";
5
- import "../../index.js";
5
+ import "../DomainContext/index.js";
6
6
  import "../ModulePrivilegesContext/index.js";
7
7
  import "../ModuleSkeletonContext/index.js";
8
8
  import { u as useNetwork } from "../../hooks/useNetwork/index.js";
@@ -11,6 +11,7 @@ const initialState = {
11
11
  getLabel: () => "..",
12
12
  getModuleLabel: () => "No dictionary context"
13
13
  };
14
+ console.log("ModuleDictionaryContext createContext");
14
15
  const ModuleDictionaryContext = createContext(initialState);
15
16
  function ModuleDictionaryProvider(props) {
16
17
  const {
@@ -2,7 +2,7 @@ import { createContext, useState, useEffect, useCallback } from "react";
2
2
  import "../EnvironmentContext/index.js";
3
3
  import { u as useFlags } from "../../hooks/useFlags/index.js";
4
4
  import { u as useHostTools } from "../../hooks/useHostTools/index.js";
5
- import "../../index.js";
5
+ import "../DomainContext/index.js";
6
6
  import "../ModuleDictionaryContext/index.js";
7
7
  import "../ModuleSkeletonContext/index.js";
8
8
  import { u as useNetwork } from "../../hooks/useNetwork/index.js";
@@ -2,7 +2,7 @@ import { createContext } from "react";
2
2
  import "../EnvironmentContext/index.js";
3
3
  import { a as useFlagsPresent } from "../../hooks/useFlags/index.js";
4
4
  import "../HostToolsContext/index.js";
5
- import "../../index.js";
5
+ import "../DomainContext/index.js";
6
6
  import "../ModuleDictionaryContext/index.js";
7
7
  import "../ModulePrivilegesContext/index.js";
8
8
  import "../NetworkContext/index.js";
@@ -1,8 +1,8 @@
1
1
  export { EnvironmentContext, EnvironmentProvider } from './EnvironmentContext';
2
2
  export { HostToolsContext, HostToolsProvider } from './HostToolsContext';
3
3
  export { NetworkContext, NetworkProvider } from './NetworkContext';
4
- export { DomainContext, DomainProvider } from './DomainContext';
5
4
  export { FlagsContext, FlagsProvider } from './FlagsContext';
5
+ export { DomainContext, DomainProvider } from './DomainContext';
6
6
  export { ModuleDictionaryContext, ModuleDictionaryProvider } from './ModuleDictionaryContext';
7
7
  export { ModulePrivilegesContext, ModulePrivilegesProvider } from './ModulePrivilegesContext';
8
8
  export { ModuleSkeletonContext, ModuleSkeletonProvider } from './ModuleSkeletonContext';
package/hooks/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { useFlags, useFlagsPresent } from './useFlags';
3
3
  export { useHostTools } from './useHostTools';
4
4
  export { useDomain } from './useDomain';
5
5
  export { useLocalStorage } from './useLocalStorage';
6
+ export { useLocalStorageWithListener } from './useLocalStorageWithListener';
6
7
  export { useModuleDictionary } from './useModuleDictionary';
7
8
  export { useModulePrivileges } from './useModulePrivileges';
8
9
  export { useModuleSkeleton } from './useModuleSkeleton';
@@ -0,0 +1,9 @@
1
+ import { useContext } from "react";
2
+ import { D as DomainContext } from "../../contexts/DomainContext/index.js";
3
+ const useDomain = () => {
4
+ const context = useContext(DomainContext);
5
+ if (!context)
6
+ throw new Error("useDomain context must be use inside DomainContext");
7
+ return context;
8
+ };
9
+ export { useDomain as u };
@@ -1,4 +1,4 @@
1
- import { useState } from "react";
1
+ import { useState, useEffect } from "react";
2
2
  function useLocalStorage(key, initialValue) {
3
3
  const [value, setValue] = useState(() => {
4
4
  try {
@@ -18,4 +18,29 @@ function useLocalStorage(key, initialValue) {
18
18
  };
19
19
  return [value, setValueInLocalStorage];
20
20
  }
21
- export { useLocalStorage as u };
21
+ function useLocalStorageWithListener(key, defaultValue) {
22
+ const [value, setValue] = useState(() => {
23
+ const storedValue = localStorage.getItem(key);
24
+ return storedValue === null ? defaultValue : JSON.parse(storedValue);
25
+ });
26
+ useEffect(() => {
27
+ const listener = (e) => {
28
+ if (e.storageArea === localStorage && e.key === key) {
29
+ setValue(e.newValue ? JSON.parse(e.newValue) : e.newValue);
30
+ }
31
+ };
32
+ window.addEventListener("storage", listener);
33
+ return () => {
34
+ window.removeEventListener("storage", listener);
35
+ };
36
+ }, [key, defaultValue]);
37
+ const setValueInLocalStorage = (newValue) => {
38
+ setValue((currentValue) => {
39
+ const result = typeof newValue === "function" ? newValue(currentValue) : newValue;
40
+ localStorage.setItem(key, JSON.stringify(result));
41
+ return result;
42
+ });
43
+ };
44
+ return [value, setValueInLocalStorage];
45
+ }
46
+ export { useLocalStorageWithListener as a, useLocalStorage as u };
@@ -0,0 +1,2 @@
1
+ export declare function useLocalStorageWithListener<ValueType>(key: string, defaultValue: ValueType): any[];
2
+ export default useLocalStorageWithListener;
@@ -2,6 +2,7 @@ import { useContext } from "react";
2
2
  import { M as ModuleSkeletonContext } from "../../contexts/ModuleSkeletonContext/index.js";
3
3
  const useModuleSkeleton = () => {
4
4
  const context = useContext(ModuleSkeletonContext);
5
+ console.log("useModuleSkeleton", context);
5
6
  if (!context)
6
7
  throw new Error("useModuleSkeleton context must be use inside ModuleSkeletonContext");
7
8
  return context.isSkeleton;
package/index.js CHANGED
@@ -1,17 +1,17 @@
1
- import { createContext, useState, useEffect, useContext } from "react";
1
+ import "react";
2
2
  export { E as EnvironmentContext, a as EnvironmentProvider } from "./contexts/EnvironmentContext/index.js";
3
3
  export { F as FlagsContext, a as FlagsProvider } from "./contexts/FlagsContext/index.js";
4
4
  export { H as HostToolsContext, a as HostToolsProvider } from "./contexts/HostToolsContext/index.js";
5
+ export { D as DomainContext, a as DomainProvider } from "./contexts/DomainContext/index.js";
5
6
  export { M as ModuleDictionaryContext, a as ModuleDictionaryProvider } from "./contexts/ModuleDictionaryContext/index.js";
6
7
  export { M as ModulePrivilegesContext, a as ModulePrivilegesProvider } from "./contexts/ModulePrivilegesContext/index.js";
7
8
  export { M as ModuleSkeletonContext, a as ModuleSkeletonProvider } from "./contexts/ModuleSkeletonContext/index.js";
8
9
  export { N as NetworkContext, a as NetworkProvider } from "./contexts/NetworkContext/index.js";
9
- import { useEnvironment, useNetwork } from "@m4l/core";
10
- import { jsx } from "react/jsx-runtime";
11
10
  export { u as useEnvironment } from "./hooks/useEnvironment/index.js";
12
11
  export { u as useFlags, a as useFlagsPresent } from "./hooks/useFlags/index.js";
13
12
  export { u as useHostTools } from "./hooks/useHostTools/index.js";
14
- export { u as useLocalStorage } from "./hooks/useLocalStorage/index.js";
13
+ export { u as useDomain } from "./hooks/useDomain/index.js";
14
+ export { u as useLocalStorage, a as useLocalStorageWithListener } from "./hooks/useLocalStorage/index.js";
15
15
  export { u as useModuleDictionary } from "./hooks/useModuleDictionary/index.js";
16
16
  export { u as useModulePrivileges } from "./hooks/useModulePrivileges/index.js";
17
17
  export { u as useModuleSkeleton } from "./hooks/useModuleSkeleton/index.js";
@@ -20,64 +20,7 @@ export { i as initialPagerState, u as usePaginate } from "./hooks/usePaginate/in
20
20
  export { E as EmitEvents } from "./types/index.js";
21
21
  export { a as getLocalStorage, g as getPropertyByString, s as setLocalStorage, v as voidFunction } from "./utils/index.js";
22
22
  export { a as axiosOperation } from "./utils/axiosOperation.js";
23
- import "./index.js";
23
+ import "react/jsx-runtime";
24
+ import "@m4l/core";
24
25
  import "./external/axios.js";
25
26
  import "qs";
26
- const initialState = {
27
- company_logo_small_url: "",
28
- company_logo_normal_url: "",
29
- name: ""
30
- };
31
- const DomainContext = createContext(initialState);
32
- function DomainProvider(props) {
33
- const {
34
- children
35
- } = props;
36
- const {
37
- domain_token
38
- } = useEnvironment();
39
- const [domain, setDomain] = useState({
40
- company_logo_small_url: "",
41
- company_logo_normal_url: "",
42
- name: "",
43
- isLoaded: false
44
- });
45
- const {
46
- networkOperation
47
- } = useNetwork();
48
- useEffect(() => {
49
- let mounted = true;
50
- if (domain.isLoaded) {
51
- return;
52
- }
53
- networkOperation({
54
- method: "GET",
55
- endPoint: `na/info/${domain_token}`
56
- }).then((response) => {
57
- if (mounted) {
58
- setDomain({
59
- token: domain_token,
60
- isLoaded: true,
61
- ...response.data
62
- });
63
- }
64
- }).finally(() => {
65
- });
66
- return function cleanUp() {
67
- mounted = false;
68
- };
69
- }, []);
70
- return /* @__PURE__ */ jsx(DomainContext.Provider, {
71
- value: {
72
- ...domain
73
- },
74
- children
75
- });
76
- }
77
- const useDomain = () => {
78
- const context = useContext(DomainContext);
79
- if (!context)
80
- throw new Error("useDomain context must be use inside DomainContext");
81
- return context;
82
- };
83
- export { DomainContext, DomainProvider, useDomain };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "license": "UNLICENSED",
5
5
  "author": "M4L Team",
6
6
  "dependencies": {