feat: add the delete confirmation dialog
This commit is contained in:
parent
70afbd4b45
commit
ee79740d8d
@ -5,6 +5,7 @@ import { DataGrid } from '@mui/x-data-grid';
|
|||||||
import { Typography, Button, Dialog, DialogTitle, DialogContent, IconButton, Box } from '@mui/material';
|
import { Typography, Button, Dialog, DialogTitle, DialogContent, IconButton, Box } from '@mui/material';
|
||||||
import AddOrEditProductForm from './AddOrEditProductForm.jsx';
|
import AddOrEditProductForm from './AddOrEditProductForm.jsx';
|
||||||
import EditIcon from '@mui/icons-material/Edit';
|
import EditIcon from '@mui/icons-material/Edit';
|
||||||
|
import DeleteIcon from '@mui/icons-material/Delete';
|
||||||
|
|
||||||
const columnsBase = [
|
const columnsBase = [
|
||||||
{ field: 'id', headerName: 'ID', width: 70 },
|
{ field: 'id', headerName: 'ID', width: 70 },
|
||||||
@ -28,6 +29,9 @@ export default function Admin({ children, maxWidth = 'lg', sx = {} }) {
|
|||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [editingProduct, setEditingProduct] = useState(null);
|
const [editingProduct, setEditingProduct] = useState(null);
|
||||||
|
|
||||||
|
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||||
|
const [rowToDelete, setRowToDelete] = useState(null);
|
||||||
|
|
||||||
const handleAddOrEditProduct = (product) => {
|
const handleAddOrEditProduct = (product) => {
|
||||||
if (editingProduct) {
|
if (editingProduct) {
|
||||||
// Update existing
|
// Update existing
|
||||||
@ -46,16 +50,32 @@ export default function Admin({ children, maxWidth = 'lg', sx = {} }) {
|
|||||||
setOpen(true);
|
setOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleDeleteClick = (row) => {
|
||||||
|
setRowToDelete(row);
|
||||||
|
setConfirmOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const confirmDelete = () => {
|
||||||
|
setRows(rows.filter((row) => row.id !== rowToDelete.id));
|
||||||
|
setRowToDelete(null);
|
||||||
|
setConfirmOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
...columnsBase,
|
...columnsBase,
|
||||||
{
|
{
|
||||||
field: 'actions',
|
field: 'actions',
|
||||||
headerName: 'Actions',
|
headerName: 'Actions',
|
||||||
width: 100,
|
width: 130,
|
||||||
renderCell: (params) => (
|
renderCell: (params) => (
|
||||||
<IconButton color="primary" onClick={() => handleEditClick(params)}>
|
<Box>
|
||||||
<EditIcon />
|
<IconButton color="primary" onClick={() => handleEditClick(params)}>
|
||||||
</IconButton>
|
<EditIcon />
|
||||||
|
</IconButton>
|
||||||
|
<IconButton color="error" onClick={() => handleDeleteClick(params.row)}>
|
||||||
|
<DeleteIcon />
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@ -73,6 +93,20 @@ export default function Admin({ children, maxWidth = 'lg', sx = {} }) {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|
||||||
|
<Dialog open={confirmOpen} onClose={() => setConfirmOpen(false)}>
|
||||||
|
<DialogTitle>Confirm Delete</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<Typography>
|
||||||
|
Are you sure you want to delete{' '}
|
||||||
|
<strong>{rowToDelete?.name}</strong>?
|
||||||
|
</Typography>
|
||||||
|
<Box mt={2} display="flex" justifyContent="flex-end" gap={1}>
|
||||||
|
<Button onClick={() => setConfirmOpen(false)}>Cancel</Button>
|
||||||
|
<Button variant="contained" color="error" onClick={confirmDelete}>Delete</Button>
|
||||||
|
</Box>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
<Box mt={2}>
|
<Box mt={2}>
|
||||||
<DataGrid
|
<DataGrid
|
||||||
rows={rows}
|
rows={rows}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user