Add new models and page for motions
This commit is contained in:
146
app/Livewire/Motion/Search.php
Normal file
146
app/Livewire/Motion/Search.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Motion;
|
||||
|
||||
use App\Enums\Parties as PartyEnum;
|
||||
use App\Services\RiksdagenService;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
class Search extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
#[Url]
|
||||
public $query = '';
|
||||
|
||||
#[Url]
|
||||
public $party = '';
|
||||
|
||||
#[Url]
|
||||
public $dateInterval = '';
|
||||
|
||||
private $paginator;
|
||||
|
||||
public $loading = false;
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->searchMotions();
|
||||
}
|
||||
|
||||
public function updatedQuery()
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->searchMotions();
|
||||
}
|
||||
|
||||
public function updatedParty()
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->searchMotions();
|
||||
}
|
||||
|
||||
public function updatedDateInterval()
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->searchMotions();
|
||||
}
|
||||
|
||||
public function searchMotions()
|
||||
{
|
||||
if (empty($this->query) && empty($this->party) && empty($this->dateInterval)) {
|
||||
$this->paginator = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->loading = true;
|
||||
|
||||
$cacheKey = 'motions_'.md5($this->query.$this->party.$this->dateInterval.$this->getPage());
|
||||
|
||||
$result = Cache::remember($cacheKey, 60 * 5, function () {
|
||||
return app(RiksdagenService::class)->searchMotions(
|
||||
query: $this->query,
|
||||
party: $this->party,
|
||||
dateInterval: $this->dateInterval,
|
||||
page: $this->getPage()
|
||||
);
|
||||
});
|
||||
|
||||
$motions = $result->original->dokumentlista->dokument ?? [];
|
||||
$totalResults = (int) ($result->original->dokumentlista->{'@traffar'} ?? 0);
|
||||
$perPage = 20; // Default items per page from Riksdag API
|
||||
$currentPage = $this->getPage();
|
||||
|
||||
$this->paginator = new LengthAwarePaginator(
|
||||
items: collect($motions),
|
||||
total: $totalResults,
|
||||
perPage: $perPage,
|
||||
currentPage: $currentPage,
|
||||
options: [
|
||||
'path' => request()->url(),
|
||||
'pageName' => 'page',
|
||||
]
|
||||
);
|
||||
|
||||
$this->loading = false;
|
||||
}
|
||||
|
||||
#[Computed()]
|
||||
public function motions()
|
||||
{
|
||||
return $this->paginator ? collect($this->paginator->items()) : collect();
|
||||
}
|
||||
|
||||
#[Computed()]
|
||||
public function totalResults()
|
||||
{
|
||||
return $this->paginator ? $this->paginator->total() : 0;
|
||||
}
|
||||
|
||||
public function clearFilters()
|
||||
{
|
||||
$this->query = '';
|
||||
$this->party = '';
|
||||
$this->dateInterval = '';
|
||||
$this->resetPage();
|
||||
$this->searchMotions();
|
||||
}
|
||||
|
||||
#[Computed()]
|
||||
public function parties()
|
||||
{
|
||||
return collect(PartyEnum::cases())->mapWithKeys(function ($party) {
|
||||
return [$party->value => $party->label()];
|
||||
});
|
||||
}
|
||||
|
||||
#[Computed()]
|
||||
public function dateIntervals()
|
||||
{
|
||||
return [
|
||||
'2025/26' => '2025/26',
|
||||
'2024/25' => '2024/25',
|
||||
'2023/24' => '2023/24',
|
||||
'2022/23' => '2022/23',
|
||||
'2021/22' => '2021/22',
|
||||
'2020/21' => '2020/21',
|
||||
'2019/20' => '2019/20',
|
||||
'2018/19' => '2018/19',
|
||||
];
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$this->searchMotions();
|
||||
|
||||
return view('livewire.motion.search', [
|
||||
'paginatedMotions' => $this->paginator,
|
||||
])->title('Sök Motioner - Riksdagen App');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user