Skip to content

Commit e6f3a4e

Browse files
committed
refactor(tsx): convert Admin component
1 parent 0ad3a5e commit e6f3a4e

File tree

2 files changed

+78
-55
lines changed

2 files changed

+78
-55
lines changed

src/ui/assets/jss/material-dashboard-react/layouts/dashboardStyle.js renamed to src/ui/assets/jss/material-dashboard-react/layouts/dashboardStyle.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { drawerWidth, transition, container } from '../../material-dashboard-react.js';
1+
import { Theme } from '@material-ui/core/styles/createTheme';
2+
import { drawerWidth, transition, container } from '../../material-dashboard-react';
23

3-
const appStyle = (theme) => ({
4+
interface AppStyleProps {
5+
wrapper: React.CSSProperties;
6+
mainPanel: React.CSSProperties & {
7+
[key: string]: any;
8+
};
9+
content: React.CSSProperties;
10+
container: typeof container;
11+
map: React.CSSProperties;
12+
}
13+
14+
const appStyle = (theme: Theme): AppStyleProps => ({
415
wrapper: {
516
position: 'relative',
617
top: '0',
@@ -16,14 +27,14 @@ const appStyle = (theme) => ({
1627
...transition,
1728
maxHeight: '100%',
1829
width: '100%',
19-
overflowScrolling: 'touch',
30+
WebkitOverflowScrolling: 'touch' as any,
2031
},
2132
content: {
2233
marginTop: '70px',
2334
padding: '30px 15px',
2435
minHeight: 'calc(100vh - 123px)',
2536
},
26-
container,
37+
container: { ...container },
2738
map: {
2839
marginTop: '70px',
2940
},
Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect, useRef } from 'react';
22
import { Routes, Route, Navigate, useParams } from 'react-router-dom';
33
import PerfectScrollbar from 'perfect-scrollbar';
44
import 'perfect-scrollbar/css/perfect-scrollbar.css';
@@ -12,78 +12,87 @@ import logo from '../assets/img/git-proxy.png';
1212
import { UserContext } from '../../context';
1313
import { getUser } from '../services/user';
1414

15-
let ps;
16-
let refresh = false;
15+
interface RouteType {
16+
layout: string;
17+
path: string;
18+
component: React.ComponentType<any>;
19+
}
1720

18-
const switchRoutes = (
19-
<Routes>
20-
{routes.map((prop, key) => {
21-
if (prop.layout === '/dashboard') {
22-
return <Route exact path={prop.path} element={<prop.component />} key={key} />;
23-
}
24-
return null;
25-
})}
26-
<Route exact path='/dashboard' element={<Navigate to='/dashboard/repo' />} />
27-
</Routes>
28-
);
21+
interface DashboardProps {
22+
[key: string]: any;
23+
}
2924

30-
const useStyles = makeStyles(styles);
25+
interface UserType {
26+
id?: string;
27+
name?: string;
28+
email?: string;
29+
}
30+
31+
let ps: PerfectScrollbar | undefined;
32+
let refresh = false;
3133

32-
export default function Dashboard({ ...rest }) {
33-
// styles
34+
const useStyles = makeStyles(styles as any);
35+
36+
const Dashboard: React.FC<DashboardProps> = ({ ...rest }) => {
3437
const classes = useStyles();
35-
// ref to help us initialize PerfectScrollbar on windows devices
36-
const mainPanel = React.createRef();
37-
// states and functions
38-
const [color] = React.useState('blue');
39-
const [mobileOpen, setMobileOpen] = React.useState(false);
40-
const [user, setUser] = useState({});
41-
42-
const handleDrawerToggle = () => {
43-
setMobileOpen(!mobileOpen);
44-
};
45-
const getRoute = () => {
46-
return window.location.pathname !== '/dashboard/maps';
47-
};
38+
const mainPanel = useRef<HTMLDivElement>(null);
39+
const [color] = useState<'purple' | 'blue' | 'green' | 'orange' | 'red'>('blue');
40+
const [mobileOpen, setMobileOpen] = useState<boolean>(false);
41+
const [user, setUser] = useState<UserType>({});
42+
const { id } = useParams<{ id?: string }>();
43+
44+
const handleDrawerToggle = () => setMobileOpen((prev) => !prev);
45+
const isMapRoute = () => window.location.pathname === '/dashboard/maps';
46+
4847
const resizeFunction = () => {
4948
if (window.innerWidth >= 960) {
5049
setMobileOpen(false);
5150
}
5251
};
53-
// initialize and destroy the PerfectScrollbar plugin
5452

55-
const { id } = useParams();
56-
React.useEffect(() => {
53+
const switchRoutes = (
54+
<Routes>
55+
{routes.map((prop: RouteType, key: number) => {
56+
if (prop.layout === '/dashboard') {
57+
const Component = prop.component;
58+
return <Route path={prop.path} element={<Component />} key={key} />;
59+
}
60+
return null;
61+
})}
62+
<Route path='/dashboard' element={<Navigate to='/dashboard/repo' />} />
63+
</Routes>
64+
);
65+
66+
useEffect(() => {
5767
async function loadUser() {
58-
if (navigator.platform.indexOf('Win') > -1) {
68+
if (navigator.platform.indexOf('Win') > -1 && mainPanel.current) {
5969
ps = new PerfectScrollbar(mainPanel.current, {
6070
suppressScrollX: true,
6171
suppressScrollY: false,
6272
});
6373
document.body.style.overflow = 'hidden';
64-
if (!refresh) {
65-
refresh = true;
66-
await getUser(null, setUser, null, null, null);
67-
}
6874
}
69-
window.addEventListener('resize', resizeFunction);
75+
7076
if (!refresh) {
7177
refresh = true;
72-
await getUser(null, setUser, null, null, null);
78+
await getUser(undefined, setUser);
7379
}
80+
81+
window.addEventListener('resize', resizeFunction);
7482
}
83+
7584
loadUser();
7685

77-
// Specify how to clean up after this effect:
78-
return function cleanup() {
86+
return () => {
7987
if (navigator.platform.indexOf('Win') > -1 && ps) {
8088
ps.destroy();
8189
}
8290
window.removeEventListener('resize', resizeFunction);
8391
};
8492
}, [id]);
93+
8594
return (
86-
<UserContext.Provider value={{ user, setUser }}>
95+
<UserContext.Provider value={{ user, setUser } as any}>
8796
<div className={classes.wrapper}>
8897
<Sidebar
8998
background='#1a1a1a'
@@ -96,17 +105,20 @@ export default function Dashboard({ ...rest }) {
96105
/>
97106
<div className={classes.mainPanel} ref={mainPanel}>
98107
<Navbar routes={routes} handleDrawerToggle={handleDrawerToggle} {...rest} />
99-
{/* On the /maps route we want the map to be on full screen - this is not possible if the content and conatiner classes are present because they have some paddings which would make the map smaller */}
100-
{getRoute() ? (
101-
<div className={classes.content}>
102-
<div className={classes.container}>{switchRoutes}</div>
103-
</div>
104-
) : (
108+
{isMapRoute() ? (
105109
<div className={classes.map}>{switchRoutes}</div>
110+
) : (
111+
<>
112+
<div className={classes.content}>
113+
<div className={classes.container}>{switchRoutes}</div>
114+
</div>
115+
<Footer />
116+
</>
106117
)}
107-
{getRoute() ? <Footer /> : null}
108118
</div>
109119
</div>
110120
</UserContext.Provider>
111121
);
112-
}
122+
};
123+
124+
export default Dashboard;

0 commit comments

Comments
 (0)