feat(admin): hierarchical category/subcategory cascade in walk-in POS order modal and indented category list
This commit is contained in:
parent
7a9c500e85
commit
1cd5dc7cb6
3 changed files with 101 additions and 27 deletions
|
|
@ -1440,16 +1440,34 @@ export default function CategoriesPage() {
|
||||||
pager.items.map((c) => {
|
pager.items.map((c) => {
|
||||||
const parent = categories.find((parentCat) => idsEqual(parentCat.category_id, c.parent_category_id));
|
const parent = categories.find((parentCat) => idsEqual(parentCat.category_id, c.parent_category_id));
|
||||||
const parentLabel = parent && !isRootParentId(c.parent_category_id) ? parent.name : '- Root -';
|
const parentLabel = parent && !isRootParentId(c.parent_category_id) ? parent.name : '- Root -';
|
||||||
|
const isChild = Boolean(c.parent_category_id && !isRootParentId(c.parent_category_id));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<tr key={c.category_id} className="border-t border-border hover:bg-muted/10 transition-colors">
|
<tr key={c.category_id} className={`border-t border-border hover:bg-muted/10 transition-colors ${isChild ? 'bg-muted/5' : 'bg-card font-medium'}`}>
|
||||||
{visibleColumns.categoryName && (
|
{visibleColumns.categoryName && (
|
||||||
<td className="px-5 py-3.5">
|
<td className="px-5 py-3.5">
|
||||||
<div className="flex items-center gap-3 min-w-0">
|
<div className={`flex items-center gap-2.5 min-w-0 ${isChild ? 'pl-6' : ''}`}>
|
||||||
<div className="w-9 h-9 crm-radius-icon bg-primary/10 text-primary flex items-center justify-center shrink-0">
|
{isChild && (
|
||||||
<Folder className="w-4 h-4" />
|
<span className="text-muted-foreground text-sm font-bold shrink-0">↳</span>
|
||||||
|
)}
|
||||||
|
<div className={`w-8 h-8 crm-radius-icon ${isChild ? 'bg-muted text-muted-foreground' : 'bg-primary/10 text-primary'} flex items-center justify-center shrink-0`}>
|
||||||
|
<Folder className="w-3.5 h-3.5" />
|
||||||
</div>
|
</div>
|
||||||
<div className="min-w-0">
|
<div className="min-w-0">
|
||||||
<p className="text-[13px] font-semibold text-foreground truncate">{c.name}</p>
|
<div className="flex items-center gap-2">
|
||||||
|
<p className={`text-[13px] ${isChild ? 'font-medium text-foreground' : 'font-bold text-foreground'} truncate`}>
|
||||||
|
{c.name}
|
||||||
|
</p>
|
||||||
|
{!isChild ? (
|
||||||
|
<span className="px-1.5 py-0.2 text-[9px] font-bold uppercase rounded bg-primary/10 text-primary shrink-0">
|
||||||
|
Parent
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="px-1.5 py-0.2 text-[9px] font-medium uppercase rounded bg-muted text-muted-foreground shrink-0">
|
||||||
|
Sub
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
<p className="text-[11px] text-muted-foreground truncate">{c.slug}</p>
|
<p className="text-[11px] text-muted-foreground truncate">{c.slug}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -728,3 +728,4 @@ export default function AdminCouponsPage() {
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,7 @@ export default function OrderIntelligencePage() {
|
||||||
const [categoriesList, setCategoriesList] = useState<any[]>([]);
|
const [categoriesList, setCategoriesList] = useState<any[]>([]);
|
||||||
const [brandsList, setBrandsList] = useState<any[]>([]);
|
const [brandsList, setBrandsList] = useState<any[]>([]);
|
||||||
const [selectedCategoryId, setSelectedCategoryId] = useState('');
|
const [selectedCategoryId, setSelectedCategoryId] = useState('');
|
||||||
|
const [selectedSubCategoryId, setSelectedSubCategoryId] = useState('');
|
||||||
const [selectedBrandId, setSelectedBrandId] = useState('');
|
const [selectedBrandId, setSelectedBrandId] = useState('');
|
||||||
const [selectedProductId, setSelectedProductId] = useState('');
|
const [selectedProductId, setSelectedProductId] = useState('');
|
||||||
const [skuOptions, setSkuOptions] = useState<any[]>([]);
|
const [skuOptions, setSkuOptions] = useState<any[]>([]);
|
||||||
|
|
@ -354,31 +355,44 @@ export default function OrderIntelligencePage() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const availableCategoriesOptions = useMemo(() => {
|
const availableCategoriesOptions = useMemo(() => {
|
||||||
if (categoriesList.length > 0) {
|
// Only return Root Parent Categories (parent_category_id is null/empty)
|
||||||
return categoriesList.map((c) => ({
|
const roots = categoriesList.filter((c) => !c.parent_category_id);
|
||||||
value: c.category_id,
|
const sourceList = roots.length > 0 ? roots : categoriesList;
|
||||||
label: c.name,
|
return sourceList.map((c) => ({
|
||||||
}));
|
value: c.category_id,
|
||||||
}
|
label: c.name,
|
||||||
const map = new Map<string, string>();
|
}));
|
||||||
productsList.forEach((p) => {
|
}, [categoriesList]);
|
||||||
if (p.category_id) {
|
|
||||||
map.set(p.category_id, p.category_name || p.category?.name || 'Category');
|
const availableSubCategoriesOptions = useMemo(() => {
|
||||||
}
|
if (!selectedCategoryId) return [];
|
||||||
});
|
const subs = categoriesList.filter((c) => c.parent_category_id === selectedCategoryId);
|
||||||
return Array.from(map.entries()).map(([id, name]) => ({ value: id, label: name }));
|
if (subs.length === 0) return [];
|
||||||
}, [categoriesList, productsList]);
|
return [
|
||||||
|
{ value: '', label: 'All Sub-Categories' },
|
||||||
|
...subs.map((s) => ({ value: s.category_id, label: s.name })),
|
||||||
|
];
|
||||||
|
}, [categoriesList, selectedCategoryId]);
|
||||||
|
|
||||||
|
const activeCategoryIds = useMemo(() => {
|
||||||
|
if (!selectedCategoryId) return [];
|
||||||
|
if (selectedSubCategoryId) return [selectedSubCategoryId];
|
||||||
|
const childIds = categoriesList
|
||||||
|
.filter((c) => c.parent_category_id === selectedCategoryId)
|
||||||
|
.map((c) => c.category_id);
|
||||||
|
return [selectedCategoryId, ...childIds];
|
||||||
|
}, [categoriesList, selectedCategoryId, selectedSubCategoryId]);
|
||||||
|
|
||||||
const availableBrandsOptions = useMemo(() => {
|
const availableBrandsOptions = useMemo(() => {
|
||||||
const filteredProducts = selectedCategoryId
|
const filteredProducts = activeCategoryIds.length > 0
|
||||||
? productsList.filter((p) => p.category_id === selectedCategoryId)
|
? productsList.filter((p) => activeCategoryIds.includes(p.category_id))
|
||||||
: productsList;
|
: productsList;
|
||||||
|
|
||||||
const brandIdsInFilteredProducts = new Set(filteredProducts.map((p) => p.brand_id).filter(Boolean));
|
const brandIdsInFilteredProducts = new Set(filteredProducts.map((p) => p.brand_id).filter(Boolean));
|
||||||
|
|
||||||
if (brandsList.length > 0) {
|
if (brandsList.length > 0) {
|
||||||
return brandsList
|
return brandsList
|
||||||
.filter((b) => !selectedCategoryId || brandIdsInFilteredProducts.has(b.brand_id))
|
.filter((b) => activeCategoryIds.length === 0 || brandIdsInFilteredProducts.has(b.brand_id))
|
||||||
.map((b) => ({
|
.map((b) => ({
|
||||||
value: b.brand_id,
|
value: b.brand_id,
|
||||||
label: b.name,
|
label: b.name,
|
||||||
|
|
@ -392,12 +406,12 @@ export default function OrderIntelligencePage() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return Array.from(map.entries()).map(([id, name]) => ({ value: id, label: name }));
|
return Array.from(map.entries()).map(([id, name]) => ({ value: id, label: name }));
|
||||||
}, [brandsList, productsList, selectedCategoryId]);
|
}, [brandsList, productsList, activeCategoryIds]);
|
||||||
|
|
||||||
const availableProductsOptions = useMemo(() => {
|
const availableProductsOptions = useMemo(() => {
|
||||||
let filtered = productsList;
|
let filtered = productsList;
|
||||||
if (selectedCategoryId) {
|
if (activeCategoryIds.length > 0) {
|
||||||
filtered = filtered.filter((p) => p.category_id === selectedCategoryId);
|
filtered = filtered.filter((p) => activeCategoryIds.includes(p.category_id));
|
||||||
}
|
}
|
||||||
if (selectedBrandId) {
|
if (selectedBrandId) {
|
||||||
filtered = filtered.filter((p) => p.brand_id === selectedBrandId);
|
filtered = filtered.filter((p) => p.brand_id === selectedBrandId);
|
||||||
|
|
@ -406,7 +420,7 @@ export default function OrderIntelligencePage() {
|
||||||
value: p.product_id,
|
value: p.product_id,
|
||||||
label: p.name,
|
label: p.name,
|
||||||
}));
|
}));
|
||||||
}, [productsList, selectedCategoryId, selectedBrandId]);
|
}, [productsList, activeCategoryIds, selectedBrandId]);
|
||||||
|
|
||||||
const availableVariantsForProduct = useMemo(() => {
|
const availableVariantsForProduct = useMemo(() => {
|
||||||
if (!selectedProductId) return [];
|
if (!selectedProductId) return [];
|
||||||
|
|
@ -656,6 +670,7 @@ export default function OrderIntelligencePage() {
|
||||||
setBuyerPhone('');
|
setBuyerPhone('');
|
||||||
setShippingAddress('');
|
setShippingAddress('');
|
||||||
setSelectedCategoryId('');
|
setSelectedCategoryId('');
|
||||||
|
setSelectedSubCategoryId('');
|
||||||
setSelectedBrandId('');
|
setSelectedBrandId('');
|
||||||
setSelectedProductId('');
|
setSelectedProductId('');
|
||||||
setTempSkuId('');
|
setTempSkuId('');
|
||||||
|
|
@ -1383,6 +1398,7 @@ export default function OrderIntelligencePage() {
|
||||||
value={selectedCategoryId}
|
value={selectedCategoryId}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
setSelectedCategoryId(value);
|
setSelectedCategoryId(value);
|
||||||
|
setSelectedSubCategoryId('');
|
||||||
setSelectedBrandId('');
|
setSelectedBrandId('');
|
||||||
setSelectedProductId('');
|
setSelectedProductId('');
|
||||||
setTempSkuId('');
|
setTempSkuId('');
|
||||||
|
|
@ -1395,6 +1411,45 @@ export default function OrderIntelligencePage() {
|
||||||
placeholder="Select Category"
|
placeholder="Select Category"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{availableSubCategoriesOptions.length > 0 ? (
|
||||||
|
<div>
|
||||||
|
<label className={labelClass}>Sub-Category</label>
|
||||||
|
<CustomSelect
|
||||||
|
value={selectedSubCategoryId}
|
||||||
|
onChange={(value) => {
|
||||||
|
setSelectedSubCategoryId(value);
|
||||||
|
setSelectedBrandId('');
|
||||||
|
setSelectedProductId('');
|
||||||
|
setTempSkuId('');
|
||||||
|
setTempPrice(0);
|
||||||
|
}}
|
||||||
|
options={availableSubCategoriesOptions}
|
||||||
|
placeholder="All Sub-Categories"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
<label className={labelClass}>Product Brand</label>
|
||||||
|
<CustomSelect
|
||||||
|
value={selectedBrandId}
|
||||||
|
disabled={!selectedCategoryId}
|
||||||
|
onChange={(value) => {
|
||||||
|
setSelectedBrandId(value);
|
||||||
|
setSelectedProductId('');
|
||||||
|
setTempSkuId('');
|
||||||
|
setTempPrice(0);
|
||||||
|
}}
|
||||||
|
options={[
|
||||||
|
{ value: '', label: !selectedCategoryId ? 'Select Category first' : 'Select Brand' },
|
||||||
|
...availableBrandsOptions,
|
||||||
|
]}
|
||||||
|
placeholder={!selectedCategoryId ? 'Select Category first' : 'Select Brand'}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{availableSubCategoriesOptions.length > 0 && (
|
||||||
<div>
|
<div>
|
||||||
<label className={labelClass}>Product Brand</label>
|
<label className={labelClass}>Product Brand</label>
|
||||||
<CustomSelect
|
<CustomSelect
|
||||||
|
|
@ -1413,7 +1468,7 @@ export default function OrderIntelligencePage() {
|
||||||
placeholder={!selectedCategoryId ? 'Select Category first' : 'Select Brand'}
|
placeholder={!selectedCategoryId ? 'Select Category first' : 'Select Brand'}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
)}
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className={labelClass}>Product</label>
|
<label className={labelClass}>Product</label>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue