Preselect currently active section in the filter menu

Fixes #2533

Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
This commit is contained in:
Kevin Decherf 2021-01-23 21:34:45 +01:00
parent 07e249ee57
commit 8e89b3ad76
3 changed files with 41 additions and 5 deletions

View file

@ -532,6 +532,8 @@ class EntryController extends Controller
$searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : ''); $currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
$formOptions = [];
switch ($type) { switch ($type) {
case 'search': case 'search':
$qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
@ -541,12 +543,15 @@ class EntryController extends Controller
break; break;
case 'starred': case 'starred':
$qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
$formOptions['filter_starred'] = true;
break; break;
case 'archive': case 'archive':
$qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
$formOptions['filter_archived'] = true;
break; break;
case 'unread': case 'unread':
$qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
$formOptions['filter_unread'] = true;
break; break;
case 'all': case 'all':
$qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
@ -555,7 +560,7 @@ class EntryController extends Controller
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
} }
$form = $this->createForm(EntryFilterType::class); $form = $this->createForm(EntryFilterType::class, [], $formOptions);
if ($request->query->has($form->getName())) { if ($request->query->has($form->getName())) {
// manually bind values from the request // manually bind values from the request

View file

@ -118,12 +118,15 @@ class EntryFilterType extends AbstractType
]) ])
->add('isArchived', CheckboxFilterType::class, [ ->add('isArchived', CheckboxFilterType::class, [
'label' => 'entry.filters.archived_label', 'label' => 'entry.filters.archived_label',
'data' => $options['filter_archived'],
]) ])
->add('isStarred', CheckboxFilterType::class, [ ->add('isStarred', CheckboxFilterType::class, [
'label' => 'entry.filters.starred_label', 'label' => 'entry.filters.starred_label',
'data' => $options['filter_starred'],
]) ])
->add('isUnread', CheckboxFilterType::class, [ ->add('isUnread', CheckboxFilterType::class, [
'label' => 'entry.filters.unread_label', 'label' => 'entry.filters.unread_label',
'data' => $options['filter_unread'],
'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
if (false === $values['value']) { if (false === $values['value']) {
return; return;
@ -177,6 +180,9 @@ class EntryFilterType extends AbstractType
$resolver->setDefaults([ $resolver->setDefaults([
'csrf_protection' => false, 'csrf_protection' => false,
'validation_groups' => ['filtering'], 'validation_groups' => ['filtering'],
'filter_archived' => false,
'filter_starred' => false,
'filter_unread' => false,
]); ]);
} }
} }

View file

@ -857,7 +857,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(5, $crawler->filter('li.entry')); $this->assertCount(4, $crawler->filter('li.entry'));
$data = [ $data = [
'entry_filter[createdAt][left_date]' => $today->format('Y-m-d'), 'entry_filter[createdAt][left_date]' => $today->format('Y-m-d'),
@ -866,7 +866,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(5, $crawler->filter('li.entry')); $this->assertCount(4, $crawler->filter('li.entry'));
$data = [ $data = [
'entry_filter[createdAt][left_date]' => '1970-01-01', 'entry_filter[createdAt][left_date]' => '1970-01-01',
@ -919,7 +919,7 @@ class EntryControllerTest extends WallabagCoreTestCase
]; ];
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(5, $crawler->filter('li.entry')); $this->assertCount(4, $crawler->filter('li.entry'));
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
@ -928,7 +928,7 @@ class EntryControllerTest extends WallabagCoreTestCase
]; ];
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(5, $crawler->filter('li.entry')); $this->assertCount(4, $crawler->filter('li.entry'));
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
$data = [ $data = [
@ -948,6 +948,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
$form['entry_filter[isArchived]']->tick(); $form['entry_filter[isArchived]']->tick();
$form['entry_filter[isStarred]']->untick(); $form['entry_filter[isStarred]']->untick();
$form['entry_filter[isUnread]']->untick();
$crawler = $client->submit($form); $crawler = $client->submit($form);
$this->assertCount(1, $crawler->filter('li.entry')); $this->assertCount(1, $crawler->filter('li.entry'));
@ -960,6 +961,30 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertCount(1, $crawler->filter('li.entry')); $this->assertCount(1, $crawler->filter('li.entry'));
} }
public function testFilterPreselectedStatus()
{
$this->logInAs('admin');
$client = $this->getClient();
$crawler = $client->request('GET', '/unread/list');
$form = $crawler->filter('button[id=submit-filter]')->form();
$this->assertTrue($form['entry_filter[isUnread]']->hasValue());
$this->assertFalse($form['entry_filter[isArchived]']->hasValue());
$this->assertFalse($form['entry_filter[isStarred]']->hasValue());
$crawler = $client->request('GET', '/starred/list');
$form = $crawler->filter('button[id=submit-filter]')->form();
$this->assertFalse($form['entry_filter[isUnread]']->hasValue());
$this->assertFalse($form['entry_filter[isArchived]']->hasValue());
$this->assertTrue($form['entry_filter[isStarred]']->hasValue());
$crawler = $client->request('GET', '/all/list');
$form = $crawler->filter('button[id=submit-filter]')->form();
$this->assertFalse($form['entry_filter[isUnread]']->hasValue());
$this->assertFalse($form['entry_filter[isArchived]']->hasValue());
$this->assertFalse($form['entry_filter[isStarred]']->hasValue());
}
public function testFilterOnIsPublic() public function testFilterOnIsPublic()
{ {
$this->logInAs('admin'); $this->logInAs('admin');