diff --git a/src/context/SellerContext.tsx b/src/context/SellerContext.tsx index d02b029..61f0a62 100644 --- a/src/context/SellerContext.tsx +++ b/src/context/SellerContext.tsx @@ -23,6 +23,7 @@ interface SellerContextType { fetchData: () => Promise; updateStock: (id: string, newStock: number) => Promise; addProduct: (product: Omit) => Promise; + updateProduct: (id: string, updates: Partial) => Promise; deleteProduct: (id: string) => Promise; // Orders & Shipping @@ -772,6 +773,54 @@ export const SellerProvider: React.FC<{ children: React.ReactNode }> = ({ childr } }; + const updateProduct = async (id: string, updates: Partial) => { + setInventory(prev => prev.map(item => { + if (item.id === id) { + const newStock = updates.stock !== undefined ? updates.stock : item.stock; + return { + ...item, + ...updates, + stock: newStock, + status: newStock === 0 ? 'out_of_stock' : newStock < 5 ? 'low_stock' : 'active' + }; + } + return item; + })); + + if (id && !String(id).startsWith('prod-')) { + try { + const payload: any = {}; + if (updates.title !== undefined) payload.title = updates.title; + if (updates.price !== undefined) payload.price = updates.price; + if (updates.mrp !== undefined) payload.compare_at_price = updates.mrp; + if (updates.stock !== undefined) payload.stock = updates.stock; + if (updates.description !== undefined) payload.description = updates.description; + if (updates.category !== undefined) payload.category = updates.category; + if (updates.image !== undefined) payload.image = updates.image; + if (updates.additional_images !== undefined) payload.additional_images = updates.additional_images; + if (updates.video_url !== undefined) payload.video_url = updates.video_url; + if (updates.is_gi_tagged !== undefined) payload.is_gi_tagged = updates.is_gi_tagged; + if (updates.gi_tag_name !== undefined) payload.gi_tag_name = updates.gi_tag_name; + if (updates.is_odop !== undefined) payload.is_odop = updates.is_odop; + if (updates.odop_district !== undefined) payload.odop_district = updates.odop_district; + if (updates.is_sustainable !== undefined) payload.is_sustainable = updates.is_sustainable; + if (updates.sustainable_type !== undefined) payload.sustainable_type = updates.sustainable_type; + if (updates.geographical_origin !== undefined) payload.geographical_origin = updates.geographical_origin; + if (updates.statutory_certifications !== undefined) payload.statutory_certifications = updates.statutory_certifications; + if (updates.provenance_story !== undefined) payload.provenance_story = updates.provenance_story; + if (updates.coordinates !== undefined) payload.coordinates = updates.coordinates; + if (updates.package_weight !== undefined) payload.package_weight = updates.package_weight; + if (updates.shipping_profile !== undefined) payload.shipping_profile = updates.shipping_profile; + + await api.patchProduct(id, payload); + } catch (err) { + console.warn('Failed to patch product on backend:', err); + } + } + + fetchData().catch(() => {}); + }; + const deleteProduct = async (id: string) => { try { await api.deleteProduct(id); @@ -946,6 +995,7 @@ export const SellerProvider: React.FC<{ children: React.ReactNode }> = ({ childr fetchData, updateStock, addProduct, + updateProduct, deleteProduct, orders, updateOrderStatus, diff --git a/src/pages/dashboard/AddProductPage.tsx b/src/pages/dashboard/AddProductPage.tsx index 332a051..362da9a 100644 --- a/src/pages/dashboard/AddProductPage.tsx +++ b/src/pages/dashboard/AddProductPage.tsx @@ -112,7 +112,7 @@ export const AddProductPage: React.FC = () => { } } }; - const { addProduct, fetchData, user, onboarding, inventory } = useSeller(); + const { addProduct, updateProduct, fetchData, user, onboarding, inventory } = useSeller(); const sellerMid = user?.profile?.merchant_id || user?.merchant_id || onboarding?.merchantId || (user?.id ? `TRDPX${String(user.id).padStart(6, '0')}` : 'TRDPX000001'); const nextProductSeq = (inventory?.length || 0) + 1; const navigate = useNavigate(); @@ -529,8 +529,7 @@ export const AddProductPage: React.FC = () => { }; if (editingId && !String(editingId).startsWith('prod-')) { - await api.patchProduct(editingId, productPayload); - await fetchData(); + await updateProduct(editingId, productPayload); } else { await addProduct(productPayload); } @@ -595,11 +594,18 @@ export const AddProductPage: React.FC = () => {
- Catalog & Inventory / Add Product + Catalog & Inventory / {editingId ? 'Edit Product' : 'Add Product'} +
+
+

+ {editingId ? 'Edit Handcrafted Product' : 'Add New Handcrafted Product'} +

+ {editingId && ( + + SKU: {sku} + + )}
-

- Add New Handcrafted Product -

{step === 1 && 'Step 1 of 4: Category'} {step === 2 && 'Step 2 of 4: Photos & Video Showcase'} @@ -1718,7 +1724,9 @@ export const AddProductPage: React.FC = () => { ) : ( )}

diff --git a/src/pages/dashboard/InventoryPage.tsx b/src/pages/dashboard/InventoryPage.tsx index 051f76b..c3801f6 100644 --- a/src/pages/dashboard/InventoryPage.tsx +++ b/src/pages/dashboard/InventoryPage.tsx @@ -17,7 +17,8 @@ import { Edit3, ShieldCheck, CheckCircle2, - FileSpreadsheet + FileSpreadsheet, + X } from 'lucide-react'; const HEALTH_CONFIG: Record = { @@ -38,7 +39,7 @@ const PAGE_SIZE = 10; export const InventoryPage: React.FC = () => { const navigate = useNavigate(); - const { inventory, updateStock, deleteProduct, fetchData } = useSeller(); + const { inventory, updateStock, updateProduct, deleteProduct, fetchData } = useSeller(); useEffect(() => { fetchData(); @@ -83,9 +84,20 @@ export const InventoryPage: React.FC = () => { const [stockValues, setStockValues] = useState>({}); const [currentPage, setCurrentPage] = useState(1); const [editingItem, setEditingItem] = useState(null); + const [editTitle, setEditTitle] = useState(''); const [editPrice, setEditPrice] = useState(''); + const [editMrp, setEditMrp] = useState(''); + const [editStock, setEditStock] = useState(''); const [isSavingEdit, setIsSavingEdit] = useState(false); + const handleOpenQuickEdit = (item: any) => { + setEditingItem(item); + setEditTitle(item.title || ''); + setEditPrice(item.price || ''); + setEditMrp(item.mrp || item.compare_at_price || ''); + setEditStock(stockValues[item.id] ?? item.stock ?? 0); + }; + useEffect(() => { setStockValues(prev => { const next = { ...prev }; @@ -154,19 +166,24 @@ export const InventoryPage: React.FC = () => { const handleSaveEdit = async () => { if (!editingItem) return; setIsSavingEdit(true); - const currentStock = stockValues[editingItem.id] ?? editingItem.stock; - const patchPayload: any = { stock: currentStock }; - if (editPrice !== '' && !isNaN(Number(editPrice))) { - patchPayload.price = Number(editPrice); - } + const updates: any = {}; + if (editTitle.trim()) updates.title = editTitle.trim(); + if (editPrice !== '' && !isNaN(Number(editPrice))) updates.price = Number(editPrice); + if (editMrp !== '' && !isNaN(Number(editMrp))) updates.mrp = Number(editMrp); + if (editStock !== '' && !isNaN(Number(editStock))) updates.stock = Math.max(0, Number(editStock)); + try { - if (editingItem.id && !String(editingItem.id).startsWith('prod-')) { - await api.patchProduct(editingItem.id, patchPayload); + if (updateProduct) { + await updateProduct(editingItem.id, updates); + } else if (editingItem.id && !String(editingItem.id).startsWith('prod-')) { + await api.patchProduct(editingItem.id, updates); + } + if (updates.stock !== undefined) { + setStockValues(prev => ({ ...prev, [editingItem.id]: updates.stock })); } - await updateStock(editingItem.id, currentStock); await fetchData(); } catch (err) { - console.warn('Failed to update product via quick edit drawer:', err); + console.warn('Failed to update product via quick edit:', err); } finally { setIsSavingEdit(false); setEditingItem(null); @@ -491,14 +508,21 @@ export const InventoryPage: React.FC = () => {
- + @@ -570,6 +594,252 @@ export const InventoryPage: React.FC = () => {
+ {/* ── Quick Edit Modal ── */} + {editingItem && ( +
+
+ {/* Modal Header */} +
+
+

+ Quick Edit Product +

+

+ Update pricing and stock or launch full editor +

+
+ +
+ + {/* Modal Body */} +
+ {/* Product Info Strip */} +
+ {editingItem.title} +
+
+ {editingItem.title} +
+
+ + SKU: {editingItem.sku} + + + {editingItem.category} + +
+
+
+ + {/* Title Field */} +
+ + setEditTitle(e.target.value)} + style={{ + width: '100%', + height: '38px', + padding: '0 12px', + border: '1px solid #D1D5DB', + borderRadius: '8px', + fontSize: '0.86rem', + color: '#111827' + }} + /> +
+ + {/* Pricing & Stock Grid */} +
+
+ + setEditPrice(e.target.value === '' ? '' : Number(e.target.value))} + style={{ + width: '100%', + height: '38px', + padding: '0 12px', + border: '1px solid #D1D5DB', + borderRadius: '8px', + fontSize: '0.86rem', + color: '#111827', + fontWeight: 600 + }} + /> +
+
+ + setEditMrp(e.target.value === '' ? '' : Number(e.target.value))} + style={{ + width: '100%', + height: '38px', + padding: '0 12px', + border: '1px solid #D1D5DB', + borderRadius: '8px', + fontSize: '0.86rem', + color: '#6B7280' + }} + /> +
+
+ + setEditStock(e.target.value === '' ? '' : Number(e.target.value))} + style={{ + width: '100%', + height: '38px', + padding: '0 12px', + border: '1px solid #D1D5DB', + borderRadius: '8px', + fontSize: '0.86rem', + color: '#111827', + fontWeight: 600 + }} + /> +
+
+
+ + {/* Modal Footer */} +
+ +
+ + +
+
+
+
+ )} +
); };