From 4333cd5a7172605a6d7bb2c1e140e7dc6359bab1 Mon Sep 17 00:00:00 2001 From: Steve White Date: Tue, 8 Oct 2024 17:10:48 -0500 Subject: [PATCH] Modded to display box name instead of "Box Number"; also item details. --- src/components/Boxes.js | 2 +- src/components/ItemDetails.js | 54 ++++++++++++++++++++++++ src/components/Items.js | 78 +++++++++++++++++++---------------- 3 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 src/components/ItemDetails.js diff --git a/src/components/Boxes.js b/src/components/Boxes.js index 401e634..1c6e6b6 100644 --- a/src/components/Boxes.js +++ b/src/components/Boxes.js @@ -62,7 +62,7 @@ export default function Boxes({ token }) { }> {/* Use Link component */} + {/* Use Link component */} {box.name} } diff --git a/src/components/ItemDetails.js b/src/components/ItemDetails.js new file mode 100644 index 0000000..0c9b44b --- /dev/null +++ b/src/components/ItemDetails.js @@ -0,0 +1,54 @@ +// src/components/ItemDetails.js +import React, { useState } from 'react'; +import { TextField, Button, Container } from '@mui/material'; +import axios from 'axios'; + +export default function ItemDetails({ item, token, onSave }) { + const [name, setName] = useState(item.name); + const [description, setDescription] = useState(item.description); + const [imagePath, setImagePath] = useState(item.image_path || ''); + + const handleSave = () => { + axios.put(`${process.env.REACT_APP_API_URL}/items/${item.ID}`, + { name, description, image_path: imagePath }, + { + headers: { Authorization: `Bearer ${token}` } + } + ).then(() => { + onSave(); // Notify parent to refresh items + }); + }; + + return ( + +

Edit Item: {item.name}

+ setName(e.target.value)} + /> + setDescription(e.target.value)} + /> + setImagePath(e.target.value)} + /> + +
+ ); +} \ No newline at end of file diff --git a/src/components/Items.js b/src/components/Items.js index 3033805..adf1d24 100644 --- a/src/components/Items.js +++ b/src/components/Items.js @@ -1,4 +1,3 @@ -// src/components/Items.js import React, { useEffect, useState, useCallback } from 'react'; import { Container, @@ -8,57 +7,51 @@ import { TextField, Button, IconButton, - Typography, // Import Typography for displaying image paths + Typography, } from '@mui/material'; -import { Delete as DeleteIcon } from '@mui/icons-material'; +import { Delete as DeleteIcon, Edit as EditIcon } from '@mui/icons-material'; import axios from 'axios'; -import { useParams } from 'react-router-dom'; - +import { useParams, useLocation } from 'react-router-dom'; +import ItemDetails from './ItemDetails'; // Import the new ItemDetails component export default function Items({ token }) { const { id: boxId } = useParams(); const [items, setItems] = useState([]); const [newItemName, setNewItemName] = useState(''); const [newItemDescription, setNewItemDescription] = useState(''); - const [newItemImagePath, setNewItemImagePath] = useState(`/images/default.jpg`); + const [newItemImagePath, setNewItemImagePath] = useState('/images/default.jpg'); + const [editingItem, setEditingItem] = useState(null); // Track which item is being edited + const location = useLocation(); + const boxName = location.state?.boxName || 'Unknown Box'; + - // Use useCallback to memoize fetchItems const fetchItems = useCallback(() => { - console.log('Fetching items for box:', boxId); axios.get(`${process.env.REACT_APP_API_URL}/boxes/${boxId}/items`, { headers: { Authorization: `Bearer ${token}` } }).then(response => { - console.log('Items:', response.data); setItems(response.data); }); - }, [boxId, token]); // Include dependencies for fetchItems + }, [boxId, token]); useEffect(() => { fetchItems(); }, [boxId, token, fetchItems]); const handleAddItem = () => { - console.log('Adding item to box:', boxId); - console.log('Item Name:', newItemName); - console.log('Item Description:', newItemDescription); - console.log('Item Image Path:', newItemImagePath); - console.log('Token:', token); - axios.post(`${process.env.REACT_APP_API_URL}/items`, { name: newItemName, description: newItemDescription, - box_id: parseInt(boxId, 10), // Assuming your API needs box_id to associate the item - //image_path: newItemImagePath // Include the image path in the request + box_id: parseInt(boxId, 10), }, { headers: { Authorization: `Bearer ${token}` } } - ).then(response => { + ).then(() => { setNewItemName(''); setNewItemDescription(''); - setNewItemImagePath(''); // Clear the image path field - fetchItems(); // Refresh the item list after adding + setNewItemImagePath(''); + fetchItems(); }); }; @@ -66,13 +59,22 @@ export default function Items({ token }) { axios.delete(`${process.env.REACT_APP_API_URL}/items/${itemId}`, { headers: { Authorization: `Bearer ${token}` } }).then(() => { - fetchItems(); // Refresh the item list after deleting + fetchItems(); }); }; + const handleEditItem = (item) => { + setEditingItem(item); // Set the item to be edited + }; + + const handleSaveEdit = () => { + setEditingItem(null); // Clear the editing state after saving + fetchItems(); // Refresh the list after edit + }; + return ( -

Items in Box: {boxId}

+

Items in Box: {boxName}

{/* Add Item Form */} - {console.log("Items List:", items)} - - {/* Item List */} - - {items.map((item) => ( - console.log("Item ID:", item.ID), + {/* Conditionally render the ItemDetails component if editingItem is not null */} + {editingItem ? ( + + ) : ( + + {items.map((item) => ( handleDeleteItem(item.ID)}> - - + <> + handleEditItem(item)}> + + + handleDeleteItem(item.ID)}> + + + }> {item.description} - {item.image_path && ( // Conditionally render image path + {item.image_path && ( Image: {item.image_path} )} @@ -127,7 +134,8 @@ export default function Items({ token }) { /> ))} - + + )}
); -} +} \ No newline at end of file