Skip to content

Commit

Permalink
fixes (#78)
Browse files Browse the repository at this point in the history
* fix: hide candidates on referendum

* migrate election type
  • Loading branch information
andreiio authored Nov 23, 2024
1 parent b6c20da commit 2cd220a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
12 changes: 6 additions & 6 deletions app/Enums/ElectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
use App\Concerns\Enums\Comparable;
use Filament\Support\Contracts\HasLabel;

enum ElectionType: string implements HasLabel
enum ElectionType: int implements HasLabel
{
use Arrayable;
use Comparable;

case PRESIDENTIAL = 'presidential';
case PARLIAMENTARY = 'parliamentary';
case EURO = 'euro';
case LOCAL = 'local';
case REFERENDUM = 'referendum';
case PRESIDENTIAL = 1;
case REFERENDUM = 2;
case PARLIAMENTARY = 3;
case LOCAL = 4;
case EURO = 5;

public function getLabel(): ?string
{
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Election.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected static function booted(): void
static::addGlobalScope('latest', function (Builder $query) {
return $query
->orderByDesc('year')
->orderByDesc('is_live');
->orderByDesc('is_live')
->orderBy('type');
});
}

Expand Down
3 changes: 1 addition & 2 deletions app/View/Components/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Timeline extends Component

public function __construct()
{
$this->years = Election::query()
->get()
$this->years = Election::all()
->groupBy([
'year',
fn (Election $election) => $election->type->getLabel(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use App\Enums\ElectionType;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('elections', function (Blueprint $table) {
$table->tinyInteger('new_type')->after('type');
});

$this->migrateData();

Schema::table('elections', function (Blueprint $table) {
$table->dropColumn('type');
$table->renameColumn('new_type', 'type');
});
}

protected function migrateData(): void
{
collect([
'presidential' => ElectionType::PRESIDENTIAL,
'referendum' => ElectionType::REFERENDUM,
'parliamentary' => ElectionType::PARLIAMENTARY,
'local' => ElectionType::LOCAL,
'euro' => ElectionType::EURO,
])->each(
fn (ElectionType $newType, string $oldType) => DB::table('elections')
->where('type', $oldType)
->update(['new_type' => $newType])
);
}
};
5 changes: 4 additions & 1 deletion resources/views/livewire/pages/election-turnouts.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@use('App\Enums\ElectionType')
@use('App\Livewire\Charts')
@use('Filament\Support\Colors\Color')

Expand Down Expand Up @@ -66,7 +67,9 @@
:election="$election"
show-embed />

<x-candidates.turnouts-table :items="$this->candidates" />
@if ($election->type->isNot(ElectionType::REFERENDUM))
<x-candidates.turnouts-table :items="$this->candidates" />
@endif

<livewire:news-feed :election="$election" />
</section>
Expand Down

0 comments on commit 2cd220a

Please sign in to comment.