nimuli/app/Livewire/Modals/CreateWebhook.php

54 lines
1.4 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Domains\Webhook\Models\Webhook;
use Illuminate\Support\Str;
use LivewireUI\Modal\ModalComponent;
class CreateWebhook extends ModalComponent
{
public string $url = '';
public string $secret = '';
public array $selectedEvents = [];
public array $availableEvents = [
'link.clicked' => 'Link clicked',
'link.created' => 'Link created',
'link.updated' => 'Link updated',
'link.deleted' => 'Link deleted',
];
protected function rules(): array
{
return [
'url' => 'required|url|max:2048',
'secret' => 'nullable|string|max:255',
'selectedEvents' => 'required|array|min:1',
'selectedEvents.*' => 'string|in:' . implode(',', array_keys($this->availableEvents)),
];
}
public function save(): void
{
$this->validate();
$workspace = app('current_workspace');
Webhook::create([
'workspace_id' => $workspace->id,
'url' => $this->url,
'secret' => $this->secret ?: Str::random(32),
'events' => $this->selectedEvents,
'is_active' => true,
]);
$this->dispatch('webhookCreated');
$this->closeModal();
}
public function render(): \Illuminate\View\View
{
return view('livewire.modals.create-webhook');
}
}