70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Actions\CreatePrivateEndpointAction;
|
|
use App\Models\User;
|
|
use App\Models\WebhookEndpoint;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\View\View;
|
|
use Livewire\Component;
|
|
|
|
class Dashboard extends Component
|
|
{
|
|
public string $endpointName = '';
|
|
|
|
public bool $showCreateForm = false;
|
|
|
|
public function saveEndpoint(CreatePrivateEndpointAction $createPrivateEndpoint): void
|
|
{
|
|
$this->validate([
|
|
'endpointName' => ['nullable', 'string', 'max:100'],
|
|
]);
|
|
|
|
/** @var User $user */
|
|
$user = Auth::user();
|
|
$endpoint = $createPrivateEndpoint->handle($user, $this->endpointName ?: null);
|
|
|
|
$this->redirect($endpoint->publicUrl(), navigate: true);
|
|
}
|
|
|
|
public function deleteEndpoint(string $endpointId): void
|
|
{
|
|
$endpoint = $this->ownedEndpoint($endpointId);
|
|
Gate::authorize('delete', $endpoint);
|
|
$endpoint->delete();
|
|
}
|
|
|
|
public function toggleEndpoint(string $endpointId): void
|
|
{
|
|
$endpoint = $this->ownedEndpoint($endpointId);
|
|
Gate::authorize('update', $endpoint);
|
|
$endpoint->update(['is_active' => ! $endpoint->is_active]);
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
/** @var User $user */
|
|
$user = Auth::user();
|
|
|
|
return view('livewire.dashboard', [
|
|
'endpoints' => WebhookEndpoint::query()
|
|
->ownedBy($user)
|
|
->withCount('requests')
|
|
->latest()
|
|
->get(),
|
|
]);
|
|
}
|
|
|
|
private function ownedEndpoint(string $endpointId): WebhookEndpoint
|
|
{
|
|
/** @var User $user */
|
|
$user = Auth::user();
|
|
|
|
return WebhookEndpoint::query()
|
|
->ownedBy($user)
|
|
->findOrFail($endpointId);
|
|
}
|
|
}
|