Add new models and page for motions
This commit is contained in:
@@ -2,11 +2,12 @@
|
||||
|
||||
namespace App\Livewire\Person;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Services\RiksdagenService;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Asantibanez\LivewireCharts\Models\PieChartModel;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Lazy;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Lazy()]
|
||||
class Show extends Component
|
||||
@@ -17,9 +18,15 @@ class Show extends Component
|
||||
|
||||
public $votes = [];
|
||||
|
||||
public $motions = [];
|
||||
|
||||
public $votesByYear = [];
|
||||
|
||||
public $selectedYear;
|
||||
public $motionsByYear = [];
|
||||
|
||||
public $votesSelectedYear;
|
||||
|
||||
public $motionsSelectedYear;
|
||||
|
||||
public $selectedUppdragTab = 'current';
|
||||
|
||||
@@ -27,47 +34,87 @@ class Show extends Component
|
||||
|
||||
public $previousUppdrag = [];
|
||||
|
||||
private RiksdagenService $service;
|
||||
|
||||
public function mount($personId)
|
||||
{
|
||||
$this->personId = $personId;
|
||||
$service = app(RiksdagenService::class);
|
||||
$result = $service->searchPerson(mp_id: $personId);
|
||||
$this->service = app(RiksdagenService::class);
|
||||
$result = Cache::remember('person_'.$personId, 24 * 60 * 60, function () use ($personId) {
|
||||
return $this->service->searchPerson(mp_id: $personId);
|
||||
});
|
||||
$this->person = $result->original->personlista->person ?? null;
|
||||
|
||||
$this->getPersonVotes();
|
||||
$this->getPersonMotions();
|
||||
$this->groupUppdrag();
|
||||
}
|
||||
|
||||
#[Computed()]
|
||||
public function riksdagenUrl()
|
||||
{
|
||||
return "https://www.riksdagen.se/sv/ledamoter-partier/ledamot/" . $this->person->tilltalsnamn . "-" . $this->person->efternamn . "_" . $this->personId;
|
||||
return 'https://www.riksdagen.se/sv/ledamoter-partier/ledamot/'.$this->person->tilltalsnamn.'-'.$this->person->efternamn.'_'.$this->personId;
|
||||
}
|
||||
|
||||
public function getPersonVotes()
|
||||
{
|
||||
$service = app(RiksdagenService::class);
|
||||
$result = $service->searchVotes(mp_id: $this->personId);
|
||||
$result = Cache::remember('person_votes_'.$this->personId, 24 * 60 * 60, function () {
|
||||
return $this->service->searchVotes(mp_id: $this->personId);
|
||||
});
|
||||
|
||||
$votesList = $result->original->voteringlista->votering ?? [];
|
||||
|
||||
// Group votes by year (from rm field like 2025/26)
|
||||
// Group votes by "rm" field (e.g., 2024/25)
|
||||
foreach ($votesList as $vote) {
|
||||
$year = explode('/', $vote->rm)[0]; // Extract year from rm like "2025/26"
|
||||
if (!isset($this->votesByYear[$year])) {
|
||||
$this->votesByYear[$year] = [];
|
||||
$rm = $vote->rm;
|
||||
if (! isset($this->votesByYear[$rm])) {
|
||||
$this->votesByYear[$rm] = [];
|
||||
}
|
||||
$this->votesByYear[$year][] = $vote;
|
||||
$this->votesByYear[$rm][] = $vote;
|
||||
}
|
||||
|
||||
// Set default selected year to the most recent
|
||||
if (!empty($this->votesByYear)) {
|
||||
$this->selectedYear = max(array_keys($this->votesByYear));
|
||||
if (! empty($this->votesByYear)) {
|
||||
// Sort keys as strings (e.g., 2024/25, 2023/24, ...)
|
||||
$years = array_keys($this->votesByYear);
|
||||
rsort($years, SORT_STRING);
|
||||
$this->votesSelectedYear = $years[0];
|
||||
}
|
||||
}
|
||||
|
||||
public function selectYear($year)
|
||||
public function getPersonMotions()
|
||||
{
|
||||
$this->selectedYear = $year;
|
||||
$result = Cache::remember('person_motions_'.$this->personId, 24 * 60 * 60, function () {
|
||||
return $this->service->getPersonMotions(mp_id: $this->personId);
|
||||
});
|
||||
$motionsList = $result->original->dokumentlista->dokument ?? [];
|
||||
|
||||
// Group votes by "rm" field (e.g., 2024/25)
|
||||
foreach ($motionsList as $motion) {
|
||||
$rm = $motion->rm;
|
||||
if (! isset($this->motionsByYear[$rm])) {
|
||||
$this->motionsByYear[$rm] = [];
|
||||
}
|
||||
$this->motionsByYear[$rm][] = $motion;
|
||||
}
|
||||
|
||||
// Set default selected year to the most recent
|
||||
if (! empty($this->motionsByYear)) {
|
||||
// Sort keys as strings (e.g., 2024/25, 2023/24, ...)
|
||||
$years = array_keys($this->motionsByYear);
|
||||
rsort($years, SORT_STRING);
|
||||
$this->motionsSelectedYear = $years[0];
|
||||
}
|
||||
}
|
||||
|
||||
public function selectVotesYear($year)
|
||||
{
|
||||
$this->votesSelectedYear = $year;
|
||||
}
|
||||
|
||||
public function selectMotionsYear($year)
|
||||
{
|
||||
$this->motionsSelectedYear = $year;
|
||||
}
|
||||
|
||||
public function selectUppdragTab($tab)
|
||||
@@ -77,7 +124,7 @@ class Show extends Component
|
||||
|
||||
public function groupUppdrag()
|
||||
{
|
||||
if (!$this->person || !isset($this->person->personuppdrag->uppdrag)) {
|
||||
if (! $this->person || ! isset($this->person->personuppdrag->uppdrag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,11 +145,11 @@ class Show extends Component
|
||||
}
|
||||
|
||||
// Sort by date (most recent first)
|
||||
usort($this->currentUppdrag, function($a, $b) {
|
||||
usort($this->currentUppdrag, function ($a, $b) {
|
||||
return \Carbon\Carbon::parse($b->from)->timestamp - \Carbon\Carbon::parse($a->from)->timestamp;
|
||||
});
|
||||
|
||||
usort($this->previousUppdrag, function($a, $b) {
|
||||
usort($this->previousUppdrag, function ($a, $b) {
|
||||
return \Carbon\Carbon::parse($b->tom ?: $b->from)->timestamp - \Carbon\Carbon::parse($a->tom ?: $a->from)->timestamp;
|
||||
});
|
||||
}
|
||||
@@ -110,16 +157,16 @@ class Show extends Component
|
||||
#[Computed()]
|
||||
public function votingStatistics()
|
||||
{
|
||||
if (!$this->selectedYear || !isset($this->votesByYear[$this->selectedYear])) {
|
||||
if (! $this->votesSelectedYear || ! isset($this->votesByYear[$this->votesSelectedYear])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$votes = $this->votesByYear[$this->selectedYear];
|
||||
$votes = $this->votesByYear[$this->votesSelectedYear];
|
||||
$statistics = [];
|
||||
|
||||
foreach ($votes as $vote) {
|
||||
$voteType = $vote->rost;
|
||||
if (!isset($statistics[$voteType])) {
|
||||
if (! isset($statistics[$voteType])) {
|
||||
$statistics[$voteType] = 0;
|
||||
}
|
||||
$statistics[$voteType]++;
|
||||
@@ -133,8 +180,8 @@ class Show extends Component
|
||||
{
|
||||
$statistics = $this->votingStatistics;
|
||||
|
||||
$pieChart = (new PieChartModel())
|
||||
->setTitle('Voteringsstatistik för ' . $this->selectedYear)
|
||||
$pieChart = (new PieChartModel)
|
||||
->setTitle('Voteringsstatistik för '.$this->votesSelectedYear)
|
||||
->setAnimated(true)
|
||||
->withDataLabels();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user