@buerokratt-ria/menu 0.1.16 → 0.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buerokratt-ria/menu",
3
- "version": "0.1.16",
3
+ "version": "0.2.0",
4
4
  "description": "Generic MainNavigation component that would be injected as dependency.",
5
5
  "main": "index.js",
6
6
  "scripts": {},
@@ -15,9 +15,22 @@ interface MenuTreeProps {
15
15
  handleNavToggle: (event: MouseEvent) => void;
16
16
  }
17
17
 
18
+ interface MenuItemLabelProps {
19
+ menuItem: MenuItem
20
+ currentlySelectedLanguage: string
21
+ }
22
+
23
+ const MenuItemLabel: React.FC<MenuItemLabelProps> = ({ menuItem, currentlySelectedLanguage }) => {
24
+ return (
25
+ <>
26
+ {menuItem.label[currentlySelectedLanguage]} {menuItem.count != null ? `(${menuItem.count})` : ''}
27
+ </>
28
+ );
29
+ };
30
+
18
31
  const MenuTree: FC<MenuTreeProps> = ({
19
- menuItems,
20
- serviceId,
32
+ menuItems,
33
+ serviceId,
21
34
  handleNavToggle,
22
35
  }) => {
23
36
  const currentlySelectedLanguage = useTranslation().i18n.language;
@@ -50,10 +63,10 @@ const MenuTree: FC<MenuTreeProps> = ({
50
63
  ) : (
51
64
  serviceId.includes(menuItem.id!)
52
65
  ? <NavLink to={menuItem.path || '#'}>
53
- {menuItem.label[currentlySelectedLanguage]}
66
+ <MenuItemLabel menuItem={menuItem} currentlySelectedLanguage={currentlySelectedLanguage} />
54
67
  </NavLink>
55
68
  : <a href={(menuData.find(dataItem => dataItem.id === menuItem.id)?.url ?? '') + menuItem.path}>
56
- {menuItem.label[currentlySelectedLanguage]}
69
+ <MenuItemLabel menuItem={menuItem} currentlySelectedLanguage={currentlySelectedLanguage} />
57
70
  </a>
58
71
  )}
59
72
  </li>
@@ -1,32 +1,38 @@
1
- import { useState } from "react";
1
+ import { useEffect, useState } from "react";
2
2
  import { useQuery } from "@tanstack/react-query";
3
3
  import { MenuItem } from "../types/menuItem";
4
4
  import useMenuItems from "./useMenuItems";
5
+ import { CountConf } from "../types/countConf";
5
6
 
6
- const useFilteredMenuItems = () => {
7
- const items = useMenuItems();
7
+ const useFilteredMenuItems = (countConf?: CountConf) => {
8
+ const items = useMenuItems(countConf);
8
9
  const [menuItems, setMenuItems] = useState<MenuItem[]>([]);
9
10
 
10
- useQuery({
11
+ const { data } = useQuery<{ response: [] }>({
11
12
  queryKey: ['accounts/user-role', 'prod'],
12
- onSuccess: (res: any) => {
13
- const filteredItems = items.filter((item) => {
14
- const role = res.response;
15
- if (role.includes('ROLE_ADMINISTRATOR'))
16
- return item.id;
17
- if (role.includes('ROLE_SERVICE_MANAGER'))
18
- return item.id != "settings" && item.id != "training";
19
- if (role.includes('ROLE_CUSTOMER_SUPPORT_AGENT'))
20
- return item.id != "settings" && item.id != "analytics";
21
- if (role.includes('ROLE_CHATBOT_TRAINER'))
22
- return item.id != "settings" && item.id != "conversations";
23
- if (role.includes('ROLE_ANALYST'))
24
- return item.id == "analytics" || item.id == "monitoring";
25
- });
26
- setMenuItems(filteredItems ?? []);
27
- },
28
13
  });
29
14
 
15
+ useEffect(() => {
16
+ const filteredItems = items.filter((item) => {
17
+ if (!data) {
18
+ return;
19
+ }
20
+ const role: any[] = data.response;
21
+ if (role.includes('ROLE_ADMINISTRATOR'))
22
+ return item.id;
23
+ if (role.includes('ROLE_SERVICE_MANAGER'))
24
+ return item.id != "settings" && item.id != "training";
25
+ if (role.includes('ROLE_CUSTOMER_SUPPORT_AGENT'))
26
+ return item.id != "settings" && item.id != "analytics";
27
+ if (role.includes('ROLE_CHATBOT_TRAINER'))
28
+ return item.id != "settings" && item.id != "conversations";
29
+ if (role.includes('ROLE_ANALYST'))
30
+ return item.id == "analytics" || item.id == "monitoring";
31
+ });
32
+
33
+ setMenuItems(filteredItems ?? []);
34
+ }, [items, data]);
35
+
30
36
  return menuItems;
31
37
  }
32
38
 
@@ -2,8 +2,9 @@ import { useMemo, useState } from "react";
2
2
  import { useQuery } from "@tanstack/react-query";
3
3
  import menuStructure from '../data/menu-structure.json';
4
4
  import { MenuItem } from "../types/menuItem";
5
+ import { CountConf } from "../types/countConf";
5
6
 
6
- const useMenuItems = () => {
7
+ const useMenuItems = (count?: CountConf) => {
7
8
  const externalMenuItems = import.meta.env.REACT_APP_MENU_JSON;
8
9
  const cachedMenu = getMenuCache();
9
10
  const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>(cachedMenu ?? []);
@@ -26,11 +27,11 @@ const useMenuItems = () => {
26
27
 
27
28
  const items = useMemo(() => {
28
29
  let externals;
29
-
30
+
30
31
  try {
31
- if(externalMenuItems) {
32
+ if (externalMenuItems) {
32
33
  externals = JSON.parse(externalMenuItems);
33
- if(!Array.isArray(externals)) {
34
+ if (!Array.isArray(externals)) {
34
35
  console.warn('REACT_APP_MENU_JSON was ignored becuase it wasn\'t an array');
35
36
  }
36
37
  }
@@ -38,15 +39,28 @@ const useMenuItems = () => {
38
39
  console.warn(e);
39
40
  }
40
41
 
41
- return externals ?? mainMenuItems ?? menuStructure ?? [];
42
- }, [externalMenuItems, mainMenuItems, menuStructure]);
43
-
42
+ const addCountToPathFields = (item: MenuItem) => {
43
+ if (count && item.path in count) {
44
+ item.count = count[item.path]
45
+ }
46
+
47
+ if (item.children) {
48
+ item.children.forEach(child => addCountToPathFields(child));
49
+ }
50
+ }
51
+
52
+ const finalMenu = externals ?? mainMenuItems ?? menuStructure ?? [];
53
+ finalMenu.forEach((item: MenuItem) => addCountToPathFields(item))
54
+
55
+ return finalMenu;
56
+ }, [externalMenuItems, mainMenuItems, menuStructure, count]);
57
+
44
58
  return items;
45
59
  }
46
60
 
47
61
  function getMenuCache(): any {
48
62
  const cached = getCache();
49
- if(Array.isArray(cached) && cached.length > 0)
63
+ if (Array.isArray(cached) && cached.length > 0)
50
64
  return cached;
51
65
  return null;
52
66
  }
@@ -6,10 +6,15 @@ import { useTranslation } from "react-i18next";
6
6
  import MenuTree from './components/menuTree';
7
7
  import useFilteredMenuItems from './hooks/useFilteredMenuItems';
8
8
  import './main-navigation.scss';
9
+ import { CountConf } from "./types/countConf";
9
10
 
10
- const MainNavigation: FC = () => {
11
+ interface MainNavigationProps {
12
+ countConf?: CountConf
13
+ }
14
+
15
+ const MainNavigation: FC<MainNavigationProps> = ({countConf}) => {
11
16
  const { t } = useTranslation();
12
- const menuItems = useFilteredMenuItems();
17
+ const menuItems = useFilteredMenuItems(countConf);
13
18
  const serviceId = useMemo(() => import.meta.env.REACT_APP_SERVICE_ID.split(','), []);
14
19
 
15
20
  const [navCollapsed, setNavCollapsed] = useState(false);
@@ -0,0 +1,3 @@
1
+ export interface CountConf {
2
+ [key: string]: number
3
+ }
@@ -10,4 +10,5 @@ export interface MenuItem {
10
10
  target?: '_blank' | '_self';
11
11
  children?: MenuItem[];
12
12
  hidden?: boolean;
13
+ count?: number
13
14
  }
@@ -16,6 +16,7 @@
16
16
  "skipLibCheck": true,
17
17
  "skipDefaultLibCheck": true,
18
18
  "baseUrl": ".",
19
+ "jsx": "react"
19
20
  },
20
21
  "exclude": ["node_modules", "tmp"]
21
22
  }
Binary file