@@ -25,17 +25,15 @@ import DrawerHeader from '../../components/DrawerHeader';
2525import { Cached , ChevronLeft , ChevronRight } from '@mui/icons-material' ;
2626import { useHomePageContext } from '../../app/HomePageContext' ;
2727import { isGuest , TeamType } from 'shared' ;
28- import { getAllTeamTypes } from '../../apis/team-types.api' ;
29- import ConstructionIcon from '@mui/icons-material/Construction' ;
30- import ElectricBoltIcon from '@mui/icons-material/ElectricBolt' ;
31- import CodeIcon from '@mui/icons-material/Code' ;
32- import WorkIcon from '@mui/icons-material/Work' ;
28+ import * as MuiIcons from '@mui/icons-material' ;
29+ import { useAllTeamTypes } from '../../hooks/team-types.hooks' ;
30+ import ErrorPage from '../../pages/ErrorPage' ;
3331import BarChartIcon from '@mui/icons-material/BarChart' ;
3432import { useCurrentUser } from '../../hooks/users.hooks' ;
3533import QueryStatsIcon from '@mui/icons-material/QueryStats' ;
3634import CurrencyExchangeIcon from '@mui/icons-material/CurrencyExchange' ;
3735import ShoppingCartIcon from '@mui/icons-material/ShoppingCart' ;
38- import { useState , useEffect } from 'react' ;
36+ import { useState } from 'react' ;
3937
4038interface SidebarProps {
4139 drawerOpen : boolean ;
@@ -49,36 +47,18 @@ const Sidebar = ({ drawerOpen, setDrawerOpen, moveContent, setMoveContent }: Sid
4947 const { onPNMHomePage, onOnboardingHomePage } = useHomePageContext ( ) ;
5048 const user = useCurrentUser ( ) ;
5149 const { onGuestHomePage } = useHomePageContext ( ) ;
52- const [ allTeams , setAllTeams ] = useState < LinkItem [ ] > ( [ ] ) ;
50+ const { isError : teamsError , error : teamsErrorMsg , data : teams } = useAllTeamTypes ( ) ;
5351
54- const getIcon = ( iconName : string ) => {
55- const icons : { [ key : string ] : React . ComponentType } = {
56- ConstructionIcon ,
57- CodeIcon ,
58- ElectricBoltIcon ,
59- WorkIcon
52+ const allTeams : LinkItem [ ] = ( teams ?? [ ] ) . map ( ( team : TeamType ) => {
53+ const IconComponent = MuiIcons [ ( team . iconName in MuiIcons ? team . iconName : 'Circle' ) as keyof typeof MuiIcons ] ;
54+ return {
55+ name : team . name ,
56+ icon : < IconComponent /> ,
57+ route : routes . TEAMS + '/' + team . teamTypeId
6058 } ;
61- const Icon = icons [ iconName ] ;
62- return Icon ? < Icon /> : undefined ;
63- } ;
64-
65- useEffect ( ( ) => {
66- getAllTeamTypes ( )
67- . then ( ( response ) => {
68- console . log ( 'All teams from API:' , response . data ) ;
69- setAllTeams (
70- response . data . map ( ( team : TeamType ) => ( {
71- name : team . name ,
72- icon : getIcon ( team . iconName ) ,
73- route : routes . TEAMS + '/' + team . teamTypeId
74- } ) )
75- ) ;
76- } )
77- . catch ( ( error ) => {
78- console . log ( "Teams couldn't load " + error ) ;
79- } ) ;
80- } , [ ] ) ;
59+ } ) ;
8160
61+ if ( teamsError ) return < ErrorPage error = { teamsErrorMsg } /> ;
8262 const memberLinkItems : LinkItem [ ] = [
8363 {
8464 name : 'Home' ,
0 commit comments