- Introduced `Confirmation` trait for reusable confirmation modal logic in Livewire components. - Added `ConfirmationModal` Livewire component for handling confirmation prompts with customizable title, subtitle, description, and styling. - Updated `captainhook.json` to increase `subjectLength` limit. - Integrated confirmation modal in the sidebar layout with `<livewire:confirmation-modal/>`.
66 lines
2.2 KiB
PHP
66 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Concerns;
|
|
|
|
use Livewire\Attributes\On;
|
|
|
|
trait Confirmation
|
|
{
|
|
/**
|
|
* Shows a confirmation modal before executing the given method.
|
|
*
|
|
* @param string $method Method name that should be executed if the user confirms.
|
|
* @param mixed|null $params Any parameters that should be passed to the method.
|
|
* @param string $title Title of the confirmation modal.
|
|
* @param string $subtitle Subtitle of the confirmation modal.
|
|
* @param string|null $description Description of the confirmation modal.
|
|
* @param string $descriptionClass Classes to apply to the description element. (!, !important to overide)
|
|
* @param string $acceptBtnClass Classes to apply to the accept button. (!, !important to overide)
|
|
* @param string $icon Icon to display in the confirmation modal.
|
|
*/
|
|
public function requireConfirmation(
|
|
string $method,
|
|
mixed $params = null,
|
|
string $title = 'Are you sure?',
|
|
string $subtitle = 'This action cannot be undone.',
|
|
?string $description = null,
|
|
string $descriptionClass = '',
|
|
string $acceptBtnClass = '',
|
|
string $icon = 'lucide.triangle-alert'
|
|
): void {
|
|
$this->dispatch(
|
|
'open-confirmation',
|
|
componentId: $this->getId(),
|
|
action: $method,
|
|
params: $params,
|
|
title: $title,
|
|
subtitle: $subtitle,
|
|
description: $description,
|
|
descriptionClass: $descriptionClass,
|
|
acceptBtnClass: $acceptBtnClass,
|
|
icon: $icon
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Listen globally for the acceptance, but only process if the ID matches.
|
|
*/
|
|
#[On('confirmation-accepted')]
|
|
public function onConfirmationAccepted(string $action, mixed $params, string $componentId): void
|
|
{
|
|
// Ignore the event if it wasn't requested by this specific component instance
|
|
if ($this->getId() !== $componentId) {
|
|
return;
|
|
}
|
|
|
|
// Verify the method actually exists on the component
|
|
if (method_exists($this, $action)) {
|
|
|
|
// Execute the requested method, passing any provided parameters
|
|
$this->{$action}($params);
|
|
}
|
|
}
|
|
}
|