Skip to content

[Ui] Don't trigger grid reload at first page load #26984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions app/code/Magento/Ui/Component/Filters/Type/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Ui\Component\Filters\Type;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Component\AbstractComponent;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
Expand Down Expand Up @@ -57,20 +61,47 @@ abstract class AbstractFilter extends AbstractComponent
* @param FilterModifier $filterModifier
* @param array $components
* @param array $data
* @param BookmarkManagementInterface|null $bookmarkManagement
* @param RequestInterface|null $request
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
FilterBuilder $filterBuilder,
FilterModifier $filterModifier,
array $components = [],
array $data = []
array $data = [],
BookmarkManagementInterface $bookmarkManagement = null,
RequestInterface $request = null
) {
$this->uiComponentFactory = $uiComponentFactory;
$this->filterBuilder = $filterBuilder;
parent::__construct($context, $components, $data);
$this->filterData = $this->getContext()->getFiltersParams();
$this->filterModifier = $filterModifier;

$bookmarkManagement = $bookmarkManagement ?: ObjectManager::getInstance()
->get(BookmarkManagementInterface::class);
$request = $request ?: ObjectManager::getInstance()->get(RequestInterface::class);

$filterData = $this->getContext()->getFiltersParams();
if (!$request->isAjax()) {
$bookmark = $bookmarkManagement->getByIdentifierNamespace(
'current',
$context->getNamespace()
);
if (null !== $bookmark) {
$bookmarkConfig = $bookmark->getConfig();
$filterData = $bookmarkConfig['current']['filters']['applied'] ?? [];

$request->setParams(
[
'paging' => $bookmarkConfig['current']['paging'] ?? []
]
);
}
}

$this->filterData = $filterData;
}

/**
Expand Down
20 changes: 18 additions & 2 deletions app/code/Magento/Ui/Component/Filters/Type/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Ui\Component\Filters\Type;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Component\Form\Element\Select as ElementSelect;
use Magento\Ui\Component\Filters\FilterModifier;

Expand Down Expand Up @@ -41,6 +44,8 @@ class Select extends AbstractFilter
* @param OptionSourceInterface|null $optionsProvider
* @param array $components
* @param array $data
* @param BookmarkManagementInterface|null $bookmarkManagement
* @param RequestInterface|null $request
*/
public function __construct(
ContextInterface $context,
Expand All @@ -49,10 +54,21 @@ public function __construct(
FilterModifier $filterModifier,
OptionSourceInterface $optionsProvider = null,
array $components = [],
array $data = []
array $data = [],
BookmarkManagementInterface $bookmarkManagement = null,
RequestInterface $request = null
) {
$this->optionsProvider = $optionsProvider;
parent::__construct($context, $uiComponentFactory, $filterBuilder, $filterModifier, $components, $data);
parent::__construct(
$context,
$uiComponentFactory,
$filterBuilder,
$filterModifier,
$components,
$data,
$bookmarkManagement,
$request
);
}

/**
Expand Down
101 changes: 57 additions & 44 deletions app/code/Magento/Ui/Model/BookmarkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,58 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Ui\Model;

class BookmarkManagement implements \Magento\Ui\Api\BookmarkManagementInterface
use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Api\BookmarkRepositoryInterface;

/**
* Bookmark Management class provide functional for retrieving bookmarks by params
* @SuppressWarnings(PHPMD.LongVariable)
*/
class BookmarkManagement implements BookmarkManagementInterface
{
/**
* @var \Magento\Ui\Api\BookmarkRepositoryInterface
* @var BookmarkRepositoryInterface
*/
protected $bookmarkRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* @var \Magento\Framework\Api\FilterBuilder
* @var FilterBuilder
*/
protected $filterBuilder;

/**
* @var \Magento\Authorization\Model\UserContextInterface
* @var UserContextInterface
*/
protected $userContext;

/**
* @param \Magento\Ui\Api\BookmarkRepositoryInterface $bookmarkRepository
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Magento\Authorization\Model\UserContextInterface $userContext
* @var array
*/
private $bookmarkRegistry = [];

/**
* @param BookmarkRepositoryInterface $bookmarkRepository
* @param FilterBuilder $filterBuilder
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param UserContextInterface $userContext
*/
public function __construct(
\Magento\Ui\Api\BookmarkRepositoryInterface $bookmarkRepository,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Authorization\Model\UserContextInterface $userContext
BookmarkRepositoryInterface $bookmarkRepository,
FilterBuilder $filterBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
UserContextInterface $userContext
) {
$this->bookmarkRepository = $bookmarkRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
Expand All @@ -47,9 +63,12 @@ public function __construct(
}

/**
* {@inheritdoc}
* Create search criteria builder with namespace and user filters
*
* @param $namespace
* @return void
*/
public function loadByNamespace($namespace)
private function prepareSearchCriteriaBuilderByNamespace($namespace): void
{
$userIdFilter = $this->filterBuilder
->setField('user_id')
Expand All @@ -64,47 +83,41 @@ public function loadByNamespace($namespace)

$this->searchCriteriaBuilder->addFilters([$userIdFilter]);
$this->searchCriteriaBuilder->addFilters([$namespaceFilter]);
}

/**
* {@inheritdoc}
*/
public function loadByNamespace($namespace)
{
$this->prepareSearchCriteriaBuilderByNamespace($namespace);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->bookmarkRepository->getList($searchCriteria);

return $searchResults;
return $this->bookmarkRepository->getList($searchCriteria);
}

/**
* {@inheritdoc}
*/
public function getByIdentifierNamespace($identifier, $namespace)
{
$userIdFilter = $this->filterBuilder
->setField('user_id')
->setConditionType('eq')
->setValue($this->userContext->getUserId())
->create();
$identifierFilter = $this->filterBuilder
->setField('identifier')
->setConditionType('eq')
->setValue($identifier)
->create();
$namespaceFilter = $this->filterBuilder
->setField('namespace')
->setConditionType('eq')
->setValue($namespace)
->create();
if (!isset($this->bookmarkRegistry[$identifier . $namespace])) {
$this->prepareSearchCriteriaBuilderByNamespace($namespace);
$identifierFilter = $this->filterBuilder
->setField('identifier')
->setConditionType('eq')
->setValue($identifier)
->create();
$this->searchCriteriaBuilder->addFilters([$identifierFilter]);

$this->searchCriteriaBuilder->addFilters([$userIdFilter]);
$this->searchCriteriaBuilder->addFilters([$identifierFilter]);
$this->searchCriteriaBuilder->addFilters([$namespaceFilter]);

$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->bookmarkRepository->getList($searchCriteria);
if ($searchResults->getTotalCount() > 0) {
foreach ($searchResults->getItems() as $searchResult) {
$bookmark = $this->bookmarkRepository->getById($searchResult->getId());
return $bookmark;
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->bookmarkRepository->getList($searchCriteria);
if ($searchResults->getTotalCount() > 0) {
$items = $searchResults->getItems();
$this->bookmarkRegistry[$identifier . $namespace] = array_shift($items);
return $this->bookmarkRegistry[$identifier . $namespace];
}
}

return null;
return $this->bookmarkRegistry[$identifier . $namespace] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
namespace Magento\Ui\Test\Unit\Component\Filters\Type;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface as UiContext;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
use Magento\Framework\View\Element\UiComponent\Processor;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Component\Filters\FilterModifier;
use Magento\Ui\Component\Filters\Type\DateRange;
use Magento\Ui\Component\Form\Element\DataType\Date as FormDate;
Expand Down Expand Up @@ -41,6 +43,16 @@ class DateRangeTest extends TestCase
*/
protected $filterModifierMock;

/**
* @var BookmarkManagementInterface|MockObject
*/
private $bookmarkManagementMock;

/**
* @var RequestInterface|MockObject
*/
private $requestMock;

/**
* Set up
*/
Expand All @@ -61,6 +73,16 @@ protected function setUp(): void
FilterModifier::class,
['applyFilterModifier']
);

$this->bookmarkManagementMock = $this->getMockForAbstractClass(
BookmarkManagementInterface::class
);
$this->bookmarkManagementMock->expects($this->never())->method('getByIdentifierNamespace');

$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->addMethods(['isAjax'])
->getMockForAbstractClass();
$this->requestMock->expects($this->once())->method('isAjax')->willReturn(true);
}

/**
Expand All @@ -76,7 +98,10 @@ public function testGetComponentName()
$this->uiComponentFactory,
$this->filterBuilderMock,
$this->filterModifierMock,
[]
[],
[],
$this->bookmarkManagementMock,
$this->requestMock
);
$this->assertSame(DateRange::NAME, $dateRange->getComponentName());
}
Expand Down Expand Up @@ -148,7 +173,9 @@ public function testPrepare($name, $filterData, $expectedCondition)
$this->filterBuilderMock,
$this->filterModifierMock,
[],
['name' => $name]
['name' => $name],
$this->bookmarkManagementMock,
$this->requestMock
);

$dateRange->prepare();
Expand Down
Loading