Skip to content

Commit

Permalink
Use Import/Export Actions in FareResource
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed Oct 8, 2023
1 parent ea9f19a commit d7650be
Showing 1 changed file with 6 additions and 51 deletions.
57 changes: 6 additions & 51 deletions app/Filament/Resources/FareResource/Pages/ListFares.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace App\Filament\Resources\FareResource\Pages;

use App\Filament\Actions\ExportAction;
use App\Filament\Actions\ImportAction;
use App\Filament\Resources\FareResource;
use App\Repositories\FareRepository;
use App\Services\ExportService;
use App\Services\ImportService;
use App\Models\Enums\ImportExportType;
use Filament\Actions;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ListFares extends ListRecords
{
Expand All @@ -21,49 +16,9 @@ class ListFares extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\Action::make('export')->label('Export to CSV')
->action(function (): BinaryFileResponse {
$exporter = app(ExportService::class);
$fareRepo = app(FareRepository::class);
$fares = $fareRepo->all();

$path = $exporter->exportFares($fares);
return response()->download($path, 'fares.csv', ['content-type' => 'text/csv'])->deleteFileAfterSend(true);
})->after(function (): void {
Notification::make()
->title('Fares Exported')
->success()
->send();
}),
Actions\Action::make('import')->label('Import from CSV')
->form([
FileUpload::make('importFile')->acceptedFileTypes(['text/csv'])->disk('local')->directory('import'),
Toggle::make('delete')->label('Delete Previous Fares')->default(false),
])->action(function (array $data, Actions\Action $action): void {
$importSvc = app(ImportService::class);

$path = storage_path('app/'.$data['importFile']);
Log::info('Uploaded Fare import file to '.$path);

$logs = $importSvc->importFares($path, $data['delete']);

session(['logs' => $logs]);

if (count($logs['errors']) === 0) {
Notification::make()
->title('Fares Imported Successfully')
->success()
->send();
} else {
Notification::make()
->title('There were '.count($logs['errors']).' errors importing the fares')
->body(implode('<br>', $logs['errors']))
->persistent()
->danger()
->send();
}
}),
Actions\CreateAction::make()->label('Add Fare'),
ExportAction::make('export')->arguments(['resourceTitle' => 'fares', 'exportType' => ImportExportType::FARES]),
ImportAction::make('import')->arguments(['resourceTitle' => 'fares', 'importType' => ImportExportType::FARES]),
Actions\CreateAction::make()->label('Add Fare')->icon('heroicon-o-plus-circle'),
];
}
}

0 comments on commit d7650be

Please sign in to comment.