no redux
This commit is contained in:
111
src/utils/categoryUtils.js
Normal file
111
src/utils/categoryUtils.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Pure category tree helper functions.
|
||||
* These operate on plain category maps and have no side-effects.
|
||||
*/
|
||||
|
||||
export const ROOT_CATEGORY_NAME = 'Home';
|
||||
|
||||
/**
|
||||
* @param {object} item
|
||||
* @returns {object}
|
||||
*/
|
||||
export function normalizeCategoryNode(item) {
|
||||
return {
|
||||
...item,
|
||||
id: item.id,
|
||||
parentId: item.parentId,
|
||||
name: item.name == null ? null : String(item.name).trim() || null,
|
||||
posterminalIds: Array.isArray(item.posterminalIds) ? item.posterminalIds : [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the raw GET /api/category/tree response body into normalized state.
|
||||
* @param {object} data
|
||||
* @param {number|string|null} companyRootCategoryId
|
||||
*/
|
||||
export function parseCategoryTreeResponse(data, companyRootCategoryId) {
|
||||
const list = Array.isArray(data.tree) ? data.tree : [];
|
||||
const rootCategoryId = data.rootCategoryId != null ? data.rootCategoryId : companyRootCategoryId;
|
||||
|
||||
const categories = Object.fromEntries(
|
||||
list.map((item) => [String(item.id), normalizeCategoryNode(item)]),
|
||||
);
|
||||
|
||||
return {
|
||||
rootCategoryId,
|
||||
categories,
|
||||
rootArticles: Array.isArray(data.articles) ? data.articles : [],
|
||||
languageId: data.languageId,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Display name for a category node (root → "Home").
|
||||
*/
|
||||
export function categoryDisplayName(name, categoryId, rootCategoryId) {
|
||||
if (rootCategoryId != null && String(categoryId) === String(rootCategoryId)) {
|
||||
return ROOT_CATEGORY_NAME;
|
||||
}
|
||||
return name?.trim() || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* URL slug for a category node (root → "Home").
|
||||
*/
|
||||
export function categorySlug(node, rootCategoryId) {
|
||||
if (rootCategoryId != null && String(node.id) === String(rootCategoryId)) {
|
||||
return ROOT_CATEGORY_NAME;
|
||||
}
|
||||
return node.slug || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct children of `parentId`, sorted by `sort` then `id`.
|
||||
*/
|
||||
export function getCategoryChildren(categories, parentId) {
|
||||
if (!categories) return [];
|
||||
const parentKey = String(parentId);
|
||||
return Object.values(categories)
|
||||
.filter((item) => item.parentId != null && String(item.parentId) === parentKey)
|
||||
.sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0) || a.id - b.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Breadcrumb trail from root to `currentCategoryId`.
|
||||
*/
|
||||
export function getCategoryBreadcrumbs(categories, currentCategoryId, rootCategoryId) {
|
||||
if (!categories || currentCategoryId == null) return [];
|
||||
|
||||
const crumbs = [];
|
||||
const seen = new Set();
|
||||
let id = currentCategoryId;
|
||||
|
||||
while (id != null && !seen.has(String(id))) {
|
||||
seen.add(String(id));
|
||||
const node = categories[String(id)];
|
||||
if (!node) break;
|
||||
|
||||
crumbs.unshift({
|
||||
id: node.id,
|
||||
name: categoryDisplayName(node.name, node.id, rootCategoryId),
|
||||
slug: categorySlug(node, rootCategoryId),
|
||||
});
|
||||
|
||||
const parentId = node.parentId;
|
||||
if (parentId == null || parentId === 0) break;
|
||||
if (String(parentId) === String(id)) break;
|
||||
id = parentId;
|
||||
}
|
||||
|
||||
return crumbs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map of category.id → category node from a raw tree response.
|
||||
* @param {object} data
|
||||
* @param {number|string|null} companyRootCategoryId
|
||||
*/
|
||||
export function normalizeCategoryMap(data, companyRootCategoryId) {
|
||||
return parseCategoryTreeResponse(data, companyRootCategoryId).categories;
|
||||
}
|
||||
Reference in New Issue
Block a user