Rewrite filters to better ui
This commit is contained in:
parent
cda03f7b67
commit
a7c94a8f21
@ -4,18 +4,23 @@ namespace App\Dto;
|
|||||||
|
|
||||||
readonly class SnipFilterRequest implements CachableDtoInterface
|
readonly class SnipFilterRequest implements CachableDtoInterface
|
||||||
{
|
{
|
||||||
|
public const string VISIBILITY_ALL = 'all';
|
||||||
|
public const string VISIBILITY_VISIBLE = 'visible';
|
||||||
|
public const string VISIBILITY_HIDDEN = 'hidden';
|
||||||
|
|
||||||
public const string SORT_NAME = 'name';
|
public const string SORT_NAME = 'name';
|
||||||
public const string SORT_DATE = 'date';
|
public const string SORT_DATE = 'date';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public bool $onlyVisible = true,
|
public ?string $visibility = self::VISIBILITY_VISIBLE,
|
||||||
public ?string $sort = self::SORT_DATE,
|
public ?string $sort = self::SORT_DATE,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'onlyVisible' => $this->onlyVisible,
|
'visibility' => $this->visibility,
|
||||||
|
'sort' => $this->sort,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -50,18 +50,29 @@ class SnipRepository extends ServiceEntityRepository
|
|||||||
->setParameter('user', $user)
|
->setParameter('user', $user)
|
||||||
;
|
;
|
||||||
|
|
||||||
$qb->andWhere('s.visible = :visible')
|
switch ($request->visibility) {
|
||||||
->setParameter('visible', $request->onlyVisible)
|
case SnipFilterRequest::VISIBILITY_ALL:
|
||||||
;
|
break;
|
||||||
|
case SnipFilterRequest::VISIBILITY_VISIBLE:
|
||||||
if ($request->sort === SnipFilterRequest::SORT_NAME) {
|
$qb->andWhere('s.visible = true');
|
||||||
$qb->orderBy('s.name', 'ASC');
|
break;
|
||||||
} elseif ($request->sort === SnipFilterRequest::SORT_DATE) {
|
case SnipFilterRequest::VISIBILITY_HIDDEN:
|
||||||
$qb->orderBy('s.createdAt', 'DESC');
|
$qb->andWhere('s.visible = false');
|
||||||
} else {
|
break;
|
||||||
throw new \InvalidArgumentException('Invalid sort option: ', $request->sort);
|
default:
|
||||||
|
throw new \InvalidArgumentException('Invalid visibility option: ', $request->visibility);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch ($request->sort) {
|
||||||
|
case SnipFilterRequest::SORT_NAME:
|
||||||
|
$qb->orderBy('s.name', 'ASC');
|
||||||
|
break;
|
||||||
|
case SnipFilterRequest::SORT_DATE:
|
||||||
|
$qb->orderBy('s.createdAt', 'DESC');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new \InvalidArgumentException('Invalid sort option: ', $request->sort);
|
||||||
|
}
|
||||||
return $qb->getQuery()->getResult();
|
return $qb->getQuery()->getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,23 +1,12 @@
|
|||||||
{% extends 'base/one.column.html.twig' %}
|
{% extends 'base/two.column.html.twig' %}
|
||||||
|
|
||||||
{% set title = 'My Snips' %}
|
{% set title = 'My Snips' %}
|
||||||
|
|
||||||
{% block body %}
|
{% block column1 %}
|
||||||
|
<h3>Snips</h3>
|
||||||
<a class="btn btn-success" href="{{ path('snip_new') }}">
|
<a class="btn btn-success" href="{{ path('snip_new') }}">
|
||||||
<i class="fa fa-plus"></i> Add
|
<i class="fa fa-plus"></i> Add
|
||||||
</a>
|
</a>
|
||||||
{% if request.onlyVisible %}
|
|
||||||
<a class="btn btn-secondary" href="{{ path('snip_index', {onlyVisible: false}) }}">Show hidden</a>
|
|
||||||
{% else %}
|
|
||||||
<a class="btn btn-secondary" href="{{ path('snip_index', {onlyVisible: true}) }}">Hide hidden</a>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if request.sort != 'name' %}
|
|
||||||
<a class="btn btn-secondary" href="{{ path('snip_index', {sort: 'name'}) }}">Sort by name</a>
|
|
||||||
{% endif %}
|
|
||||||
{% if request.sort != 'date' %}
|
|
||||||
<a class="btn btn-secondary" href="{{ path('snip_index', {sort: 'date'}) }}">Sort by date</a>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<br><br>
|
<br><br>
|
||||||
<div class="list-group">
|
<div class="list-group">
|
||||||
@ -31,3 +20,28 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block column2 %}
|
||||||
|
<h3>Filters</h3>
|
||||||
|
|
||||||
|
<h5>Ordering</h5>
|
||||||
|
<div class="list-group">
|
||||||
|
{% for sortOption in ['name', 'date'] %}
|
||||||
|
<a href="{{ path('snip_index', {sort: sortOption}) }}"
|
||||||
|
class="list-group-item list-group-item-action {% if sortOption is same as(request.sort) %}list-group-item-primary{% endif %}">
|
||||||
|
Sort by {{ sortOption|capitalize }}
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<h5>Visibility</h5>
|
||||||
|
<div class="list-group">
|
||||||
|
{% for visibilityOption in ['all', 'visible', 'hidden'] %}
|
||||||
|
<a href="{{ path('snip_index', {visibility: visibilityOption}) }}"
|
||||||
|
class="list-group-item list-group-item-action {% if request.visibility is same as(visibilityOption) %}list-group-item-primary{% endif %}">
|
||||||
|
Show {{ visibilityOption|capitalize }}
|
||||||
|
</a>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user