Skip to content

Commit

Permalink
Use Import/Export Actions in FlightResource
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed Oct 8, 2023
1 parent 8c70856 commit 67da786
Showing 1 changed file with 5 additions and 38 deletions.
43 changes: 5 additions & 38 deletions app/Filament/Resources/FlightResource/Pages/ListFlights.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace App\Filament\Resources\FlightResource\Pages;

use App\Filament\Actions\ExportAction;
use App\Filament\Actions\ImportAction;
use App\Filament\Resources\FlightResource;
use App\Repositories\FlightRepository;
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 ListFlights extends ListRecords
{
Expand All @@ -21,36 +16,8 @@ class ListFlights extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\Action::make('export')->label('Export to CSV')
->action(function (): BinaryFileResponse {
$exporter = app(ExportService::class);
$flightRepo = app(FlightRepository::class);

$where = [];
$file_name = 'flights.csv';
$flights = $flightRepo->where($where)->orderBy('airline_id')->orderBy('flight_number')->orderBy('route_code')->orderBy('route_leg')->get();

$path = $exporter->exportFlights($flights);

return response()->download($path, $file_name, ['content-type' => 'text/csv'])->deleteFileAfterSend(true);
})->after(function (): void {
Notification::make()
->title('Flights 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 Flights')->default(false),
])->action(function (array $data): void {
$importSvc = app(ImportService::class);

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

$importSvc->importFlights($path, $data['delete']);
}),
ExportAction::make('export')->arguments(['resourceTitle' => 'flights', 'exportType' => ImportExportType::FLIGHTS]),
ImportAction::make('import')->arguments(['resourceTitle' => 'flights', 'importType' => ImportExportType::FLIGHTS]),
Actions\CreateAction::make()->label('Add Flight'),
];
}
Expand Down

0 comments on commit 67da786

Please sign in to comment.