feat: adding the edit button and logic
This commit is contained in:
		
							parent
							
								
									cef04888c6
								
							
						
					
					
						commit
						70afbd4b45
					
				| @ -1,60 +1,45 @@ | |||||||
| import React, { useState } from 'react'; | import React, { useState, useEffect } from 'react'; | ||||||
| import { Box, Button, TextField, Typography, Grid } from '@mui/material'; | import { Box, Button, TextField, Grid } from '@mui/material'; | ||||||
| 
 | 
 | ||||||
| export default function AddProductForm({ onAdd }) { | export default function AddOrEditProductForm({ onAdd, initialData }) { | ||||||
|   const [product, setProduct] = useState({ |   const [product, setProduct] = useState({ | ||||||
|     name: '', |     name: '', | ||||||
|     price: '', |     price: '', | ||||||
|     supplier: '', |     provider: '', | ||||||
|     stock: '', |     stock: '', | ||||||
|     category: '', |     category: '' | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|  |   useEffect(() => { | ||||||
|  |     if (initialData) { | ||||||
|  |       setProduct(initialData); | ||||||
|  |     } else { | ||||||
|  |       setProduct({ | ||||||
|  |         name: '', | ||||||
|  |         price: '', | ||||||
|  |         provider: '', | ||||||
|  |         stock: '', | ||||||
|  |         category: '' | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|  |   }, [initialData]); | ||||||
|  | 
 | ||||||
|   const handleChange = (e) => { |   const handleChange = (e) => { | ||||||
|     const { name, value } = e.target; |     const { name, value } = e.target; | ||||||
|     setProduct((prev) => ({ ...prev, [name]: value })); |     setProduct((prev) => ({ | ||||||
|  |       ...prev, | ||||||
|  |       [name]: name === 'price' || name === 'stock' ? Number(value) : value | ||||||
|  |     })); | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|  const handleSave = () => { |   const handleSubmit = () => { | ||||||
|     if (onAdd) { |     if (onAdd) { | ||||||
|       onAdd(product); |       onAdd(product); | ||||||
|       setProduct({ name: '', price: '', provider: '', stock: '', category: '' }); |  | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|   const handleDelete = () => { |  | ||||||
|     console.log('Deleting product'); |  | ||||||
|   }; |  | ||||||
| 
 |  | ||||||
|   const handleUpdate = () => { |  | ||||||
|     console.log('Updating product:', product); |  | ||||||
|   }; |  | ||||||
| 
 |  | ||||||
|   return ( |   return ( | ||||||
|     <Box |     <Box mt={2}> | ||||||
|       sx={{ |  | ||||||
|         maxWidth: 400, |  | ||||||
|         mx: 'auto', |  | ||||||
|         mt: 4, |  | ||||||
|         p: 3, |  | ||||||
|         borderRadius: 2, |  | ||||||
|         boxShadow: 3, |  | ||||||
|         bgcolor: 'white', |  | ||||||
|       }} |  | ||||||
|     > |  | ||||||
|       <Typography variant="h6" gutterBottom textAlign="center" color='black'> |  | ||||||
|         Add Products |  | ||||||
|       </Typography> |  | ||||||
| 
 |  | ||||||
|       <TextField |  | ||||||
|         fullWidth |  | ||||||
|         label="Company" |  | ||||||
|         name="company" |  | ||||||
|         value={product.company || ''} |  | ||||||
|         onChange={handleChange} |  | ||||||
|         margin="normal" |  | ||||||
|       /> |  | ||||||
| 
 |  | ||||||
|       <TextField |       <TextField | ||||||
|         fullWidth |         fullWidth | ||||||
|         label="Name" |         label="Name" | ||||||
| @ -78,7 +63,7 @@ export default function AddProductForm({ onAdd }) { | |||||||
|             fullWidth |             fullWidth | ||||||
|             label="Provider" |             label="Provider" | ||||||
|             name="provider" |             name="provider" | ||||||
|             value={product.provider || ''} |             value={product.provider} | ||||||
|             onChange={handleChange} |             onChange={handleChange} | ||||||
|             margin="normal" |             margin="normal" | ||||||
|           /> |           /> | ||||||
| @ -103,17 +88,10 @@ export default function AddProductForm({ onAdd }) { | |||||||
|         onChange={handleChange} |         onChange={handleChange} | ||||||
|         margin="normal" |         margin="normal" | ||||||
|       /> |       /> | ||||||
| 
 |       <Box display="flex" justifyContent="flex-end" mt={2}> | ||||||
|       <Box display="flex" justifyContent="space-between" mt={2}> |         <Button variant="contained" onClick={handleSubmit}> | ||||||
|         <Button variant="contained" color="primary" onClick={handleSave}> |  | ||||||
|           Save |           Save | ||||||
|         </Button> |         </Button> | ||||||
|         <Button variant="outlined" onClick={handleDelete}> |  | ||||||
|           Delete |  | ||||||
|         </Button> |  | ||||||
|         <Button variant="outlined" onClick={handleUpdate}> |  | ||||||
|           Update |  | ||||||
|         </Button> |  | ||||||
|       </Box> |       </Box> | ||||||
|     </Box> |     </Box> | ||||||
|   ); |   ); | ||||||
| @ -2,10 +2,11 @@ | |||||||
| import SectionContainer from '../components/SectionContainer'; | import SectionContainer from '../components/SectionContainer'; | ||||||
| import React, { useState } from 'react'; | import React, { useState } from 'react'; | ||||||
| import { DataGrid } from '@mui/x-data-grid'; | import { DataGrid } from '@mui/x-data-grid'; | ||||||
| import { Typography, Button, Dialog, DialogTitle, DialogContent } from '@mui/material'; | import { Typography, Button, Dialog, DialogTitle, DialogContent, IconButton, Box } from '@mui/material'; | ||||||
| import AddProductForm from './AddProductForm'; | import AddOrEditProductForm from './AddOrEditProductForm.jsx'; | ||||||
|  | import EditIcon from '@mui/icons-material/Edit'; | ||||||
| 
 | 
 | ||||||
| const columns = [ | const columnsBase = [ | ||||||
|     { field: 'id', headerName: 'ID', width: 70 }, |     { field: 'id', headerName: 'ID', width: 70 }, | ||||||
|     { field: 'company', headerName: 'Company', flex: 1 }, |     { field: 'company', headerName: 'Company', flex: 1 }, | ||||||
|     { field: 'name', headerName: 'Name', flex: 1 }, |     { field: 'name', headerName: 'Name', flex: 1 }, | ||||||
| @ -25,36 +26,67 @@ export default function Admin({ children, maxWidth = 'lg', sx = {} }) { | |||||||
|     ]); |     ]); | ||||||
| 
 | 
 | ||||||
|     const [open, setOpen] = useState(false); |     const [open, setOpen] = useState(false); | ||||||
|  |     const [editingProduct, setEditingProduct] = useState(null); | ||||||
| 
 | 
 | ||||||
|   const handleAddProduct = (newProduct) => { |     const handleAddOrEditProduct = (product) => { | ||||||
|  |         if (editingProduct) { | ||||||
|  |             // Update existing | ||||||
|  |             setRows(rows.map((row) => (row.id === editingProduct.id ? { ...editingProduct, ...product } : row))); | ||||||
|  |         } else { | ||||||
|  |             // Add new | ||||||
|             const id = rows.length + 1; |             const id = rows.length + 1; | ||||||
|     setRows([...rows, { id, company: 'Fendi casa', ...newProduct }]); |             setRows([...rows, { id, company: 'Fendi casa', ...product }]); | ||||||
|  |         } | ||||||
|         setOpen(false); |         setOpen(false); | ||||||
|  |         setEditingProduct(null); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     return ( |     const handleEditClick = (params) => { | ||||||
|  |         setEditingProduct(params.row); | ||||||
|  |         setOpen(true); | ||||||
|  |     }; | ||||||
| 
 | 
 | ||||||
|         <SectionContainer sx={{width: '100%' }}> |     const columns = [ | ||||||
|  |         ...columnsBase, | ||||||
|  |         { | ||||||
|  |             field: 'actions', | ||||||
|  |             headerName: 'Actions', | ||||||
|  |             width: 100, | ||||||
|  |             renderCell: (params) => ( | ||||||
|  |                 <IconButton color="primary" onClick={() => handleEditClick(params)}> | ||||||
|  |                     <EditIcon /> | ||||||
|  |                 </IconButton> | ||||||
|  |             ) | ||||||
|  |         } | ||||||
|  |     ]; | ||||||
|  | 
 | ||||||
|  |     return ( | ||||||
|  |         <SectionContainer sx={{ width: '100%' }}> | ||||||
|             <Typography variant="h6" gutterBottom> |             <Typography variant="h6" gutterBottom> | ||||||
|                 Product Catalog |                 Product Catalog | ||||||
|             </Typography> |             </Typography> | ||||||
| 
 | 
 | ||||||
|              <Button variant="contained" color="primary" onClick={() => setOpen(true)} sx={{ mb: 2 }}> |             <Dialog open={open} onClose={() => { setOpen(false); setEditingProduct(null); }} maxWidth="sm" fullWidth> | ||||||
|                 Add Product |                 <DialogTitle>{editingProduct ? 'Edit Product' : 'Add Product'}</DialogTitle> | ||||||
|             </Button> |  | ||||||
| 
 |  | ||||||
|             <Dialog open={open} onClose={() => setOpen(false)} maxWidth="sm" fullWidth> |  | ||||||
|                 <DialogTitle>Add Product</DialogTitle> |  | ||||||
|                 <DialogContent> |                 <DialogContent> | ||||||
|                 <AddProductForm onAdd={handleAddProduct} /> |                     <AddOrEditProductForm onAdd={handleAddOrEditProduct} initialData={editingProduct} /> | ||||||
|                 </DialogContent> |                 </DialogContent> | ||||||
|             </Dialog> |             </Dialog> | ||||||
| 
 | 
 | ||||||
|  |             <Box mt={2}> | ||||||
|                 <DataGrid |                 <DataGrid | ||||||
|                     rows={rows} |                     rows={rows} | ||||||
|                     columns={columns} |                     columns={columns} | ||||||
|                     pageSize={5} |                     pageSize={5} | ||||||
|                 rowsPerPageOptions={[5]} /> |                     rowsPerPageOptions={[5]} | ||||||
|  |                 /> | ||||||
|  | 
 | ||||||
|  |                 <Box display="flex" justifyContent="flex-end" mt={2}> | ||||||
|  |                     <Button variant="contained" color="primary" onClick={() => setOpen(true)}> | ||||||
|  |                         Add Product | ||||||
|  |                     </Button> | ||||||
|  |                 </Box> | ||||||
|  |             </Box> | ||||||
|         </SectionContainer> |         </SectionContainer> | ||||||
|     ); |     ); | ||||||
| } | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Rodolfo Ruiz
						Rodolfo Ruiz