Skip to content

Categories tree rendering too long #176

@JaxxC

Description

@JaxxC

Terms

  • Before you open this issue, I have checked if the issue has already been reported.

UnoPim Version(s) affected

0.2.1

Issue Description

Categories tree widget rendering time more then 10 seconds, and it grows with categories number increasing

Preconditions

Having large amount of categories

Steps to reproduce

  1. Go to 'Catalog->Categories'
  2. Create 100 or more categories, or import them from csv
  3. Click 'Create new category', or edit existing
  4. Tree widget rendering very long time

Expected Result

Fast rendering of tree

Actual Result

Very long rendering of tree

Screenshots

No response

Additional Context

Obviously reason is converting to JSON entire collection. And solution is quite simple - processing tree before sending it to template. I'd tested with simple method

public function create()
    {
        $tree = $this->categoryRepository->getCategoryTree(null, ['id']);

        $categories = $this->transformCategoryTree($tree);

        $leftCategoryFields = $this->categoryFieldRepository->getActiveCategoryFieldsBySection('left');

        $rightCategoryFields = $this->categoryFieldRepository->getActiveCategoryFieldsBySection('right');

        return view('admin::catalog.categories.create', compact('categories', 'leftCategoryFields', 'rightCategoryFields'));
    }

public function transformCategoryTree($categories)
    {
        return $categories->map(function ($category) {
            return [
                'id' => $category->id,
                'name' => $category->name,
                'children' => $category->children && $category->children->isNotEmpty()
                    ? $this->transformCategoryTree($category->children)
                    : [],
            ];
        })->toArray();
    }

And rendering become fast

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions