Add new models and page for motions

This commit is contained in:
2025-12-27 16:51:57 +01:00
parent 3ef975ad39
commit 45fe15eef2
36 changed files with 3753 additions and 355 deletions

View 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');
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Livewire\Motion;
use App\Services\RiksdagenService;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\Computed;
use Livewire\Component;
class Show extends Component
{
public $motionId;
public $motion;
private RiksdagenService $service;
public function mount($motionId)
{
$this->motionId = $motionId;
$this->service = app(RiksdagenService::class);
$result = Cache::remember('motion_'.$motionId, 24 * 60 * 60, function () use ($motionId) {
return $this->service->getMotion($motionId);
});
$this->motion = $result->original->dokumentlista->dokument[0] ?? null;
}
#[Computed()]
public function riksdagenUrl()
{
return 'https://www.riksdagen.se/sv/dokument-lagar/dokument/'.$this->motionId;
}
#[Computed()]
public function authors()
{
if (! $this->motion || ! isset($this->motion->dokintressent->intressent)) {
return collect();
}
$intressenter = $this->motion->dokintressent->intressent;
if (! is_array($intressenter)) {
$intressenter = [$intressenter];
}
return collect($intressenter)->where('roll', 'undertecknare');
}
#[Computed()]
public function attachments()
{
if (! $this->motion || ! isset($this->motion->filbilaga->fil)) {
return collect();
}
$files = $this->motion->filbilaga->fil;
if (! is_array($files)) {
$files = [$files];
}
return collect($files);
}
public function render()
{
return view('livewire.motion.show')
->title(($this->motion->titel ?? 'Motion').' - Riksdagen App');
}
}