84 lines
3.2 KiB
PHP
84 lines
3.2 KiB
PHP
<?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()->where('client_id', auth()->user()->client_id)->orwhereIn('id', [1, 2]))
|
|
->description('Send a message to one of the users listed')
|
|
->columns([
|
|
TextColumn::make('name')->icon('phosphor-user-duotone')->searchable(),
|
|
TextColumn::make('email')->icon('phosphor-envelope-duotone'),
|
|
])
|
|
->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('Notification from '.$record->name)
|
|
->icon('phosphor-envelope-duotone')
|
|
->body($data['message'])
|
|
->sendToDatabase($record);
|
|
|
|
Notification::make()
|
|
->title('Message send successfully')
|
|
->success()
|
|
->send();
|
|
}),
|
|
|
|
])
|
|
->toolbarActions([
|
|
// ...
|
|
]);
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.user-notifications');
|
|
}
|
|
}
|