add notifications

This commit is contained in:
salam committed 2026-02-05 13:23:59 +01:00
1 parent 1913eabc00
commit 01be3b34ad
26 files changed
+319 -149

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
<?php
namespace App\Livewire;
use App\Mail\UserNotification;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Support\Enums\Width;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Mail;
use Livewire\Component;
class UserNotifications extends Component implements HasActions, HasSchemas, HasTable
{
use InteractsWithActions;
use InteractsWithSchemas;
use InteractsWithTable;
public function table(Table $table): Table
{
return $table
->query(User::query())
->columns([
TextColumn::make('name')->icon('phosphor-user-duotone')->searchable(),
TextColumn::make('email')->icon('phosphor-envelope-duotone'),
])
->filters([
// ...
])
->recordActions([
Action::make('notification')
->icon('phosphor-note-pencil-duotone')
->tooltip('Create a notification')
->modalHeading(fn ($record) => 'Send a notification to '.$record->name)
->iconButton()
->modalWidth(Width::Large)
// ->modalDescription('Send a notification')
->modalSubmitActionLabel('Send')
->modalIcon('phosphor-envelope-duotone')
->schema([
Textarea::make('message')
->rows(4),
Toggle::make('notify_by_email')->label('Send also as email')
// ->belowContent('Notify me also by e-mail')
->onColor('info')
->offColor('gray'),
])->action(function (array $data, $record) {
if ($data['notify_by_email']) {
Mail::to($record)
->send(new UserNotification($data['message']));
}
Notification::make()
->title('Saved successfully')
->icon('phosphor-paper-plane-right')
->body($data['message'])
->sendToDatabase($record);
}),
])
->toolbarActions([
// ...
]);
}
public function render(): View
{
return view('livewire.user-notifications');
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class UserNotification extends Mailable
{
use Queueable, SerializesModels;
protected string $message;
/**
* Create a new message instance.
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'NUH-Odds Notification',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'mail.user.notified',
with: [
'username' => auth()->user()->name,
'message' => $this->message,
],
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}