@buerokratt-ria/menu 0.1.13 → 0.1.15
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/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -34,35 +34,14 @@ To publish created package:
|
|
|
34
34
|
* `import { MainNavigation } from '@buerokratt-ria/menu/src'` for Header and Menu
|
|
35
35
|
* If you want to use local package, put created package to the root of react app and add dependency like "@buerokratt-ria/header": "file:buerokratt-ria-menu-0.0.5.tgz" (use proper version)
|
|
36
36
|
### Using MainNavigation component
|
|
37
|
-
*
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
setMainMenuItems(res);
|
|
46
|
-
localStorage.setItem(CACHE_NAME, JSON.stringify(res));
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.log(e);
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
onError: (error: any) => {
|
|
52
|
-
setMainMenuItems(getCache());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
function getCache(): any {
|
|
58
|
-
const cache = localStorage.getItem(CACHE_NAME) || '{}';
|
|
59
|
-
return JSON.parse(cache);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
* Then pass this MainMenu items to menu component `<MainNavigation serviceId={import.meta.env.REACT_APP_SERVICE_ID.split(',')} items={MainMenuItems}/>`
|
|
63
|
-
* If you want to use only local file provided by package then pass empty array instead `[]`
|
|
64
|
-
* REACT_APP_SERVICE_ID set of values seperated by comma resembling current module, examples of this value could
|
|
65
|
-
be found in following links
|
|
37
|
+
* MainNavigation uses four env variable:
|
|
38
|
+
* use `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH` to set the url for json that contains the menu item array
|
|
39
|
+
* use `REACT_APP_MENU_JSON` with json string of the menu item array
|
|
40
|
+
* `REACT_APP_MENU_JSON` will overrides buth the local file provieded by package and the `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH`
|
|
41
|
+
* If you want to use only local file provided by package then remove `REACT_APP_MENU_JSON`, `REACT_APP_MENU_URL` & `REACT_APP_MENU_PATH` variables
|
|
42
|
+
* `REACT_APP_SERVICE_ID` set of values seperated by comma resembling current module, examples of this value could be found in following links
|
|
43
|
+
|
|
44
|
+
|
|
66
45
|
|
|
67
46
|
### Implemented examples:
|
|
68
47
|
* https://github.com/buerokratt/Training-Module
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -22,7 +22,9 @@ const MenuTree: FC<MenuTreeProps> = ({
|
|
|
22
22
|
}) => {
|
|
23
23
|
const currentlySelectedLanguage = useTranslation().i18n.language;
|
|
24
24
|
|
|
25
|
-
return menuItems
|
|
25
|
+
return menuItems
|
|
26
|
+
.filter(x => !x.hidden)
|
|
27
|
+
.map((menuItem) => (
|
|
26
28
|
<li key={menuItem.label[currentlySelectedLanguage]}>
|
|
27
29
|
{!!menuItem.children ? (
|
|
28
30
|
<>
|
|
@@ -5,27 +5,22 @@ import { MenuItem } from "../types/menuItem";
|
|
|
5
5
|
|
|
6
6
|
const useMenuItems = () => {
|
|
7
7
|
const externalMenuItems = import.meta.env.REACT_APP_MENU_JSON;
|
|
8
|
-
|
|
9
|
-
const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>([]);
|
|
8
|
+
const cachedMenu = getMenuCache();
|
|
9
|
+
const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>(cachedMenu ?? []);
|
|
10
10
|
|
|
11
11
|
useQuery({
|
|
12
|
-
enabled: !externalMenuItems,
|
|
12
|
+
enabled: !externalMenuItems && !cachedMenu,
|
|
13
13
|
queryKey: [import.meta.env.REACT_APP_MENU_URL + import.meta.env.REACT_APP_MENU_PATH],
|
|
14
14
|
onSuccess: (res: any) => {
|
|
15
15
|
try {
|
|
16
16
|
setMainMenuItems(res);
|
|
17
|
+
setCache(res);
|
|
17
18
|
} catch (e) {
|
|
18
|
-
setMainMenuItems(null);
|
|
19
19
|
console.error(e);
|
|
20
20
|
}
|
|
21
|
-
setCache(res);
|
|
22
21
|
},
|
|
23
22
|
onError: () => {
|
|
24
|
-
|
|
25
|
-
if(cached.length > 0)
|
|
26
|
-
setMainMenuItems(cached);
|
|
27
|
-
else
|
|
28
|
-
setMainMenuItems(null);
|
|
23
|
+
setMainMenuItems(cachedMenu);
|
|
29
24
|
},
|
|
30
25
|
});
|
|
31
26
|
|
|
@@ -33,21 +28,29 @@ const useMenuItems = () => {
|
|
|
33
28
|
let externals;
|
|
34
29
|
|
|
35
30
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
if(externalMenuItems) {
|
|
32
|
+
externals = JSON.parse(externalMenuItems);
|
|
33
|
+
if(!Array.isArray(externals)) {
|
|
34
|
+
console.warn('REACT_APP_MENU_JSON was ignored becuase it wasn\'t an array');
|
|
35
|
+
}
|
|
39
36
|
}
|
|
40
37
|
} catch (e) {
|
|
41
|
-
console.
|
|
38
|
+
console.warn(e);
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
return allItems.filter(x => !x.hidden);
|
|
41
|
+
return externals ?? mainMenuItems ?? menuStructure ?? [];
|
|
46
42
|
}, [externalMenuItems, mainMenuItems, menuStructure]);
|
|
47
43
|
|
|
48
44
|
return items;
|
|
49
45
|
}
|
|
50
46
|
|
|
47
|
+
function getMenuCache(): any {
|
|
48
|
+
const cached = getCache();
|
|
49
|
+
if(Array.isArray(cached) && cached.length > 0)
|
|
50
|
+
return cached;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
51
54
|
function getCache(): any {
|
|
52
55
|
const cache = localStorage.getItem('mainmenu-cache') ?? '[]';
|
|
53
56
|
return JSON.parse(cache);
|
|
Binary file
|