From 70afbd4b4502a00c15347d696161f2d64b50a860 Mon Sep 17 00:00:00 2001 From: Rodolfo Ruiz Date: Wed, 4 Jun 2025 19:59:34 -0600 Subject: [PATCH] feat: adding the edit button and logic --- ...oductForm.jsx => AddOrEditProductForm.jsx} | 78 ++++++---------- src/private/Admin.jsx | 90 +++++++++++++------ 2 files changed, 89 insertions(+), 79 deletions(-) rename src/private/{AddProductForm.jsx => AddOrEditProductForm.jsx} (50%) diff --git a/src/private/AddProductForm.jsx b/src/private/AddOrEditProductForm.jsx similarity index 50% rename from src/private/AddProductForm.jsx rename to src/private/AddOrEditProductForm.jsx index ae25d96..f92b84f 100644 --- a/src/private/AddProductForm.jsx +++ b/src/private/AddOrEditProductForm.jsx @@ -1,60 +1,45 @@ -import React, { useState } from 'react'; -import { Box, Button, TextField, Typography, Grid } from '@mui/material'; +import React, { useState, useEffect } from 'react'; +import { Box, Button, TextField, Grid } from '@mui/material'; -export default function AddProductForm({ onAdd }) { +export default function AddOrEditProductForm({ onAdd, initialData }) { const [product, setProduct] = useState({ name: '', price: '', - supplier: '', + provider: '', stock: '', - category: '', + category: '' }); + useEffect(() => { + if (initialData) { + setProduct(initialData); + } else { + setProduct({ + name: '', + price: '', + provider: '', + stock: '', + category: '' + }); + } + }, [initialData]); + const handleChange = (e) => { 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) { onAdd(product); - setProduct({ name: '', price: '', provider: '', stock: '', category: '' }); } }; - const handleDelete = () => { - console.log('Deleting product'); - }; - - const handleUpdate = () => { - console.log('Updating product:', product); - }; - return ( - - - Add Products - - - - + @@ -103,17 +88,10 @@ export default function AddProductForm({ onAdd }) { onChange={handleChange} margin="normal" /> - - - - - ); diff --git a/src/private/Admin.jsx b/src/private/Admin.jsx index 48508f5..beeb516 100644 --- a/src/private/Admin.jsx +++ b/src/private/Admin.jsx @@ -2,10 +2,11 @@ import SectionContainer from '../components/SectionContainer'; import React, { useState } from 'react'; import { DataGrid } from '@mui/x-data-grid'; -import { Typography, Button, Dialog, DialogTitle, DialogContent } from '@mui/material'; -import AddProductForm from './AddProductForm'; +import { Typography, Button, Dialog, DialogTitle, DialogContent, IconButton, Box } from '@mui/material'; +import AddOrEditProductForm from './AddOrEditProductForm.jsx'; +import EditIcon from '@mui/icons-material/Edit'; -const columns = [ +const columnsBase = [ { field: 'id', headerName: 'ID', width: 70 }, { field: 'company', headerName: 'Company', flex: 1 }, { field: 'name', headerName: 'Name', flex: 1 }, @@ -17,44 +18,75 @@ const columns = [ export default function Admin({ children, maxWidth = 'lg', sx = {} }) { const [rows, setRows] = useState([ - { id: 1, company: 'Fendi casa', name: 'Product 1', price: 10.99, provider: 'Provider A', stock: 100, category: 'Home' }, - { id: 2, company: 'Fendi casa', name: 'Product 2', price: 20.0, provider: 'Provider B', stock: 50, category: 'Home' }, - { id: 3, company: 'Fendi casa', name: 'Product 3', price: 5.5, provider: 'Provider C', stock: 200, category: 'Home' }, - { id: 4, company: 'Fendi casa', name: 'Product 4', price: 15.75, provider: 'Provider D', stock: 30, category: 'Home' }, - { id: 5, company: 'Fendi casa', name: 'Product 5', price: 8.2, provider: 'Provider E', stock: 75, category: 'Home' } - ]); + { id: 1, company: 'Fendi casa', name: 'Product 1', price: 10.99, provider: 'Provider A', stock: 100, category: 'Home' }, + { id: 2, company: 'Fendi casa', name: 'Product 2', price: 20.0, provider: 'Provider B', stock: 50, category: 'Home' }, + { id: 3, company: 'Fendi casa', name: 'Product 3', price: 5.5, provider: 'Provider C', stock: 200, category: 'Home' }, + { id: 4, company: 'Fendi casa', name: 'Product 4', price: 15.75, provider: 'Provider D', stock: 30, category: 'Home' }, + { id: 5, company: 'Fendi casa', name: 'Product 5', price: 8.2, provider: 'Provider E', stock: 75, category: 'Home' } + ]); - const [open, setOpen] = useState(false); + const [open, setOpen] = useState(false); + const [editingProduct, setEditingProduct] = useState(null); - const handleAddProduct = (newProduct) => { - const id = rows.length + 1; - setRows([...rows, { id, company: 'Fendi casa', ...newProduct }]); - setOpen(false); - }; + 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; + setRows([...rows, { id, company: 'Fendi casa', ...product }]); + } + setOpen(false); + setEditingProduct(null); + }; + + const handleEditClick = (params) => { + setEditingProduct(params.row); + setOpen(true); + }; + + const columns = [ + ...columnsBase, + { + field: 'actions', + headerName: 'Actions', + width: 100, + renderCell: (params) => ( + handleEditClick(params)}> + + + ) + } + ]; return ( - - + Product Catalog - - - setOpen(false)} maxWidth="sm" fullWidth> - Add Product + { setOpen(false); setEditingProduct(null); }} maxWidth="sm" fullWidth> + {editingProduct ? 'Edit Product' : 'Add Product'} - + - + + + + + + + ); } \ No newline at end of file