Let's modify the code to:
- Add "Create" buttons to both Products and Collections tabs.
- Open a modal on clicking the "Create" button, allowing users to create a new Product or Collection.
- Show the products within a Collection when a collection card is clicked.
- Add a title and/or photo URL to the collection card (depending on the data provided).
We'll need to create two new components: CreateProductModal and CreateCollectionModal. Also, we'll need to update the ProductTable.js, CollectionCards.js, and MainContent.js components.
CreateProductModal
In your src/components folder, create a new file CreateProductModal.js.
Fill CreateProductModal.js:
import React, { useState } from 'react';
function CreateProductModal({ onCreate, collections }) {
const [name, setName] = useState('');
const [collection, setCollection] = useState('');
const handleSubmit = () => {
onCreate(name, collection);
setName('');
setCollection('');
};
return (
<div className="modal">
<div className="modal-content">
<h3>Create Product</h3>
<input
type="text"
placeholder="Product Name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<label>Assign to Collection:</label>
<select
value={collection}
onChange={(e) => setCollection(e.target.value)}
>
<option value="">None</option>
{collections.map((col) => (
<option key={col.id} value={col.id}>
{col.name}
</option>
))}
</select>
<button onClick={handleSubmit}>Create</button>
</div>
</div>
);
}
export default CreateProductModal;
CreateCollectionModal
Create a new file called CreateCollectionModal.js in src/components.
Fill CreateCollectionModal.js:
import React, { useState } from 'react';
function CreateCollectionModal({ onCreate }) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [photoURL, setPhotoURL] = useState('');
const handleSubmit = () => {
onCreate(name, description, photoURL);
setName('');
setDescription('');
setPhotoURL('');
};
return (
<div className="modal">
<div className="modal-content">
<h3>Create Collection</h3>
<input
type="text"
placeholder="Collection Name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<input
type="text"
placeholder="Description"
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
<input
type="text"
placeholder="Photo URL (optional)"
value={photoURL}
onChange={(e) => setPhotoURL(e.target.value)}
/>
<button onClick={handleSubmit}>Create</button>
</div>
</div>
);
}
export default CreateCollectionModal;
Update the components
Update the ProductTable.js:
import React, { useState } from 'react';
import CreateProductModal from './CreateProductModal';
function ProductTable({ products, collections, onProductCreate }) {
const [showModal, setShowModal] = useState(false);
const handleCreateProduct = (name, collection) => {
onProductCreate(name, collection);
setShowModal(false);
};
return (
<div>
<button onClick={() => setShowModal(true)}>Create Product</button>
{showModal && (
<CreateProductModal
onCreate={handleCreateProduct}
collections={collections}
/>
)}
<table>
{/* ...same as before... */}
</table>
</div>
);
}
export default ProductTable;
Update the CollectionCards.js:
import React, { useState } from 'react';
import CreateCollectionModal from './CreateCollectionModal';
function CollectionCards({ collections, onCollectionCreate }) {
const [showModal, setShowModal] = useState(false);
const handleCreateCollection = (name, description, photoURL) => {
onCollectionCreate(name, description, photoURL);
setShowModal(false);
};
return (
<div>
<button onClick={() => setShowModal(true)}>Create Collection</button>
{showModal && (
<CreateCollectionModal onCreate={handleCreateCollection} />
)}
<div className="collection-cards">
{/* ...same as before... */}
</div>
</div>
);
}
export default CollectionCards;
Update MainContent.js:
import React, { useState } from 'react';
// ...other imports...
function MainContent() {
// ...same as before...
const [products, setProducts] = useState([
{ id: 1, name: 'Product 1', collection: null },
{ id: 2, name: 'Product 2', collection: 1 },
]);
const [collections, setCollections] = useState([
{ id: 1, name: 'Collection 1', description: 'Desc 1', photoURL: '' },
]);
const handleProductCreate = (name, collection) => {
const newProduct = {
id: products.length + 1,
name,
collection: collection ? parseInt(collection) : null,
};
setProducts([...products, newProduct]);
};
const handleCollectionCreate = (name, description, photoURL) => {
const newCollection = {
id: collections.length + 1,
name,
description,
photoURL,
};
setCollections([...collections, newCollection]);
};
// ...same as before...
return (
<div className="main-content">
{/* ...same as before... */}
{showProducts ? (
<ProductTable
products={products}
collections={collections}
onProductCreate={handleProductCreate}
/>
) : (
<CollectionCards
collections={collections}
onCollectionCreate={handleCollectionCreate}
/>
)}
</div>
);
}
export default MainContent;
- Add/update styles to
src/App.css:
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-content {
background: white;
padding: 30px;
border-radius: 5px;
}
Now, you should have a React page with the requested features. Clicking on "Create Product" or "Create Collection" will open a modal to create new items, and you have the ability to assign products to collections. The collection cards will display the title and/or attached photo based on the data provided.