mailwolt/app/Livewire/Ui/System/Modal/WebhookEditModal.php

71 lines
2.1 KiB
PHP

<?php
namespace App\Livewire\Ui\System\Modal;
use App\Models\Webhook;
use LivewireUI\Modal\ModalComponent;
class WebhookEditModal extends ModalComponent
{
public int $webhookId;
public string $name = '';
public string $url = '';
public array $selected = [];
public bool $is_active = true;
public string $secret = '';
public function mount(int $webhookId): void
{
$webhook = Webhook::findOrFail($webhookId);
$this->webhookId = $webhookId;
$this->name = $webhook->name;
$this->url = $webhook->url;
$this->selected = $webhook->events;
$this->is_active = $webhook->is_active;
$this->secret = $webhook->secret;
}
public function save(): void
{
$this->validate([
'name' => 'required|string|max:80',
'url' => 'required|url|max:500',
'selected' => 'required|array|min:1',
'selected.*' => 'in:' . implode(',', array_keys(Webhook::allEvents())),
], [
'selected.required' => 'Bitte mindestens ein Event auswählen.',
]);
Webhook::findOrFail($this->webhookId)->update([
'name' => $this->name,
'url' => $this->url,
'events' => $this->selected,
'is_active' => $this->is_active,
]);
$this->dispatch('webhook-saved');
$this->closeModal();
}
public function regenerateSecret(): void
{
$webhook = Webhook::findOrFail($this->webhookId);
$webhook->update(['secret' => \App\Services\WebhookService::generateSecret()]);
$this->secret = $webhook->fresh()->secret;
$this->dispatch('notify', type: 'success', message: 'Secret neu generiert.');
}
public function toggleAll(): void
{
$all = array_keys(Webhook::allEvents());
$this->selected = count($this->selected) === count($all) ? [] : $all;
}
public function render()
{
return view('livewire.ui.system.modal.webhook-edit-modal', [
'allEvents' => Webhook::allEvents(),
]);
}
}