Files
Campus-Plug/frontend/src/components/ProductForm.jsx

301 lines
9.9 KiB
React
Raw Normal View History

2025-04-14 12:09:06 -06:00
import React, { useState } from "react";
2025-04-12 18:33:13 -06:00
const ProductForm = ({
editingProduct,
setEditingProduct,
onSave,
onCancel,
}) => {
2025-04-14 12:09:06 -06:00
const [selectedCategory, setSelectedCategory] = useState("");
const categories = [
"Electronics",
"Clothing",
"Home & Garden",
"Toys & Games",
"Books",
"Sports & Outdoors",
"Automotive",
"Beauty & Personal Care",
"Health & Wellness",
"Jewelry",
"Art & Collectibles",
"Food & Beverages",
"Office Supplies",
"Pet Supplies",
"Music & Instruments",
"Other",
];
const addCategory = () => {
if (
selectedCategory &&
!(editingProduct.categories || []).includes(selectedCategory)
) {
setEditingProduct((prev) => ({
...prev,
categories: [...(prev.categories || []), selectedCategory],
}));
setSelectedCategory("");
}
};
const removeCategory = (categoryToRemove) => {
setEditingProduct((prev) => ({
...prev,
categories: (prev.categories || []).filter(
(cat) => cat !== categoryToRemove,
),
}));
};
2025-04-12 18:33:13 -06:00
return (
2025-04-14 12:09:06 -06:00
<div className="bg-white border-2 border-gray-200 rounded-md p-6 shadow-lg">
2025-04-12 18:33:13 -06:00
{/* Back Button */}
<button
onClick={onCancel}
2025-04-14 12:09:06 -06:00
className="mb-4 text-sm text-emerald-600 hover:text-emerald-800 flex items-center font-medium"
2025-04-12 18:33:13 -06:00
>
Back to Listings
</button>
2025-04-14 12:09:06 -06:00
<h3 className="text-xl font-bold text-gray-800 mb-6 border-b-2 border-gray-100 pb-3">
2025-04-12 18:33:13 -06:00
{editingProduct?.id ? "Edit Your Product" : "List a New Product"}
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
{/* Product Name */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Product Name
</label>
<input
type="text"
value={editingProduct.name}
onChange={(e) =>
setEditingProduct({ ...editingProduct, name: e.target.value })
}
2025-04-14 12:09:06 -06:00
className="w-full px-4 py-2 border-2 border-gray-200 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
2025-04-12 18:33:13 -06:00
/>
</div>
{/* Price */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Price ($)
</label>
<input
type="number"
value={editingProduct.price}
onChange={(e) =>
setEditingProduct({
...editingProduct,
price: e.target.value,
})
}
2025-04-14 12:09:06 -06:00
className="w-full px-4 py-2 border-2 border-gray-200 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
2025-04-12 18:33:13 -06:00
/>
</div>
2025-04-14 12:09:06 -06:00
{/* Categories - Dropdown with Add button */}
<div className="md:col-span-2">
<label className="block text-sm font-medium text-gray-700 mb-2">
Categories
</label>
<div className="flex gap-2">
<select
value={selectedCategory}
onChange={(e) => setSelectedCategory(e.target.value)}
className="flex-1 px-4 py-2 border-2 border-gray-200 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
>
<option value="" disabled>
Select a category
</option>
{categories
.filter(
(cat) => !(editingProduct.categories || []).includes(cat),
)
.map((category) => (
<option key={category} value={category}>
{category}
</option>
))}
</select>
<button
type="button"
onClick={addCategory}
disabled={!selectedCategory}
className="px-4 py-2 bg-emerald-600 text-white rounded-md hover:bg-emerald-700 disabled:bg-gray-300 disabled:cursor-not-allowed"
>
Add
</button>
</div>
{/* Selected Categories */}
{(editingProduct.categories || []).length > 0 ? (
<div className="mt-3 flex flex-wrap gap-2">
{(editingProduct.categories || []).map((category) => (
<span
key={category}
className="inline-flex items-center px-3 py-1 rounded-md text-sm font-medium bg-emerald-100 text-emerald-800"
>
{category}
<button
type="button"
onClick={() => removeCategory(category)}
className="ml-2 text-emerald-600 hover:text-emerald-800"
>
×
</button>
</span>
))}
</div>
) : (
<p className="text-xs text-gray-500 mt-2">
Please select at least one category
</p>
)}
</div>
{/* Status - Updated to Unsold/Sold */}
2025-04-12 18:33:13 -06:00
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Status
</label>
<select
value={editingProduct.status}
onChange={(e) =>
setEditingProduct({
...editingProduct,
status: e.target.value,
})
}
2025-04-14 12:09:06 -06:00
className="w-full px-4 py-2 border-2 border-gray-200 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
2025-04-12 18:33:13 -06:00
>
2025-04-14 12:09:06 -06:00
<option value="Unsold">Unsold</option>
<option value="Sold">Sold</option>
2025-04-12 18:33:13 -06:00
</select>
</div>
2025-04-14 12:09:06 -06:00
{/* Description - New Field */}
2025-04-12 18:33:13 -06:00
<div className="md:col-span-2">
<label className="block text-sm font-medium text-gray-700 mb-2">
2025-04-14 12:09:06 -06:00
Description
2025-04-12 18:33:13 -06:00
</label>
2025-04-14 12:09:06 -06:00
<textarea
value={editingProduct.description || ""}
onChange={(e) =>
setEditingProduct({
...editingProduct,
description: e.target.value,
})
}
rows="4"
className="w-full px-4 py-2 border-2 border-gray-200 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500"
placeholder="Describe your product in detail..."
></textarea>
</div>
{/* Simplified Image Upload */}
<div className="md:col-span-2">
<label className="block text-sm font-medium text-gray-700 mb-2">
Product Images{" "}
<span className="text-gray-500 text-sm">(Max 5)</span>
</label>
{/* Simple file input */}
2025-04-12 18:33:13 -06:00
<input
type="file"
accept="image/*"
multiple
onChange={(e) => {
const files = Array.from(e.target.files).slice(0, 5);
setEditingProduct((prev) => ({
...prev,
images: [...prev.images, ...files].slice(0, 5),
}));
}}
2025-04-14 12:09:06 -06:00
className="hidden"
id="image-upload"
2025-04-12 18:33:13 -06:00
/>
2025-04-14 12:09:06 -06:00
<label
htmlFor="image-upload"
className="block w-full p-3 border-2 border-dashed border-emerald-200 bg-emerald-50 rounded-md text-center cursor-pointer hover:bg-emerald-100 transition-colors"
>
<span className="text-emerald-700 font-medium">
Click to upload images
</span>
</label>
2025-04-12 18:33:13 -06:00
2025-04-14 12:09:06 -06:00
{/* Image previews */}
{editingProduct.images.length > 0 && (
<div className="mt-4">
<p className="text-sm text-gray-600 mb-2">
{editingProduct.images.length}{" "}
{editingProduct.images.length === 1 ? "image" : "images"}{" "}
selected
</p>
<div className="flex flex-wrap gap-3">
{editingProduct.images.map((img, idx) => (
<div
key={idx}
className="relative w-20 h-20 border-2 border-gray-200 rounded-md overflow-hidden"
>
<img
src={URL.createObjectURL(img)}
alt={`Product image ${idx + 1}`}
className="w-full h-full object-cover"
/>
<button
onClick={() => {
const updated = [...editingProduct.images];
updated.splice(idx, 1);
setEditingProduct((prev) => ({
...prev,
images: updated,
}));
}}
className="absolute top-0 right-0 bg-white bg-opacity-80 w-6 h-6 flex items-center justify-center text-gray-700 hover:text-red-600"
title="Remove image"
>
&times;
</button>
</div>
))}
{editingProduct.images.length > 0 && (
2025-04-12 18:33:13 -06:00
<button
2025-04-14 12:09:06 -06:00
onClick={() =>
setEditingProduct((prev) => ({ ...prev, images: [] }))
}
className="text-sm text-red-600 hover:text-red-800 mt-2"
2025-04-12 18:33:13 -06:00
>
2025-04-14 12:09:06 -06:00
Clear all
2025-04-12 18:33:13 -06:00
</button>
2025-04-14 12:09:06 -06:00
)}
</div>
</div>
)}
2025-04-12 18:33:13 -06:00
</div>
</div>
{/* Actions */}
2025-04-14 12:09:06 -06:00
<div className="mt-8 flex justify-end gap-4 border-t-2 border-gray-100 pt-4">
2025-04-12 18:33:13 -06:00
<button
onClick={onCancel}
2025-04-14 12:09:06 -06:00
className="bg-gray-100 text-gray-700 px-6 py-2 rounded-md hover:bg-gray-200 font-medium"
2025-04-12 18:33:13 -06:00
>
Cancel
</button>
<button
onClick={onSave}
2025-04-14 12:09:06 -06:00
className="bg-emerald-600 text-white px-8 py-2 rounded-md hover:bg-emerald-700 font-medium"
2025-04-12 18:33:13 -06:00
>
{editingProduct.id ? "Update Product" : "Add Product"}
</button>
</div>
</div>
);
};
export default ProductForm;