Skip to content

Commit d986ff7

Browse files
committed
Use tag input instead of text input
1 parent ad1e5f3 commit d986ff7

File tree

1 file changed

+62
-93
lines changed

1 file changed

+62
-93
lines changed

src/Resources/MailResource.php

Lines changed: 62 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
namespace Vormkracht10\FilamentMails\Resources;
44

5-
use Filament\Forms\Components\TextInput;
5+
use Filament\Tables;
6+
use Illuminate\View\View;
7+
use Filament\Tables\Table;
8+
use Illuminate\Support\Carbon;
9+
use Filament\Infolists\Infolist;
10+
use Filament\Resources\Resource;
11+
use Illuminate\Support\Collection;
12+
use Filament\Tables\Actions\Action;
13+
use Vormkracht10\Mails\Models\Mail;
614
use Filament\Infolists\Components\Grid;
7-
use Filament\Infolists\Components\RepeatableEntry;
8-
use Filament\Infolists\Components\Section;
915
use Filament\Infolists\Components\Tabs;
16+
use Vormkracht10\Mails\Enums\EventType;
17+
use Filament\Forms\Components\TagsInput;
18+
use Filament\Notifications\Notification;
19+
use Vormkracht10\Mails\Models\MailEvent;
20+
use Filament\Infolists\Components\Section;
21+
use Vormkracht10\Mails\Actions\ResendMail;
1022
use Filament\Infolists\Components\Tabs\Tab;
1123
use Filament\Infolists\Components\TextEntry;
1224
use Filament\Infolists\Components\ViewEntry;
13-
use Filament\Infolists\Infolist;
14-
use Filament\Notifications\Notification;
15-
use Filament\Resources\Resource;
16-
use Filament\Tables;
17-
use Filament\Tables\Actions\Action;
18-
use Filament\Tables\Table;
19-
use Illuminate\Support\Carbon;
20-
use Illuminate\Support\Collection;
21-
use Illuminate\View\View;
22-
use Vormkracht10\FilamentMails\Resources\MailResource\Pages\ListMails;
25+
use Filament\Infolists\Components\RepeatableEntry;
2326
use Vormkracht10\FilamentMails\Resources\MailResource\Pages\ViewMail;
27+
use Vormkracht10\FilamentMails\Resources\MailResource\Pages\ListMails;
2428
use Vormkracht10\FilamentMails\Resources\MailResource\Widgets\MailStatsWidget;
25-
use Vormkracht10\Mails\Actions\ResendMail;
26-
use Vormkracht10\Mails\Enums\EventType;
27-
use Vormkracht10\Mails\Models\Mail;
28-
use Vormkracht10\Mails\Models\MailEvent;
2929

3030
class MailResource extends Resource
3131
{
@@ -84,22 +84,22 @@ public static function infolist(Infolist $infolist): Infolist
8484
->label(__('Subject')),
8585
TextEntry::make('from')
8686
->label(__('From'))
87-
->getStateUsing(fn (Mail $record) => self::formatMailState($record->from)),
87+
->getStateUsing(fn(Mail $record) => self::formatMailState($record->from)),
8888
TextEntry::make('to')
8989
->label(__('Recipient'))
90-
->getStateUsing(fn (Mail $record) => self::formatMailState($record->to)),
90+
->getStateUsing(fn(Mail $record) => self::formatMailState($record->to)),
9191
TextEntry::make('cc')
9292
->label(__('CC'))
9393
->default('-')
94-
->getStateUsing(fn (Mail $record) => self::formatMailState($record->cc ?? [])),
94+
->getStateUsing(fn(Mail $record) => self::formatMailState($record->cc ?? [])),
9595
TextEntry::make('bcc')
9696
->label(__('BCC'))
9797
->default('-')
98-
->getStateUsing(fn (Mail $record) => self::formatMailState($record->bcc ?? [])),
98+
->getStateUsing(fn(Mail $record) => self::formatMailState($record->bcc ?? [])),
9999
TextEntry::make('reply_to')
100100
->default('-')
101101
->label(__('Reply To'))
102-
->getStateUsing(fn (Mail $record) => self::formatMailState($record->reply_to ?? [])),
102+
->getStateUsing(fn(Mail $record) => self::formatMailState($record->reply_to ?? [])),
103103
]),
104104
]),
105105
Tab::make(__('Statistics'))
@@ -171,11 +171,11 @@ public static function infolist(Infolist $infolist): Infolist
171171
TextEntry::make('type')
172172
->label(__('Type'))
173173
->badge()
174-
->url(fn (MailEvent $record) => route('filament.' . filament()->getCurrentPanel()?->getId() . '.resources.mails.events.view', [
174+
->url(fn(MailEvent $record) => route('filament.' . filament()->getCurrentPanel()?->getId() . '.resources.mails.events.view', [
175175
'record' => $record,
176176
'tenant' => filament()->getTenant()?->id,
177177
]))
178-
->color(fn (EventType $state): string => match ($state) {
178+
->color(fn(EventType $state): string => match ($state) {
179179
EventType::DELIVERED => 'success',
180180
EventType::CLICKED => 'clicked',
181181
EventType::OPENED => 'info',
@@ -189,7 +189,7 @@ public static function infolist(Infolist $infolist): Infolist
189189
return ucfirst($state->value);
190190
}),
191191
TextEntry::make('occurred_at')
192-
->url(fn (MailEvent $record) => route('filament.' . filament()->getCurrentPanel()?->getId() . '.resources.mails.events.view', [
192+
->url(fn(MailEvent $record) => route('filament.' . filament()->getCurrentPanel()?->getId() . '.resources.mails.events.view', [
193193
'record' => $record,
194194
'tenant' => filament()->getTenant()?->id,
195195
]))
@@ -219,7 +219,7 @@ public static function infolist(Infolist $infolist): Infolist
219219
->hiddenLabel()
220220
->label(__('HTML Content'))
221221
->extraAttributes(['class' => 'overflow-x-auto'])
222-
->formatStateUsing(fn (string $state, Mail $record): View => view(
222+
->formatStateUsing(fn(string $state, Mail $record): View => view(
223223
'filament-mails::mails.preview',
224224
['html' => $state, 'mail' => $record],
225225
)),
@@ -229,7 +229,7 @@ public static function infolist(Infolist $infolist): Infolist
229229
TextEntry::make('html')
230230
->hiddenLabel()
231231
->extraAttributes(['class' => 'overflow-x-auto'])
232-
->formatStateUsing(fn (string $state, Mail $record): View => view(
232+
->formatStateUsing(fn(string $state, Mail $record): View => view(
233233
'filament-mails::mails.html',
234234
['html' => $state, 'mail' => $record],
235235
))
@@ -260,12 +260,12 @@ public static function infolist(Infolist $infolist): Infolist
260260
TextEntry::make('attachments')
261261
->hiddenLabel()
262262
->label(__('Attachments'))
263-
->visible(fn (Mail $record) => $record->attachments->count() == 0)
263+
->visible(fn(Mail $record) => $record->attachments->count() == 0)
264264
->default(__('Email has no attachments')),
265265
RepeatableEntry::make('attachments')
266266
->hiddenLabel()
267267
->label(__('Attachments'))
268-
->visible(fn (Mail $record) => $record->attachments->count() > 0)
268+
->visible(fn(Mail $record) => $record->attachments->count() > 0)
269269
->schema([
270270
Grid::make(3)
271271
->schema([
@@ -277,7 +277,7 @@ public static function infolist(Infolist $infolist): Infolist
277277
->label(__('Mime Type')),
278278
ViewEntry::make('uuid')
279279
->label(__('Download'))
280-
->getStateUsing(fn ($record) => $record)
280+
->getStateUsing(fn($record) => $record)
281281
->view('filament-mails::mails.download'),
282282
]),
283283
]),
@@ -297,7 +297,7 @@ public static function table(Table $table): Table
297297
->label(__('Status'))
298298
->sortable()
299299
->badge()
300-
->color(fn (string $state): string => match ($state) {
300+
->color(fn(string $state): string => match ($state) {
301301
__('Soft Bounced') => 'warning',
302302
__('Hard Bounced') => 'danger',
303303
__('Complained') => 'danger',
@@ -317,27 +317,27 @@ public static function table(Table $table): Table
317317
Tables\Columns\IconColumn::make('attachments')
318318
->label('')
319319
->alignLeft()
320-
->getStateUsing(fn (Mail $record) => $record->attachments->count() > 0)
321-
->icon(fn (string $state): string => $state ? 'heroicon-o-paper-clip' : ''),
320+
->getStateUsing(fn(Mail $record) => $record->attachments->count() > 0)
321+
->icon(fn(string $state): string => $state ? 'heroicon-o-paper-clip' : ''),
322322
Tables\Columns\TextColumn::make('to')
323323
->label(__('Recipient'))
324324
->limit(50)
325-
->getStateUsing(fn (Mail $record) => self::formatMailState(emails: $record->to, mailOnly: true))
325+
->getStateUsing(fn(Mail $record) => self::formatMailState(emails: $record->to, mailOnly: true))
326326
->sortable()
327327
->searchable(),
328328
Tables\Columns\TextColumn::make('opens')
329329
->label(__('Opens'))
330-
->tooltip(fn (Mail $record) => __('Last opened at :date', ['date' => $record->last_opened_at?->format('d-m-Y H:i')]))
330+
->tooltip(fn(Mail $record) => __('Last opened at :date', ['date' => $record->last_opened_at?->format('d-m-Y H:i')]))
331331
->sortable(),
332332
Tables\Columns\TextColumn::make('clicks')
333333
->label(__('Clicks'))
334-
->tooltip(fn (Mail $record) => __('Last clicked at :date', ['date' => $record->last_clicked_at?->format('d-m-Y H:i')]))
334+
->tooltip(fn(Mail $record) => __('Last clicked at :date', ['date' => $record->last_clicked_at?->format('d-m-Y H:i')]))
335335
->sortable(),
336336
Tables\Columns\TextColumn::make('sent_at')
337337
->label(__('Sent At'))
338338
->dateTime('d-m-Y H:i')
339339
->since()
340-
->tooltip(fn (Mail $record) => $record->sent_at?->format('d-m-Y H:i'))
340+
->tooltip(fn(Mail $record) => $record->sent_at?->format('d-m-Y H:i'))
341341
->sortable()
342342
->searchable(),
343343
])
@@ -357,35 +357,16 @@ public static function table(Table $table): Table
357357
->modalDescription(__('Are you sure you want to resend this mail?'))
358358
->hiddenLabel()
359359
->tooltip(__('Resend'))
360-
->form([
361-
TextInput::make('to')
362-
->label(__('Recipient'))
363-
->helperText(__('You can add multiple email addresses separated by commas.'))
364-
->required(),
365-
TextInput::make('cc')
366-
->label(__('CC')),
367-
TextInput::make('bcc')
368-
->label(__('BCC')),
369-
])
360+
->form(self::getResendForm())
370361
->fillForm(function (Mail $record) {
371362
return [
372-
'to' => implode(', ', array_keys($record->to)),
373-
'cc' => is_array($record->cc) ? implode(', ', array_keys($record->cc)) : null,
374-
'bcc' => is_array($record->bcc) ? implode(', ', array_keys($record->bcc)) : null,
363+
'to' => array_keys($record->to),
364+
'cc' => is_array($record->cc) ? array_keys($record->cc) : null,
365+
'bcc' => is_array($record->bcc) ? array_keys($record->bcc) : null,
375366
];
376367
})
377368
->action(function (Mail $record, array $data) {
378-
$to = explode(',', $data['to']);
379-
380-
if ($data['cc']) {
381-
$data['cc'] = explode(',', $data['cc']);
382-
}
383-
384-
if ($data['bcc']) {
385-
$data['bcc'] = explode(',', $data['bcc']);
386-
}
387-
388-
(new ResendMail)->handle($record, $to, $data['cc'] ?? [], $data['bcc'] ?? []);
369+
(new ResendMail)->handle($record, $data['to'], $data['cc'] ?? [], $data['bcc'] ?? []);
389370

390371
Notification::make()
391372
->title(__('Mail will be resent in the background'))
@@ -400,38 +381,10 @@ public static function table(Table $table): Table
400381
->icon('heroicon-o-arrow-uturn-right')
401382
->requiresConfirmation()
402383
->modalDescription(__('Are you sure you want to resend the selected mails?'))
403-
->hiddenLabel()
404-
->tooltip(__('Resend'))
405-
->form([
406-
TextInput::make('to')
407-
->label(__('Recipient'))
408-
->helperText(__('You can add multiple email addresses separated by commas.'))
409-
->required(),
410-
TextInput::make('cc')
411-
->label(__('CC')),
412-
TextInput::make('bcc')
413-
->label(__('BCC')),
414-
])
415-
->fillForm(function () {
416-
return [
417-
'to' => '',
418-
'cc' => '',
419-
'bcc' => '',
420-
];
421-
})
384+
->form(self::getResendForm())
422385
->action(function (Collection $records, array $data) {
423386
foreach ($records as $record) {
424-
$to = explode(',', $data['to']);
425-
426-
if ($data['cc']) {
427-
$data['cc'] = explode(',', $data['cc']);
428-
}
429-
430-
if ($data['bcc']) {
431-
$data['bcc'] = explode(',', $data['bcc']);
432-
}
433-
434-
(new ResendMail)->handle($record, $to, $data['cc'] ?? [], $data['bcc'] ?? []);
387+
(new ResendMail)->handle($record, $data['to'], $data['cc'] ?? [], $data['bcc'] ?? []);
435388
}
436389

437390
Notification::make()
@@ -444,6 +397,22 @@ public static function table(Table $table): Table
444397
]);
445398
}
446399

400+
private static function getResendForm(): array
401+
{
402+
return [
403+
TagsInput::make('to')
404+
->placeholder(__('Recipient'))
405+
->label(__('Recipient'))
406+
->required(),
407+
TagsInput::make('cc')
408+
->placeholder(__('Recipient'))
409+
->label(__('CC')),
410+
TagsInput::make('bcc')
411+
->placeholder(__('Recipient'))
412+
->label(__('BCC')),
413+
];
414+
}
415+
447416
public static function getPages(): array
448417
{
449418
return [
@@ -455,8 +424,8 @@ public static function getPages(): array
455424
private static function formatMailState(array $emails, bool $mailOnly = false): string
456425
{
457426
return collect($emails)
458-
->mapWithKeys(fn ($value, $key) => [$key => $value ?? $key])
459-
->map(fn ($value, $key) => $mailOnly ? $key : ($value === null ? $key : "$value <$key>"))
427+
->mapWithKeys(fn($value, $key) => [$key => $value ?? $key])
428+
->map(fn($value, $key) => $mailOnly ? $key : ($value === null ? $key : "$value <$key>"))
460429
->implode(', ');
461430
}
462431

0 commit comments

Comments
 (0)