clusev/app/Livewire/Modals/ConfirmAction.php

96 lines
2.7 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\AuditEvent;
use Illuminate\Support\Facades\Auth;
use LivewireUI\Modal\ModalComponent;
/**
* Generic R5 confirmation dialog for destructive / stateful actions.
*
* The opener passes copy + an audit descriptor + a follow-up event. On confirm
* we persist exactly one AuditEvent and re-dispatch the opener's event so the
* originating page can apply its own state change. The modal stays
* domain-agnostic and is reused for every confirm in the app (R5).
*/
class ConfirmAction extends ModalComponent
{
public string $heading = '';
public string $body = '';
public string $confirmLabel = '';
public bool $danger = false;
public string $icon = '';
/** Audit descriptor — one row written on confirm. */
public string $auditAction = '';
public ?string $auditTarget = null;
public ?int $serverId = null;
/** Follow-up event re-dispatched to the page after a successful confirm. */
public string $event = '';
/** @var array<string, mixed> */
public array $params = [];
/** Toast shown on confirm. Pass '' to defer the notification to the handler
* (e.g. when the real outcome is only known after a remote command runs);
* leave null to fall back to the generic default toast. */
public ?string $notify = null;
public static function modalMaxWidth(): string
{
return 'md';
}
/** Apply localized fallbacks for any copy the opener did not pass. */
public function boot(): void
{
if ($this->heading === '') {
$this->heading = __('modals.confirm_action.default_heading');
}
if ($this->confirmLabel === '') {
$this->confirmLabel = __('common.confirm');
}
if ($this->notify === null) {
$this->notify = __('modals.confirm_action.default_notify');
}
}
public function confirm(): void
{
if ($this->auditAction !== '') {
AuditEvent::create([
'user_id' => Auth::id(),
'server_id' => $this->serverId,
'actor' => Auth::user()?->name ?? 'system',
'action' => $this->auditAction,
'target' => $this->auditTarget,
'ip' => request()->ip(),
]);
}
if ($this->event !== '') {
$this->dispatch($this->event, ...$this->params);
}
// Empty notify = the handler will report the real outcome itself.
if ($this->notify !== '') {
$this->dispatch('notify', message: $this->notify);
}
$this->closeModal();
}
public function render()
{
return view('livewire.modals.confirm-action');
}
}