Skip to content

Commit

Permalink
Feature/fil001 147 filter on tags (#16)
Browse files Browse the repository at this point in the history
* FIL001-147 add tags filter

* FIL001-147 add tags filter buttons

* FIL001-147 change buttons and use relation as offset

* FIL001-147 put relation in string

* Update phpstan config

---------

Co-authored-by: thibautdeg <[email protected]>
Co-authored-by: Jyrki De Neve <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2024
1 parent 4add0fb commit e992e72
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false

17 changes: 17 additions & 0 deletions resources/views/livewire/resource-picker.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ class="block w-full transition text-sm py-1 !ps-8 !pe-3 duration-75 border-none
</svg>
</label>
</div>
@if(!empty($relationFilters))
@foreach($relationFilters as $relation => $filters)
<div class="flex-1 mb-4">
<div class="text-sm font-medium mb-2 dark:text-gray-200">Filter by {{ $relation }}</div>
<div class="flex flex-wrap gap-2">
@foreach($filters as $filter)
<x-filament::badge
:color="$this->isRelationFilterActive($relation, $filter['id']) ? 'info' : 'gray'"
wire:click="toggleRelation('{{$relation}}', {{ $filter['id'] }})"
>
{{ $filter['name'] }}
</x-filament::badge>
@endforeach
</div>
</div>
@endforeach
@endif
@endif
<div
class="overflow-hidden overflow-y-auto p-1"
Expand Down
2 changes: 2 additions & 0 deletions resources/views/picker.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

:min-items="$minItems ?? null"
:max-items="$maxItems ?? null"

:relation-filters="$relationFilters ?? null"
/>
53 changes: 53 additions & 0 deletions src/Livewire/ResourcePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class ResourcePicker extends Component

public ?int $maxItems = null;

public ?array $relationFilters = null;

public string $search = '';

public array $selectedRelations = [];

public function mount(
string $resourceClass,
string $displayType,
Expand All @@ -46,6 +50,7 @@ public function mount(
int $gridColumns,
?int $minItems = null,
?int $maxItems = null,
?array $relationFilters = null,
) {
$this->resourceClass = $resourceClass;
$this->displayType = $displayType;
Expand All @@ -58,6 +63,7 @@ public function mount(
$this->gridColumns = $gridColumns;
$this->minItems = $minItems;
$this->maxItems = $maxItems;
$this->relationFilters = $relationFilters;

$this->items = $this->getItems();
}
Expand All @@ -73,6 +79,7 @@ public function render()
public function getItems(int $offset = 0)
{
return ResourceQuery::get($this->resourceClass, $this->search)
->tap(fn ($query) => $this->searchRelations($query))
->latest()
->offset($offset)
->limit(24)
Expand All @@ -82,6 +89,7 @@ public function getItems(int $offset = 0)
public function getItemCount()
{
return ResourceQuery::get($this->resourceClass, $this->search)
->tap(fn ($query) => $this->searchRelations($query))
->count();
}

Expand All @@ -97,4 +105,49 @@ public function updatedSearch(): void
{
$this->items = $this->getItems();
}

public function toggleRelation($relation, $id): void
{
$this->setSelectedRelation($relation, $id);

$this->items = $this->getItems();
}

protected function searchRelations($query)
{
return collect($this->relationFilters)->each(function ($filters, $relation) use ($query) {
if (! empty($this->getSelectedRelations($relation))) {
$query->whereHas($relation, function ($q) use ($relation) {
$q->whereIn('id', $this->getSelectedRelations($relation));
});
}
});
}

protected function setSelectedRelation($relation, $id): void
{
if (! array_key_exists($relation, $this->selectedRelations)) {
$this->selectedRelations[$relation][] = $id;

return;
}

if (! in_array($id, $this->selectedRelations[$relation])) {
$this->selectedRelations[$relation][] = $id;

return;
}

$this->selectedRelations[$relation] = array_diff($this->selectedRelations[$relation], [$id]);
}

protected function getSelectedRelations($relation)
{
return $this->selectedRelations[$relation] ?? [];
}

public function isRelationFilterActive($relation, $id): bool
{
return in_array($id, $this->getSelectedRelations($relation));
}
}

0 comments on commit e992e72

Please sign in to comment.