diff --git a/app/Filament/Admin/Resources/CandidateResource.php b/app/Filament/Admin/Resources/CandidateResource.php index 493c3dd..cf29457 100644 --- a/app/Filament/Admin/Resources/CandidateResource.php +++ b/app/Filament/Admin/Resources/CandidateResource.php @@ -5,7 +5,9 @@ namespace App\Filament\Admin\Resources; use App\Filament\Admin\Resources\CandidateResource\Pages; +use App\Filament\Imports\SimpleCandidateImporter; use App\Models\Candidate; +use Filament\Facades\Filament; use Filament\Forms\Components\ColorPicker; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; @@ -13,6 +15,7 @@ use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables; +use Filament\Tables\Actions\ImportAction; use Filament\Tables\Columns\ColorColumn; use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; use Filament\Tables\Columns\TextColumn; @@ -87,6 +90,14 @@ public static function table(Table $table): Table ->filters([ // ]) + ->headerActions([ + ImportAction::make() + ->importer(SimpleCandidateImporter::class) + ->options([ + 'election_id' => Filament::getTenant()->id, + 'candidate_list' => true, + ]), + ]) ->actions([ Tables\Actions\EditAction::make(), ]) diff --git a/app/Filament/Imports/SimpleCandidateImporter.php b/app/Filament/Imports/SimpleCandidateImporter.php index 6feedad..7f93261 100644 --- a/app/Filament/Imports/SimpleCandidateImporter.php +++ b/app/Filament/Imports/SimpleCandidateImporter.php @@ -21,6 +21,9 @@ public static function getColumns(): array ->requiredMapping() ->rules(['required', 'string', 'max:255']), + ImportColumn::make('acronym') + ->rules(['nullable', 'string', 'max:255']), + ImportColumn::make('color') ->rules(['nullable', 'string', 'hex_color']), ]; @@ -33,9 +36,10 @@ public function getJobRetryUntil(): ?CarbonInterface public function resolveRecord(): Candidate|Party { - static::$model = RecordService::isIndependentCandidate($this->data['name']) - ? Candidate::class - : Party::class; + static::$model = Party::class; + if (RecordService::isIndependentCandidate($this->data['name']) || $this->options['candidate_list']) { + static::$model = Candidate::class; + } return static::getModel()::firstOrNew([ 'name' => $this->data['name'], diff --git a/app/Models/Party.php b/app/Models/Party.php index d68fcc8..d992c4d 100644 --- a/app/Models/Party.php +++ b/app/Models/Party.php @@ -37,7 +37,9 @@ class Party extends Model implements HasMedia, HasDisplayName public static function booted(): void { static::creating(function (Party $party) { - if (blank($party->acronym)) { + + if (empty($party->acronym)) { + dd('here', $party); $party->acronym = Str::initials($party->name); } }); diff --git a/resources/views/livewire/pages/election-turnouts.blade.php b/resources/views/livewire/pages/election-turnouts.blade.php index 2794e4c..1e0c27f 100644 --- a/resources/views/livewire/pages/election-turnouts.blade.php +++ b/resources/views/livewire/pages/election-turnouts.blade.php @@ -70,7 +70,7 @@ :election="$election" show-embed /> - @if ($election->type->isNot(ElectionType::REFERENDUM)) + @if ($election->type->isNot(ElectionType::REFERENDUM) && $this->candidates->isNotEmpty()) @endif