Skip to content

Commit

Permalink
Added previous/next buttons for view page
Browse files Browse the repository at this point in the history
  • Loading branch information
marcogermani87 committed Jul 26, 2024
1 parent c00498a commit 9fbb7a7
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 1 deletion.
2 changes: 2 additions & 0 deletions resources/lang/de/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => 'Anhang konnte nicht heruntergeladen werden!',
'add_attachments' => 'Anhänge hinzufügen',
'attachments_number' => ':count Anhang|:count Anhänge',
'previous' => 'Vorherige',
'next' => 'Nächste',
];
2 changes: 2 additions & 0 deletions resources/lang/en/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => 'Unable to download attachment!',
'add_attachments' => 'Add attachments',
'attachments_number' => ':count attachment|:count attachments',
'previous' => 'Previous',
'next' => 'Next',
];
2 changes: 2 additions & 0 deletions resources/lang/it/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => "Impossibile scaricare l'allegato!",
'add_attachments' => 'Aggiungi allegati',
'attachments_number' => ':count allegato|:count allegati',
'previous' => 'Precedente',
'next' => 'Prossimo',
];
2 changes: 2 additions & 0 deletions resources/lang/nl/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => 'Kan bijlage niet downloaden!',
'add_attachments' => 'Bijlagen toevoegen',
'attachments_number' => ':count bijlage|:count bijlagen',
'previous' => 'Vorig',
'next' => 'Volgende',
];
2 changes: 2 additions & 0 deletions resources/lang/pt/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => 'Não foi possível baixar o anexo!',
'add_attachments' => 'Adicionar anexos',
'attachments_number' => ':count anexo|:count anexos',
'previous' => 'Próximo',
'next' => 'Anterior',
];
2 changes: 2 additions & 0 deletions resources/lang/tr/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'download_attachment_error' => 'Eki indirme başarısız!',
'add_attachments' => 'Dosya ekle',
'attachments_number' => ':count ek|:count ek',
'previous' => 'Öncesi',
'next' => 'Sonraki',
];
Binary file modified screenshots/view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/Filament/Resources/Actions/NextAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RickDBCN\FilamentEmail\Filament\Resources\Actions;

use Filament\Actions\Action;

class NextAction extends Action
{
public static function getDefaultName(): ?string
{
return 'next';
}

protected function setUp(): void
{
parent::setUp();

$this->hiddenLabel()
->icon('heroicon-o-arrow-right')
->outlined()
->size('sm')
->tooltip(__('filament-email::filament-email.next'));
}
}
24 changes: 24 additions & 0 deletions src/Filament/Resources/Actions/PreviousAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RickDBCN\FilamentEmail\Filament\Resources\Actions;

use Filament\Actions\Action;

class PreviousAction extends Action
{
public static function getDefaultName(): ?string
{
return 'previous';
}

protected function setUp(): void
{
parent::setUp();

$this->hiddenLabel()
->icon('heroicon-o-arrow-left')
->outlined()
->size('sm')
->tooltip(__('filament-email::filament-email.previous'));
}
}
64 changes: 64 additions & 0 deletions src/Filament/Resources/Concernes/CanPaginateViewRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace RickDBCN\FilamentEmail\Filament\Resources\Concernes;

use Filament\Actions\Action;
use Illuminate\Database\Eloquent\Model;
use RickDBCN\FilamentEmail\Filament\Resources\Actions\NextAction;
use RickDBCN\FilamentEmail\Filament\Resources\Actions\PreviousAction;

trait CanPaginateViewRecord
{
protected function configureAction(Action $action): void
{
$this->configureActionRecord($action);

match (true) {
$action instanceof PreviousAction => $this->configurePreviousAction($action),
$action instanceof NextAction => $this->configureNextAction($action),
default => parent::configureAction($action),
};
}

protected function configurePreviousAction(Action $action): void
{
if ($this->getPreviousRecord()) {
$action->url(fn(): string => static::getResource()::getUrl('view', ['record' => $this->getPreviousRecord()]));
} else {
$action
->disabled()
->color('gray');
}
}

protected function configureNextAction(Action $action): void
{
if ($this->getNextRecord()) {
$action->url(fn(): string => static::getResource()::getUrl('view', ['record' => $this->getNextRecord()]));
} else {
$action
->disabled()
->color('gray');
}
}

protected function getPreviousRecord(): ?Model
{
return $this
->getRecord()
->where('created_at', '<', $this->getRecord()->created_at)
->where('id', '<>', $this->getRecord()->id)
->orderBy('created_at', 'desc')
->first();
}

protected function getNextRecord(): ?Model
{
return $this
->getRecord()
->where('created_at', '>', $this->getRecord()->created_at)
->where('id', '<>', $this->getRecord()->id)
->orderBy('created_at', 'asc')
->first();
}
}
13 changes: 12 additions & 1 deletion src/Filament/Resources/EmailResource/Pages/ViewEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use Illuminate\Support\Facades\Storage;
use RickDBCN\FilamentEmail\Filament\Resources\EmailResource;
use RickDBCN\FilamentEmail\Models\Email;
use RickDBCN\FilamentEmail\Filament\Resources\Actions\NextAction;
use RickDBCN\FilamentEmail\Filament\Resources\Actions\PreviousAction;
use RickDBCN\FilamentEmail\Filament\Resources\Concernes\CanPaginateViewRecord;

class ViewEmail extends ViewRecord
{
use InteractsWithActions;
use CanPaginateViewRecord;

public Email $email;

Expand Down Expand Up @@ -42,4 +45,12 @@ public function downloadAction(): Action
}
});
}

protected function getHeaderActions(): array
{
return [
PreviousAction::make(),
NextAction::make(),
];
}
}

0 comments on commit 9fbb7a7

Please sign in to comment.