feat: implement category management system with hierarchy support and product-category mapping UI
This commit is contained in:
parent
971dfd0e18
commit
c90eabfe52
4 changed files with 252 additions and 0 deletions
|
|
@ -12,6 +12,7 @@ import SellerDetail from './pages/sellers/SellerDetail';
|
|||
import CustomerList from './pages/customers/CustomerList';
|
||||
import CustomerDetail from './pages/customers/CustomerDetail';
|
||||
import ProductList from './pages/products/ProductList';
|
||||
import CategoryList from './pages/products/CategoryList';
|
||||
import AuditLogs from './pages/AuditLogs';
|
||||
import OrderPaymentStatus from './pages/orders/OrderPaymentStatus';
|
||||
import IssueRefund from './pages/orders/IssueRefund';
|
||||
|
|
@ -52,6 +53,7 @@ const AppRoutes = () => {
|
|||
<Route path="/customers" element={<ProtectedLayout><CustomerList /></ProtectedLayout>} />
|
||||
<Route path="/customers/:id" element={<ProtectedLayout><CustomerDetail /></ProtectedLayout>} />
|
||||
<Route path="/products" element={<ProtectedLayout><ProductList /></ProtectedLayout>} />
|
||||
<Route path="/categories" element={<ProtectedLayout><CategoryList /></ProtectedLayout>} />
|
||||
<Route path="/audit" element={<ProtectedLayout><AuditLogs /></ProtectedLayout>} />
|
||||
|
||||
<Route path="/orders/payment" element={<ProtectedLayout><OrderPaymentStatus /></ProtectedLayout>} />
|
||||
|
|
|
|||
|
|
@ -32,6 +32,11 @@ const Sidebar = () => {
|
|||
📦 Products
|
||||
</NavLink>
|
||||
</li>
|
||||
<li className="sidebar-item">
|
||||
<NavLink to="/categories" className={({ isActive }) => isActive ? 'active' : ''}>
|
||||
🏷️ Categories
|
||||
</NavLink>
|
||||
</li>
|
||||
<li className="sidebar-item">
|
||||
<NavLink to="/audit" className={({ isActive }) => isActive ? 'active' : ''}>
|
||||
📋 Audit Logs
|
||||
|
|
|
|||
172
src/pages/products/CategoryList.jsx
Normal file
172
src/pages/products/CategoryList.jsx
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
import React, { useState, useEffect } from 'react';
|
||||
import { API_BASE_URL } from '../../config';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
import StatusBadge from '../../components/StatusBadge';
|
||||
|
||||
const CategoryList = () => {
|
||||
const { token } = useAuth();
|
||||
const [categories, setCategories] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
// Form states
|
||||
const [name, setName] = useState('');
|
||||
const [slug, setSlug] = useState('');
|
||||
const [parent, setParent] = useState('');
|
||||
|
||||
const fetchCategories = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const res = await fetch(`${API_BASE_URL}/categories/`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to fetch categories');
|
||||
const data = await res.json();
|
||||
setCategories(data);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchCategories();
|
||||
}, [token]);
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
if (!name.trim()) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/categories/create/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name,
|
||||
slug: slug.trim() || undefined,
|
||||
parent: parent ? Number(parent) : undefined
|
||||
})
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to create category');
|
||||
alert('Category/Subcategory created successfully!');
|
||||
setName('');
|
||||
setSlug('');
|
||||
setParent('');
|
||||
fetchCategories();
|
||||
} catch (err) {
|
||||
alert(err.message);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1 className="page-title">Category & Subcategory Management</h1>
|
||||
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1.5fr', gap: '24px' }}>
|
||||
{/* Left Column: Form */}
|
||||
<div className="glass-card">
|
||||
<h3 className="section-title">Add Category / Subcategory</h3>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="form-group">
|
||||
<label className="form-label">Name</label>
|
||||
<input
|
||||
type="text"
|
||||
className="form-input"
|
||||
placeholder="e.g. Traditional Handlooms"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label className="form-label">Custom Slug (Optional)</label>
|
||||
<input
|
||||
type="text"
|
||||
className="form-input"
|
||||
placeholder="e.g. traditional-handlooms"
|
||||
value={slug}
|
||||
onChange={(e) => setSlug(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label className="form-label">Parent Category</label>
|
||||
<select
|
||||
className="form-input"
|
||||
value={parent}
|
||||
onChange={(e) => setParent(e.target.value)}
|
||||
style={{ background: 'var(--bg-tertiary)', color: '#fff' }}
|
||||
>
|
||||
<option value="">[None] - Create Top-Level Category</option>
|
||||
{categories.map(cat => (
|
||||
<option key={cat.id} value={cat.id}>{cat.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit" className="btn btn-primary" style={{ width: '100%', marginTop: '10px' }}>
|
||||
Create Category
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* Right Column: Display list */}
|
||||
<div className="glass-card">
|
||||
<h3 className="section-title">Existing Hierarchy</h3>
|
||||
|
||||
{loading ? (
|
||||
<div style={{ padding: '20px', textAlign: 'center' }}>Loading hierarchy...</div>
|
||||
) : error ? (
|
||||
<div style={{ padding: '20px', color: 'var(--color-danger)' }}>Error: {error}</div>
|
||||
) : categories.length === 0 ? (
|
||||
<div style={{ padding: '20px', color: 'var(--text-secondary)' }}>No categories defined yet.</div>
|
||||
) : (
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: '16px' }}>
|
||||
{categories.map(cat => (
|
||||
<div
|
||||
key={cat.id}
|
||||
style={{
|
||||
background: 'rgba(255, 255, 255, 0.02)',
|
||||
border: '1px solid var(--glass-border)',
|
||||
borderRadius: '8px',
|
||||
padding: '16px'
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<span style={{ fontWeight: '700', color: 'var(--accent-hover)' }}>🏷️ {cat.name}</span>
|
||||
<span style={{ fontSize: '12px', color: 'var(--text-muted)', fontFamily: 'monospace' }}>slug: {cat.slug}</span>
|
||||
</div>
|
||||
|
||||
{/* Subcategories list */}
|
||||
{cat.subcategories && cat.subcategories.length > 0 ? (
|
||||
<div style={{ marginTop: '12px', paddingLeft: '20px', borderLeft: '2px solid var(--glass-border)', display: 'flex', flexDirection: 'column', gap: '8px' }}>
|
||||
{cat.subcategories.map(sub => (
|
||||
<div key={sub.id} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', fontSize: '13px' }}>
|
||||
<span style={{ fontWeight: '500' }}>↳ {sub.name}</span>
|
||||
<span style={{ fontSize: '11px', color: 'var(--text-muted)', fontFamily: 'monospace' }}>slug: {sub.slug}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div style={{ marginTop: '8px', fontSize: '12px', color: 'var(--text-muted)', paddingLeft: '20px' }}>
|
||||
No subcategories mapped yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CategoryList;
|
||||
|
|
@ -9,9 +9,11 @@ const ProductList = () => {
|
|||
const [productFilter, setProductFilter] = useState('all');
|
||||
const [sellerFilter, setSellerFilter] = useState('all');
|
||||
const [products, setProducts] = useState([]);
|
||||
const [categories, setCategories] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
const [selectedProduct, setSelectedProduct] = useState(null);
|
||||
const [selectedCatVal, setSelectedCatVal] = useState('');
|
||||
|
||||
// Pagination states
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
|
@ -44,10 +46,34 @@ const ProductList = () => {
|
|||
}
|
||||
};
|
||||
|
||||
const fetchCategories = async () => {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/categories/`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setCategories(data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("Failed to fetch categories", err);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchProducts();
|
||||
fetchCategories();
|
||||
}, [productFilter, sellerFilter, token]);
|
||||
|
||||
// Set selected category option in select when detail product changes
|
||||
useEffect(() => {
|
||||
if (selectedProduct) {
|
||||
setSelectedCatVal(selectedProduct.category || '');
|
||||
}
|
||||
}, [selectedProduct]);
|
||||
|
||||
const handleStatusChange = async (prodId, action) => {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/products/${prodId}/${action}/`, {
|
||||
|
|
@ -343,6 +369,53 @@ const ProductList = () => {
|
|||
)}
|
||||
</div>
|
||||
|
||||
{/* Dynamic Category Mapping Dropdown */}
|
||||
<div className="modal-section" style={{ borderTop: '1px solid var(--glass-border)', paddingTop: '16px' }}>
|
||||
<h3 className="modal-section-title">Category Mapping</h3>
|
||||
<div style={{ display: 'flex', gap: '10px', alignItems: 'center' }}>
|
||||
<select
|
||||
className="form-input"
|
||||
value={selectedCatVal}
|
||||
onChange={(e) => setSelectedCatVal(e.target.value)}
|
||||
style={{ background: 'var(--bg-tertiary)', color: '#fff', flex: 1 }}
|
||||
>
|
||||
<option value="">[Unmapped] - Select Category / Subcategory</option>
|
||||
{categories.map(cat => (
|
||||
<React.Fragment key={cat.id}>
|
||||
<option value={cat.slug}>{cat.name} (Parent)</option>
|
||||
{cat.subcategories && cat.subcategories.map(sub => (
|
||||
<option key={sub.id} value={sub.slug}> ↳ {sub.name}</option>
|
||||
))}
|
||||
</React.Fragment>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
style={{ padding: '10px 16px', fontSize: '13px' }}
|
||||
onClick={async () => {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/products/${selectedProduct.id}/map-category/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ category: selectedCatVal })
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to map product category');
|
||||
alert('Product category mapped successfully!');
|
||||
setSelectedProduct(prev => ({ ...prev, category: selectedCatVal }));
|
||||
fetchProducts();
|
||||
} catch (err) {
|
||||
alert(err.message);
|
||||
}
|
||||
}}
|
||||
>
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="modal-section" style={{ borderTop: '1px solid var(--glass-border)', paddingTop: '16px' }}>
|
||||
<h3 className="modal-section-title">Seller / Supplier Information</h3>
|
||||
<div className="details-grid">
|
||||
|
|
|
|||
Loading…
Reference in a new issue