Skip to content

Commit

Permalink
feat: mandates (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Dec 2, 2024
1 parent a586d4c commit 0172563
Show file tree
Hide file tree
Showing 30 changed files with 1,132 additions and 205 deletions.
165 changes: 83 additions & 82 deletions app/Filament/Admin/Resources/ElectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\Locality;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
Expand All @@ -28,7 +29,6 @@
use Filament\Infolists\Infolist;
use Filament\Pages\SubNavigationPosition;
use Filament\Resources\Resource;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
Expand Down Expand Up @@ -66,98 +66,99 @@ public static function getPluralModelLabel(): string
public static function form(Form $form): Form
{
return $form
->columns(2)
->schema([
Forms\Components\Section::make()
->maxWidth(MaxWidth::ThreeExtraLarge)
->columns(2)
->schema([
Select::make('type')
->label(__('app.field.type'))
->options(ElectionType::options())
->enum(ElectionType::class)
->required(),

DatePicker::make('date')
->label(__('app.field.date'))
->default(today())
->required(),

TextInput::make('title')
->label(__('app.field.title'))
->required(),
Select::make('type')
->label(__('app.field.type'))
->options(ElectionType::options())
->enum(ElectionType::class)
->required(),

TextInput::make('subtitle')
->label(__('app.field.subtitle'))
->nullable(),
DatePicker::make('date')
->label(__('app.field.date'))
->default(today())
->required(),

TextInput::make('slug')
->label(__('app.field.slug'))
->unique(ignoreRecord: true)
->required()
->columnSpanFull(),
TextInput::make('title')
->label(__('app.field.title'))
->required(),

Toggle::make('is_live')
->label(__('app.field.is_live'))
->default(false),
TextInput::make('subtitle')
->label(__('app.field.subtitle'))
->nullable(),

Toggle::make('is_visible')
->label(__('app.field.is_visible'))
->default(false),
TextInput::make('slug')
->label(__('app.field.slug'))
->unique(ignoreRecord: true)
->required()
->columnSpanFull(),

Toggle::make('has_lists')
->label(__('app.field.has_lists'))
->default(false),
Toggle::make('is_live')
->label(__('app.field.is_live'))
->default(false),

Select::make('properties.default_tab')
->label(__('app.field.default_tab'))
->options(DefaultElectionPage::options())
->enum(DefaultElectionPage::class)
->selectablePlaceholder(false)
Toggle::make('is_visible')
->label(__('app.field.is_visible'))
->default(false),

Toggle::make('has_lists')
->label(__('app.field.has_lists'))
->default(false),

Select::make('properties.default_tab')
->label(__('app.field.default_tab'))
->options(DefaultElectionPage::options())
->enum(DefaultElectionPage::class)
->selectablePlaceholder(false)
->nullable(),

Fieldset::make(__('app.field.default_place'))
->columnSpanFull()
->statePath('properties.default_place')
->columns(2)
->schema([
Select::make('level')
->label(__('app.field.level'))
->options(DataLevel::options())
->afterStateUpdated(fn (Set $set) => $set('country', null))
->enum(DataLevel::class)
->live()
->nullable(),

Forms\Components\Section::make(__('app.field.default_place'))
->columnSpanFull()
->statePath('properties.default_place')
->columns(2)
->schema([
Select::make('level')
->label(__('app.field.level'))
->options(DataLevel::options())
->afterStateUpdated(fn (Set $set) => $set('country', null))
->enum(DataLevel::class)
->live()
->nullable(),
Select::make('country')
->label(__('app.field.country'))
->options(Country::pluck('name', 'id'))
->hidden(fn (Get $get) => ! DataLevel::isValue($get('level'), DataLevel::DIASPORA))
->nullable(),

Forms\Components\Group::make([
Select::make('county')
->label(__('app.field.county'))
->options(County::pluck('name', 'id'))
->hidden(fn (Get $get) => ! DataLevel::isValue($get('level'), DataLevel::NATIONAL))
->afterStateUpdated(fn (Set $set) => $set('locality', null))
->live()
->nullable(),
Select::make('locality')
->label(__('app.field.locality'))
->options(fn (Get $get) => $get('county') ? Locality::where('county_id', $get('county'))->pluck('name', 'id') : [])
->hidden(fn (Get $get) => ! $get('county'))
->nullable(),
]),
]),

KeyValue::make('properties.tabs')
->label(__('app.field.tabs'))
->columnSpanFull()
Select::make('country')
->label(__('app.field.country'))
->options(Country::pluck('name', 'id'))
->hidden(fn (Get $get) => ! DataLevel::isValue($get('level'), DataLevel::DIASPORA))
->nullable(),

Toggle::make('properties.show_threshold')
->label(__('app.field.show_threshold'))
->default(false),
Forms\Components\Group::make([
Select::make('county')
->label(__('app.field.county'))
->options(County::pluck('name', 'id'))
->hidden(fn (Get $get) => ! DataLevel::isValue($get('level'), DataLevel::NATIONAL))
->afterStateUpdated(fn (Set $set) => $set('locality', null))
->live()
->nullable(),
Select::make('locality')
->label(__('app.field.locality'))
->options(fn (Get $get) => $get('county') ? Locality::where('county_id', $get('county'))->pluck('name', 'id') : [])
->hidden(fn (Get $get) => ! $get('county'))
->nullable(),
]),
]),

KeyValue::make('properties.tabs')
->label(__('app.field.tabs'))
->columnSpanFull()
->nullable(),

Toggle::make('properties.show_threshold')
->label(__('app.field.show_threshold'))
->default(false),

TextInput::make('properties.total_seats')
->label(__('app.field.total_seats'))
->nullable()
->numeric(),
]);
}

Expand Down
121 changes: 121 additions & 0 deletions app/Filament/Admin/Resources/MandateResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\MandateResource\Pages;
use App\Filament\Filters\LocationFilter;
use App\Models\Candidate;
use App\Models\Mandate;
use App\Models\Party;
use App\Tables\Columns\LocationColumn;
use Filament\Forms\Components\MorphToSelect;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class MandateResource extends Resource
{
protected static ?string $model = Mandate::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function getNavigationGroup(): ?string
{
return __('app.navigation.election_data');
}

public static function getModelLabel(): string
{
return __('app.mandate.label.singular');
}

public static function getPluralModelLabel(): string
{
return __('app.mandate.label.plural');
}

public static function form(Form $form): Form
{
return $form
->schema([
Select::make('county_id')
->relationship('county', 'name')
->label(__('app.field.county'))
->searchable()
->preload()
->live(),

Select::make('locality_id')
->relationship(
'locality',
'name',
fn (Builder $query, Get $get) => $query
->where('county_id', $get('county_id'))
)
->label(__('app.field.locality'))
->searchable(),

MorphToSelect::make('votable')
->types([
MorphToSelect\Type::make(Party::class)
->titleAttribute('name'),
MorphToSelect\Type::make(Candidate::class)
->titleAttribute('name'),
])
->searchable()
->required()
->columnSpanFull(),

TextInput::make('mandates')
->required()
->numeric()
->default(0)
->minValue(0)
->maxValue(255)
->columnSpanFull(),
]);
}

public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->with('county', 'locality'))
->columns([
LocationColumn::make('location'),

TextColumn::make('votable.name')
->label(__('app.field.candidate'))
->searchable(),

TextColumn::make('mandates')
->numeric()
->sortable(),

])
->filters([
LocationFilter::make()
->withoutCountry(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->paginated([10, 25, 50, 100])
->deferLoading();
}

public static function getPages(): array
{
return [
'index' => Pages\ManageMandates::route('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\MandateResource\Pages;

use App\Filament\Admin\Resources\MandateResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageMandates extends ManageRecords
{
protected static string $resource = MandateResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
6 changes: 5 additions & 1 deletion app/Filament/Admin/Resources/RecordResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\RecordResource\Pages;
use App\Filament\Filters\LocationFilter;
use App\Models\Record;
use App\Tables\Columns\LocationColumn;
use Filament\Resources\Resource;
Expand Down Expand Up @@ -40,6 +41,9 @@ public static function table(Table $table): Table
->columns([
LocationColumn::make('location'),

TextColumn::make('section')
->label(__('app.field.section')),

TextColumn::make('part'),

TextColumn::make('eligible_voters_total')
Expand Down Expand Up @@ -109,7 +113,7 @@ public static function table(Table $table): Table
->sortable(),
])
->filters([
//
LocationFilter::make(),
])
->paginated([10, 25, 50, 100])
->deferLoading()
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Admin/Resources/TurnoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\TurnoutResource\Pages;
use App\Filament\Filters\LocationFilter;
use App\Models\Turnout;
use App\Tables\Columns\LocationColumn;
use Filament\Resources\Resource;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static function table(Table $table): Table
->sortable(),
])
->filters([
//
LocationFilter::make(),
])
->paginated([10, 25, 50, 100])
->deferLoading();
Expand Down
Loading

0 comments on commit 0172563

Please sign in to comment.