Skip to content
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

Making the labels in the sidebar usable as search filters #198

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading