Files
challenge-11/client/src/components/Dashboard.js
T

111 lines
3.2 KiB
JavaScript

import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import {
Container,
Grid,
Paper,
Typography,
Box,
} from '@mui/material';
import DirectionsCarIcon from '@mui/icons-material/DirectionsCar';
import PeopleIcon from '@mui/icons-material/People';
import ChatIcon from '@mui/icons-material/Chat';
import AdminPanelSettingsIcon from '@mui/icons-material/AdminPanelSettings';
import axios from 'axios';
const Dashboard = () => {
const navigate = useNavigate();
const [isAdmin, setIsAdmin] = useState(false);
useEffect(() => {
const checkAdminStatus = async () => {
try {
const response = await axios.get('http://localhost:5000/api/users/me', {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
},
});
setIsAdmin(response.data.role === 'admin');
} catch (error) {
console.error('Error checking admin status:', error);
}
};
checkAdminStatus();
}, []);
const menuItems = [
{
title: 'Customers',
icon: <PeopleIcon sx={{ fontSize: 40 }} />,
description: 'View and manage customer information',
path: '/customers',
},
{
title: 'Car Modifications',
icon: <DirectionsCarIcon sx={{ fontSize: 40 }} />,
description: 'Browse available car modifications',
path: '/modifications',
},
{
title: 'Contact History',
icon: <ChatIcon sx={{ fontSize: 40 }} />,
description: 'View customer interaction history',
path: '/contacts',
},
...(isAdmin ? [{
title: 'User Management',
icon: <AdminPanelSettingsIcon sx={{ fontSize: 40 }} />,
description: 'Manage system users and permissions',
path: '/users',
}] : []),
];
return (
<Container maxWidth="lg" sx={{ mt: 4, mb: 4 }}>
<Typography variant="h4" component="h1" gutterBottom sx={{ fontWeight: 'bold' }}>
Dashboard
</Typography>
<Grid container spacing={3}>
{menuItems.map((item) => (
<Grid item xs={12} md={4} key={item.title}>
<Paper
sx={{
p: 3,
display: 'flex',
flexDirection: 'column',
height: 240,
cursor: 'pointer',
transition: 'transform 0.2s',
'&:hover': {
transform: 'scale(1.02)',
},
}}
onClick={() => navigate(item.path)}
>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
flexGrow: 1,
}}
>
{item.icon}
<Typography variant="h6" component="h2" sx={{ mt: 2, fontWeight: 'bold' }}>
{item.title}
</Typography>
<Typography variant="body2" color="text.secondary" sx={{ mt: 1, textAlign: 'center' }}>
{item.description}
</Typography>
</Box>
</Paper>
</Grid>
))}
</Grid>
</Container>
);
};
export default Dashboard;