@buerokratt-ria/menu 0.1.15 → 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/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.16] - 10-06-2024
6
+
7
+ - Improve close menu functionality
8
+
5
9
  ## [0.1.15] - 10-06-2024
6
10
 
7
11
  - Fix a bug
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buerokratt-ria/menu",
3
- "version": "0.1.15",
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
  }
@@ -1,18 +1,20 @@
1
1
  import React, { FC, MouseEvent, useState, useMemo } from 'react';
2
2
  import clsx from 'clsx';
3
- import {
4
- MdKeyboardArrowRight,
5
- MdKeyboardArrowLeft,
6
- } from 'react-icons/md';
3
+ import { MdClose } from 'react-icons/md';
7
4
  import Icon from './components/icons/icon/icon';
8
5
  import { useTranslation } from "react-i18next";
9
6
  import MenuTree from './components/menuTree';
10
7
  import useFilteredMenuItems from './hooks/useFilteredMenuItems';
11
8
  import './main-navigation.scss';
9
+ import { CountConf } from "./types/countConf";
12
10
 
13
- const MainNavigation: FC = () => {
11
+ interface MainNavigationProps {
12
+ countConf?: CountConf
13
+ }
14
+
15
+ const MainNavigation: FC<MainNavigationProps> = ({countConf}) => {
14
16
  const { t } = useTranslation();
15
- const menuItems = useFilteredMenuItems();
17
+ const menuItems = useFilteredMenuItems(countConf);
16
18
  const serviceId = useMemo(() => import.meta.env.REACT_APP_SERVICE_ID.split(','), []);
17
19
 
18
20
  const [navCollapsed, setNavCollapsed] = useState(false);
@@ -23,17 +25,19 @@ const MainNavigation: FC = () => {
23
25
  event.currentTarget.setAttribute('aria-expanded', isExpanded ? 'false' : 'true');
24
26
  };
25
27
 
28
+ const handleCloseButtonClick = () => {
29
+ const doesMenuHasExpandedItem = !!document.querySelector('button[aria-expanded="true"]');
30
+ if(doesMenuHasExpandedItem)
31
+ setNavCollapsed(!navCollapsed);
32
+ }
33
+
26
34
  if (!menuItems) return null;
27
35
 
28
36
  return (
29
37
  <nav className={clsx('nav', { 'collapsed': navCollapsed })}>
30
- <button className='nav__menu-toggle close-button-item' onClick={() => setNavCollapsed(!navCollapsed)}>
31
- {
32
- navCollapsed
33
- ? <Icon icon={<MdKeyboardArrowRight className='menu-item-icon' />} size='large' />
34
- : <Icon icon={<MdKeyboardArrowLeft className='menu-item-icon' />} size='large' />
35
- }
36
- <span className='menu-item-title'>{t('mainMenu.closeMenu')}</span>
38
+ <button className='nav__menu-toggle close-button-item' onClick={handleCloseButtonClick}>
39
+ <Icon icon={<MdClose />} />
40
+ <span className='menu-item-title'>{t(navCollapsed ? 'mainMenu.openMenu' : 'mainMenu.closeMenu' )}</span>
37
41
  </button>
38
42
  <ul className='nav__menu'>
39
43
  <MenuTree
@@ -116,24 +116,14 @@
116
116
  }
117
117
 
118
118
  .collapsed {
119
- width: 64px;
120
-
121
- .menu-item-arrow,
122
- .menu-item-title {
123
- display: none !important;
124
- }
125
-
126
- .menu-item-icon {
127
- height: 24px;
128
- width: 24px;
129
- }
130
-
131
- .close-button-item,
132
- button.nav__toggle {
133
- justify-content: center;
119
+ .nav__submenu {
120
+ visibility: hidden;
121
+ height: 0;
134
122
  }
135
123
 
136
- .nav__submenu {
137
- display: none !important;
124
+ button[aria-expanded=true] {
125
+ .icon {
126
+ transform: rotate(0deg);
127
+ }
138
128
  }
139
129
  }
@@ -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