Skip to content

Commit 0ad3a5e

Browse files
committed
refactor(tsx): convert NavBar components
1 parent f9ccd54 commit 0ad3a5e

File tree

4 files changed

+124
-49
lines changed

4 files changed

+124
-49
lines changed

src/ui/assets/jss/material-dashboard-react/components/headerLinksStyle.js renamed to src/ui/assets/jss/material-dashboard-react/components/headerLinksStyle.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
1-
import { defaultFont, dangerColor, whiteColor } from '../../material-dashboard-react.js';
1+
import { Theme } from '@material-ui/core/styles';
2+
import { defaultFont, dangerColor, whiteColor } from '../../material-dashboard-react';
3+
import dropdownStyle from '../dropdownStyle';
24

3-
// eslint-disable-next-line max-len
4-
import dropdownStyle from '../dropdownStyle.js';
5+
interface HeaderLinksStyle {
6+
[key: string]: any;
7+
search: {
8+
'& > div': {
9+
marginTop: string;
10+
};
11+
[themeBreakpoint: string]: any;
12+
};
13+
linkText: {
14+
zIndex: string;
15+
fontSize: string;
16+
margin: string;
17+
[key: string]: any;
18+
};
19+
buttonLink: {
20+
[themeBreakpoint: string]: any;
21+
};
22+
searchButton: {
23+
[themeBreakpoint: string]: any;
24+
};
25+
margin: {
26+
zIndex: string;
27+
margin: string;
28+
};
29+
searchIcon: {
30+
width: string;
31+
zIndex: string;
32+
};
33+
notifications: {
34+
zIndex: string;
35+
[themeBreakpoint: string]: any;
36+
};
37+
manager: {
38+
[themeBreakpoint: string]: any;
39+
display: string;
40+
};
41+
searchWrapper: {
42+
[themeBreakpoint: string]: any;
43+
display: string;
44+
};
45+
}
546

6-
const headerLinksStyle = (theme) => ({
47+
const headerLinksStyle = (theme: Theme): HeaderLinksStyle => ({
748
...dropdownStyle(theme),
849
search: {
950
'& > div': {

src/ui/assets/jss/material-dashboard-react/components/headerStyle.js renamed to src/ui/assets/jss/material-dashboard-react/components/headerStyle.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@ import {
88
warningColor,
99
dangerColor,
1010
whiteColor,
11-
} from '../../material-dashboard-react.js';
11+
} from '../../material-dashboard-react';
1212

13-
const headerStyle = () => ({
13+
interface StyleProps {
14+
appBar: React.CSSProperties;
15+
container: React.CSSProperties;
16+
flex: React.CSSProperties;
17+
title: React.CSSProperties & {
18+
'&:hover,&:focus': React.CSSProperties;
19+
};
20+
appResponsive: React.CSSProperties;
21+
primary: React.CSSProperties;
22+
info: React.CSSProperties;
23+
success: React.CSSProperties;
24+
warning: React.CSSProperties;
25+
danger: React.CSSProperties;
26+
}
27+
28+
const headerStyle = (): StyleProps => ({
1429
appBar: {
1530
backgroundColor: 'transparent',
1631
boxShadow: 'none',
@@ -78,4 +93,4 @@ const headerStyle = () => ({
7893
},
7994
});
8095

81-
export default headerStyle;
96+
export default headerStyle;

src/ui/components/Navbars/DashboardNavbarLinks.jsx renamed to src/ui/components/Navbars/DashboardNavbarLinks.tsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import classNames from 'classnames';
33
import { makeStyles } from '@material-ui/core/styles';
44
import MenuItem from '@material-ui/core/MenuItem';
@@ -7,7 +7,7 @@ import Grow from '@material-ui/core/Grow';
77
import Paper from '@material-ui/core/Paper';
88
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
99
import Hidden from '@material-ui/core/Hidden';
10-
import Poppers from '@material-ui/core/Popper';
10+
import Popper from '@material-ui/core/Popper';
1111
import Divider from '@material-ui/core/Divider';
1212
import Button from '../CustomButtons/Button';
1313
import styles from '../../assets/jss/material-dashboard-react/components/headerLinksStyle';
@@ -18,27 +18,33 @@ import axios from 'axios';
1818
import { getCookie } from '../../utils';
1919

2020
const useStyles = makeStyles(styles);
21+
interface UserData {
22+
id: string;
23+
name: string;
24+
email: string;
25+
}
2126

2227
export default function DashboardNavbarLinks() {
2328
const classes = useStyles();
2429
const navigate = useNavigate();
25-
const [openProfile, setOpenProfile] = React.useState(null);
26-
const [, setAuth] = React.useState(true);
27-
const [, setIsLoading] = React.useState(true);
28-
const [, setIsError] = React.useState(false);
29-
const [data, setData] = React.useState(false);
30+
const [openProfile, setOpenProfile] = useState<HTMLElement | null>(null);
31+
const [, setAuth] = useState<boolean>(true);
32+
const [, setIsLoading] = useState<boolean>(true);
33+
const [, setIsError] = useState<boolean>(false);
34+
const [data, setData] = useState<UserData | null>(null);
3035

3136
useEffect(() => {
3237
getUser(setIsLoading, setData, setAuth, setIsError);
3338
}, []);
3439

35-
const handleClickProfile = (event) => {
36-
if (openProfile && openProfile.contains(event.target)) {
40+
const handleClickProfile = (event: React.MouseEvent<HTMLElement>) => {
41+
if (openProfile && openProfile.contains(event.target as Node)) {
3742
setOpenProfile(null);
3843
} else {
3944
setOpenProfile(event.currentTarget);
4045
}
4146
};
47+
4248
const handleCloseProfile = () => {
4349
setOpenProfile(null);
4450
};
@@ -47,24 +53,26 @@ export default function DashboardNavbarLinks() {
4753
navigate('/dashboard/profile', { replace: true });
4854
};
4955

50-
const logout = () => {
51-
axios
52-
.post(
53-
`${import.meta.env.VITE_API_URI}/api/auth/logout`,
56+
const logout = async () => {
57+
try {
58+
const response = await axios.post(
59+
`${import.meta.env.VITE_API_URI || 'http://localhost:3000'}/api/auth/logout`,
5460
{},
5561
{
5662
withCredentials: true,
5763
headers: {
5864
'X-CSRF-TOKEN': getCookie('csrf'),
5965
},
6066
},
61-
)
62-
.then((res) => {
63-
if (!res.data.isAuth && !res.data.user) {
64-
setAuth(false);
65-
navigate(0);
66-
}
67-
});
67+
);
68+
69+
if (!response.data.isAuth && !response.data.user) {
70+
setAuth(false);
71+
navigate(0);
72+
}
73+
} catch (error) {
74+
console.error('Logout failed:', error);
75+
}
6876
};
6977

7078
return (
@@ -74,7 +82,7 @@ export default function DashboardNavbarLinks() {
7482
color={window.innerWidth > 959 ? 'transparent' : 'white'}
7583
justIcon={window.innerWidth > 959}
7684
simple={window.innerWidth <= 959}
77-
aria-owns={openProfile ? 'profile-menu-list-grow' : null}
85+
aria-owns={openProfile ? 'profile-menu-list-grow' : undefined}
7886
aria-haspopup='true'
7987
onClick={handleClickProfile}
8088
className={classes.buttonLink}
@@ -84,7 +92,7 @@ export default function DashboardNavbarLinks() {
8492
<p className={classes.linkText}>Profile</p>
8593
</Hidden>
8694
</Button>
87-
<Poppers
95+
<Popper
8896
open={Boolean(openProfile)}
8997
anchorEl={openProfile}
9098
transition
@@ -94,7 +102,6 @@ export default function DashboardNavbarLinks() {
94102
{({ TransitionProps, placement }) => (
95103
<Grow
96104
{...TransitionProps}
97-
id='profile-menu-list-grow'
98105
style={{
99106
transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom',
100107
}}
@@ -116,7 +123,7 @@ export default function DashboardNavbarLinks() {
116123
</Paper>
117124
</Grow>
118125
)}
119-
</Poppers>
126+
</Popper>
120127
</div>
121128
</div>
122129
);
Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import classNames from 'classnames';
3-
import PropTypes from 'prop-types';
43
import { makeStyles } from '@material-ui/core/styles';
54
import AppBar from '@material-ui/core/AppBar';
65
import Toolbar from '@material-ui/core/Toolbar';
@@ -12,30 +11,48 @@ import styles from '../../assets/jss/material-dashboard-react/components/headerS
1211

1312
const useStyles = makeStyles(styles);
1413

15-
export default function Header(props) {
14+
interface Route {
15+
component: any;
16+
icon: any;
17+
layout: string;
18+
name: string;
19+
rtlName?: string;
20+
path: string;
21+
visible: boolean;
22+
}
23+
24+
interface HeaderProps {
25+
color?: 'primary' | 'info' | 'success' | 'warning' | 'danger';
26+
rtlActive?: boolean;
27+
handleDrawerToggle: () => void;
28+
routes: Route[];
29+
}
30+
31+
const Header: React.FC<HeaderProps> = (props) => {
1632
const classes = useStyles();
17-
function makeBrand() {
18-
let name;
19-
props.routes.map((prop) => {
33+
34+
const makeBrand = (): string => {
35+
let name = '';
36+
props.routes.forEach((prop) => {
2037
if (window.location.href.indexOf(prop.layout + prop.path) !== -1) {
21-
name = props.rtlActive ? prop.rtlName : prop.name;
38+
name = props.rtlActive ? prop.rtlName || prop.name : prop.name;
2239
}
23-
return null;
2440
});
2541
return name;
26-
}
27-
const { color } = props;
42+
};
43+
44+
const { color = 'primary' } = props;
2845
const appBarClasses = classNames({
29-
[' ' + classes[color]]: color,
46+
[` ${classes[color]}`]: color,
3047
});
48+
3149
return (
3250
<AppBar style={{ borderRadius: '0px', zIndex: 10 }} className={classes.appBar + appBarClasses}>
3351
<Toolbar className={classes.container}>
3452
<div className={classes.flex}>
3553
{/* Here we create navbar brand, based on route name */}
3654
<h2
3755
style={{ marginLeft: '15px', fontSize: '19px', fontWeight: 400 }}
38-
color='transparent'
3956
className={classes.title}
4057
>
4158
{makeBrand()}
@@ -52,11 +69,6 @@ export default function Header(props) {
5269
</Toolbar>
5370
</AppBar>
5471
);
55-
}
56-
57-
Header.propTypes = {
58-
color: PropTypes.oneOf(['primary', 'info', 'success', 'warning', 'danger']),
59-
rtlActive: PropTypes.bool,
60-
handleDrawerToggle: PropTypes.func,
61-
routes: PropTypes.arrayOf(PropTypes.object),
6272
};
73+
74+
export default Header;

0 commit comments

Comments
 (0)