@buerokratt-ria/menu 0.1.14 → 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
@@ -2,6 +2,10 @@
2
2
 
3
3
  All changes to this project will be documented in this file.
4
4
 
5
+ ## [0.1.15] - 10-06-2024
6
+
7
+ - Fix a bug
8
+
5
9
  ## [0.1.14] - 07-06-2024
6
10
 
7
11
  - Fix a bug
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
- * In Layout component you need to provide fetching and caching the `menu.json` file, code snippet would be:
38
- ```
39
- const CACHE_NAME = 'mainmenu-cache';
40
- const [MainMenuItems, setMainMenuItems] = useState([])
41
- const {data, isLoading, status} = useQuery({
42
- queryKey: [import.meta.env.REACT_APP_MENU_URL + import.meta.env.REACT_APP_MENU_PATH],
43
- onSuccess: (res: any) => {
44
- try {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buerokratt-ria/menu",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "Generic MainNavigation component that would be injected as dependency.",
5
5
  "main": "index.js",
6
6
  "scripts": {},
@@ -22,7 +22,9 @@ const MenuTree: FC<MenuTreeProps> = ({
22
22
  }) => {
23
23
  const currentlySelectedLanguage = useTranslation().i18n.language;
24
24
 
25
- return menuItems.map((menuItem) => (
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
- const cached = getCache();
25
- if(cached.length > 0)
26
- setMainMenuItems(cached);
27
- else
28
- setMainMenuItems(null);
23
+ setMainMenuItems(cachedMenu);
29
24
  },
30
25
  });
31
26
 
@@ -43,13 +38,19 @@ const useMenuItems = () => {
43
38
  console.warn(e);
44
39
  }
45
40
 
46
- const allItems = externals ?? mainMenuItems ?? menuStructure ?? [];
47
- return allItems.filter(x => !x.hidden);
41
+ return externals ?? mainMenuItems ?? menuStructure ?? [];
48
42
  }, [externalMenuItems, mainMenuItems, menuStructure]);
49
43
 
50
44
  return items;
51
45
  }
52
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
+
53
54
  function getCache(): any {
54
55
  const cache = localStorage.getItem('mainmenu-cache') ?? '[]';
55
56
  return JSON.parse(cache);
Binary file