subcategories implemented and working now

This commit is contained in:
Priec
2026-06-17 15:11:21 +02:00
parent f54fd3d717
commit 43562e964a
3 changed files with 62 additions and 17 deletions

View File

@@ -51,7 +51,7 @@ async fn category_sidebar(
&v,
"shop/_sidebar.html",
json!({
"category_tree": view::sidebar_rows(&categories::tree(&published)),
"category_groups": view::sidebar_groups(&published),
"lang": current_lang(&jar),
}),
)

View File

@@ -45,12 +45,24 @@ pub fn product_form(product: &products::Model, image: Option<String>) -> Value {
})
}
/// Depth-ordered `{ name, slug, depth }` rows for the storefront sidebar,
/// rendered as an indented flat list.
pub fn sidebar_rows(tree: &[(categories::Model, usize)]) -> Vec<Value> {
tree.iter()
.map(|(category, depth)| {
json!({ "name": category.name, "slug": category.slug, "depth": depth })
/// Two-level grouping for the storefront sidebar menu: each top-level category
/// as `{ name, slug, children: [{ name, slug }] }`, with its direct
/// subcategories nested under `children` (empty when the category has none).
/// Siblings are ordered by position then name on both levels.
pub fn sidebar_groups(categories: &[categories::Model]) -> Vec<Value> {
let mut top: Vec<&categories::Model> = categories
.iter()
.filter(|c| c.parent_id.is_none())
.collect();
top.sort_by(|a, b| a.position.cmp(&b.position).then_with(|| a.name.cmp(&b.name)));
top.into_iter()
.map(|category| {
let children: Vec<Value> = crate::models::categories::children_of(categories, category.id)
.into_iter()
.map(|child| json!({ "name": child.name, "slug": child.slug }))
.collect();
json!({ "name": category.name, "slug": category.slug, "children": children })
})
.collect()
}