Skip to content

Commit

Permalink
Merge pull request #198 from philipsorensen/master
Browse files Browse the repository at this point in the history
Making the labels in the sidebar usable as search filters
  • Loading branch information
ash-jc-allen authored Aug 21, 2024
2 parents dc3bcb3 + bf4d4d0 commit 366b248
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This is the official repository for [Find A PR](https://findapr.io/). Find A PR

The following tools are required in order to start the installation and run the project locally.

- PHP 8.1
- PHP 8.2
- [Composer](https://getcomposer.org/download/)

## Installation
Expand Down
29 changes: 29 additions & 0 deletions app/Livewire/ListIssues.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ final class ListIssues extends Component

public ?string $sort = null;

public array $searchLabels = [];

public ?string $searchTerm = null;

public bool $shouldDisplayFirstTimeNotice;
Expand Down Expand Up @@ -106,6 +108,7 @@ public function render(): View

$issues = $this->originalIssues
->filter(fn (Issue $issue): bool => $this->showIgnoredIssues === in_array($issue->url, $this->ignoredUrls, true))
->when($this->searchLabels, $this->applySearchLabel())
->when($this->searchTerm, $this->applySearch())
->when($this->sort, $this->applySort());

Expand Down Expand Up @@ -139,6 +142,32 @@ private function applySearch(): \Closure
};
}

private function applySearchLabel(): \Closure
{
return static function (Collection $issues, array $searchLabels): Collection {
return $issues->filter(function (Issue $issue) use ($searchLabels): bool {
foreach ($searchLabels as $searchLabel) {
if (collect($issue->labels)->contains('name', $searchLabel)) {
return true;
}
}

return false;
});
};
}

public function toggleSearchLabel(string $label): void
{
$key = array_search($label, $this->searchLabels, strict: true);

if ($key !== false) {
unset($this->searchLabels[$key]);
} else {
$this->searchLabels[] = $label;
}
}

private function applySort(): \Closure
{
return function (Collection $issues): Collection {
Expand Down
11 changes: 9 additions & 2 deletions resources/views/components/side-bar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ class="w-full rounded-md border border-gray-300 shadow-sm px-2 py-2 bg-white tex

@foreach($labels as $name => $count)
<div>
<p class="inline-block items-center px-3 py-1 my-0.5 space-x-1 rounded text-xs font-bold border bg-gray-400 dark:bg-gray-600 text-white">
<button
@class([
'inline-block items-center px-3 py-1 my-0.5 rounded text-xs space-x-1 font-bold text-white hover:bg-green-500 dark:hover:bg-green-700 transition ease-out',
'bg-green-400 dark:bg-green-600' => in_array($name, $this->searchLabels, strict: true),
'bg-gray-400 dark:bg-gray-600' => ! in_array($name, $this->searchLabels, strict: true),
])
wire:click="toggleSearchLabel('{{ $name }}')"
>
<span>{{ $name }}</span>
<span>({{$count}})</span>
</p>
</button>
</div>
@endforeach
</div>
Expand Down

0 comments on commit 366b248

Please sign in to comment.