feat(edit): 编辑页开发
This commit is contained in:
@@ -369,28 +369,47 @@ async function persistCategoryTree(
|
||||
};
|
||||
}
|
||||
|
||||
function flattenCategories(
|
||||
export function flattenCategories(
|
||||
nodes: OzonCategoryNode[],
|
||||
path: string[] = [],
|
||||
parentKey: string | null = null,
|
||||
inheritedCategory?: { id: string; name: string },
|
||||
): FlatCategory[] {
|
||||
const flattened: FlatCategory[] = [];
|
||||
for (const node of nodes) {
|
||||
const categoryId = String(node.description_category_id ?? '').trim();
|
||||
if (!categoryId || !node.category_name?.trim()) continue;
|
||||
// Ozon's last level may contain only type_name/type_id. It is still a
|
||||
// publishable option for the closest description category, so inherit
|
||||
// its category ID while retaining the type's own display name and ID.
|
||||
const explicitCategoryId = String(node.description_category_id ?? '').trim();
|
||||
const explicitCategoryName = node.category_name?.trim() ?? '';
|
||||
const categoryId = explicitCategoryId || inheritedCategory?.id || '';
|
||||
const categoryName = explicitCategoryName || inheritedCategory?.name || '';
|
||||
const typeId = String(node.type_id ?? 0);
|
||||
const nextPath = [...path, node.category_name.trim()];
|
||||
const typeName = node.type_name?.trim() ?? '';
|
||||
const name = explicitCategoryName || typeName || categoryName;
|
||||
if (!categoryId || !name) continue;
|
||||
const nextPath = [...path, name];
|
||||
const key = categoryKey(categoryId, typeId);
|
||||
flattened.push({
|
||||
external_category_id: categoryId,
|
||||
external_type_id: typeId,
|
||||
parent_key: parentKey,
|
||||
name: node.category_name.trim(),
|
||||
name,
|
||||
path: nextPath.join(' › '),
|
||||
is_active: node.disabled !== true && typeId !== '0',
|
||||
source_payload: node,
|
||||
});
|
||||
flattened.push(...flattenCategories(node.children ?? [], nextPath, key));
|
||||
const nextInheritedCategory = explicitCategoryId && explicitCategoryName
|
||||
? { id: explicitCategoryId, name: explicitCategoryName }
|
||||
: inheritedCategory;
|
||||
flattened.push(
|
||||
...flattenCategories(
|
||||
node.children ?? [],
|
||||
nextPath,
|
||||
key,
|
||||
nextInheritedCategory,
|
||||
),
|
||||
);
|
||||
}
|
||||
return flattened;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user