More UI fixes to Admin.js

This commit is contained in:
Steve White 2024-10-17 09:04:28 -05:00
parent 88a90e39b7
commit 578d38f187
1 changed files with 115 additions and 51 deletions

View File

@ -4,6 +4,21 @@ import axios from 'axios';
import { Link, useNavigate } from 'react-router-dom'; import { Link, useNavigate } from 'react-router-dom';
import { PRIMARY_COLOR, SECONDARY_COLOR } from '../App'; import { PRIMARY_COLOR, SECONDARY_COLOR } from '../App';
import './Admin.css'; // Import the CSS file import './Admin.css'; // Import the CSS file
import {
Typography,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper,
Button,
Alert,
Container,
Box,
TextField
} from '@mui/material';
export default function Admin() { export default function Admin() {
@ -26,6 +41,19 @@ export default function Admin() {
}); });
}, []); }, []);
const fetchUsers = async () => {
try {
const response = await axios.get(`${process.env.REACT_APP_API_URL}/admin/user`, {
headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
});
setUsers(response.data);
} catch (error) {
console.error('Error fetching users:', error);
// Optionally, set an error message
// setErrorMessage('Failed to fetch users. Please try again.');
}
};
const handleCreateUser = async (e) => { const handleCreateUser = async (e) => {
e.preventDefault(); e.preventDefault();
try { try {
@ -49,6 +77,9 @@ export default function Admin() {
headers: { Authorization: `Bearer ${localStorage.getItem('token')}` } headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
}); });
setUsers(users.filter(user => user.id !== id)); setUsers(users.filter(user => user.id !== id));
//setUsers(prevUsers => prevUsers.filter(user => user.id !== id));
fetchUsers();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
@ -103,54 +134,87 @@ export default function Admin() {
}; };
return ( return (
<div> <Container maxWidth="md">
<h1>Admin</h1> <Typography variant="h4" gutterBottom>
<table> Admin
<thead> </Typography>
<tr>
<th>Username</th> <Box component="form" onSubmit={handleCreateUser} sx={{ mb: 4 }}>
<th>Actions</th> <Typography variant="h6" gutterBottom>
</tr> Add New User
</thead> </Typography>
<tbody> <TextField
label="Username"
variant="outlined"
value={username}
onChange={(e) => setUsername(e.target.value)}
sx={{ mr: 2 }}
/>
<TextField
label="Password"
type="password"
variant="outlined"
value={password}
onChange={(e) => setPassword(e.target.value)}
sx={{ mr: 2 }}
/>
<Button type="submit" sx={{ backgroundColor: PRIMARY_COLOR, borderBottom: '1px solid', borderColor: '#444', color: SECONDARY_COLOR }} variant="contained" color="primary">
Add User
</Button>
</Box>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Username</TableCell>
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{users.map(user => ( {users.map(user => (
<tr key={user.ID}> <TableRow key={user.ID}>
<td>{user.username}</td> <TableCell>{user.username}</TableCell>
<td> <TableCell>
<button onClick={() => handleDeleteUser(user.ID)}>Delete</button> <Button
</td> variant="contained"
</tr> color="error"
onClick={() => handleDeleteUser(user.ID)}
>
Delete User
</Button>
</TableCell>
</TableRow>
))} ))}
</tbody> </TableBody>
</table> </Table>
<form onSubmit={handleCreateUser}> </TableContainer>
<input type="text" value={username} onChange={(e) => setUsername(e.target.value)} placeholder="Username" />
<input type="password" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="Password" /> <Box sx={{ mt: 4 }}>
<button type="submit">Create User</button> <Button
</form> variant="contained"
<h2>Database</h2> color="primary"
<table> onClick={handleBackupDatabase}
<thead> sx={{ mr: 2, backgroundColor: PRIMARY_COLOR, borderBottom: '1px solid', borderColor: '#444', color: SECONDARY_COLOR }}
<tr> >
<th>Action</th> Backup Database
<th></th> </Button>
</tr>
</thead> <Button
<tbody> variant="contained"
<tr> color="secondary"
<td>Backup</td> component="label"
<td> sx={{ backgroundColor: PRIMARY_COLOR, borderBottom: '1px solid', borderColor: '#444', color: SECONDARY_COLOR }}
<button onClick={handleBackupDatabase}>Download Backup</button> >
</td> Restore Database
</tr> <input
<tr> type="file"
<td>Restore</td> hidden
<td> ref={fileInputRef}
<input type="file" ref={fileInputRef} /> onChange={handleRestoreDatabase}
<button onClick={handleRestoreDatabase}>Upload and Restore</button> />
</td> </Button>
</tr> </Box>
</tbody> </Container>
</table> );
</div> }
)};