diff --git a/src/App.jsx b/src/App.jsx index f1b67a0..aa3d367 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -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 = () => { } /> } /> } /> + } /> } /> } /> diff --git a/src/components/Sidebar.jsx b/src/components/Sidebar.jsx index 2ba3e66..40b8525 100644 --- a/src/components/Sidebar.jsx +++ b/src/components/Sidebar.jsx @@ -32,6 +32,11 @@ const Sidebar = () => { 📦 Products +
  • + isActive ? 'active' : ''}> + 🏷️ Categories + +
  • isActive ? 'active' : ''}> 📋 Audit Logs diff --git a/src/pages/products/CategoryList.jsx b/src/pages/products/CategoryList.jsx new file mode 100644 index 0000000..2216ebf --- /dev/null +++ b/src/pages/products/CategoryList.jsx @@ -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 ( +
    +

    Category & Subcategory Management

    + +
    + {/* Left Column: Form */} +
    +

    Add Category / Subcategory

    +
    +
    + + setName(e.target.value)} + required + /> +
    + +
    + + setSlug(e.target.value)} + /> +
    + +
    + + +
    + + +
    +
    + + {/* Right Column: Display list */} +
    +

    Existing Hierarchy

    + + {loading ? ( +
    Loading hierarchy...
    + ) : error ? ( +
    Error: {error}
    + ) : categories.length === 0 ? ( +
    No categories defined yet.
    + ) : ( +
    + {categories.map(cat => ( +
    +
    + 🏷️ {cat.name} + slug: {cat.slug} +
    + + {/* Subcategories list */} + {cat.subcategories && cat.subcategories.length > 0 ? ( +
    + {cat.subcategories.map(sub => ( +
    + ↳ {sub.name} + slug: {sub.slug} +
    + ))} +
    + ) : ( +
    + No subcategories mapped yet. +
    + )} +
    + ))} +
    + )} +
    +
    +
    + ); +}; + +export default CategoryList; diff --git a/src/pages/products/ProductList.jsx b/src/pages/products/ProductList.jsx index 8a740a7..d6832b2 100644 --- a/src/pages/products/ProductList.jsx +++ b/src/pages/products/ProductList.jsx @@ -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 = () => { )} + {/* Dynamic Category Mapping Dropdown */} +
    +

    Category Mapping

    +
    + + +
    +
    +

    Seller / Supplier Information