From 46fb973203abc5ebf9f22ea8109d21d87a01b58f Mon Sep 17 00:00:00 2001 From: Steve White Date: Tue, 8 Oct 2024 16:06:18 -0500 Subject: [PATCH] Fixed item creation and display --- src/App.js | 2 +- src/components/Items.js | 128 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 116 insertions(+), 14 deletions(-) diff --git a/src/App.js b/src/App.js index 36a0e86..72ce054 100644 --- a/src/App.js +++ b/src/App.js @@ -17,7 +17,7 @@ function App() { } /> } /> } /> - } /> + } /> ); diff --git a/src/components/Items.js b/src/components/Items.js index 13c42f4..3033805 100644 --- a/src/components/Items.js +++ b/src/components/Items.js @@ -1,31 +1,133 @@ // src/components/Items.js -import React, { useEffect, useState } from 'react'; -import { Container, List, ListItem, ListItemText } from '@mui/material'; +import React, { useEffect, useState, useCallback } from 'react'; +import { + Container, + List, + ListItem, + ListItemText, + TextField, + Button, + IconButton, + Typography, // Import Typography for displaying image paths +} from '@mui/material'; +import { Delete as DeleteIcon } from '@mui/icons-material'; import axios from 'axios'; -import { useParams } from 'react-router-dom'; // Import useParams +import { useParams } from 'react-router-dom'; export default function Items({ token }) { - const { id: boxId } = useParams(); // Get boxId from URL parameters + const { id: boxId } = useParams(); const [items, setItems] = useState([]); - console.log("boxId is " + boxId) - useEffect(() => { + const [newItemName, setNewItemName] = useState(''); + const [newItemDescription, setNewItemDescription] = useState(''); + const [newItemImagePath, setNewItemImagePath] = useState(`/images/default.jpg`); + + // 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}` } + headers: { Authorization: `Bearer ${token}` } }).then(response => { - setItems(response.data); + console.log('Items:', response.data); + setItems(response.data); }); - }, [boxId, token]); + }, [boxId, token]); // Include dependencies for fetchItems + + 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 + }, + { + headers: { Authorization: `Bearer ${token}` } + } + ).then(response => { + setNewItemName(''); + setNewItemDescription(''); + setNewItemImagePath(''); // Clear the image path field + fetchItems(); // Refresh the item list after adding + }); + }; + + const handleDeleteItem = (itemId) => { + axios.delete(`${process.env.REACT_APP_API_URL}/items/${itemId}`, { + headers: { Authorization: `Bearer ${token}` } + }).then(() => { + fetchItems(); // Refresh the item list after deleting + }); + }; return ( +

Items in Box: {boxId}

+ + {/* Add Item Form */} + setNewItemName(e.target.value)} + /> + setNewItemDescription(e.target.value)} + /> + setNewItemImagePath(e.target.value)} + /> + + + {console.log("Items List:", items)} + + {/* Item List */} - {items.map((item) => ( - - + {items.map((item) => ( + console.log("Item ID:", item.ID), + handleDeleteItem(item.ID)}> + + + }> + + {item.description} + {item.image_path && ( // Conditionally render image path + Image: {item.image_path} + )} + + } + /> ))}
); -} \ No newline at end of file +}