58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Modals;
|
|
|
|
use App\Domains\Link\Models\Link;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\View\View;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
class CreateLink extends ModalComponent
|
|
{
|
|
public string $targetUrl = '';
|
|
|
|
public string $slug = '';
|
|
|
|
public string $title = '';
|
|
|
|
protected $rules = [
|
|
'targetUrl' => 'required|url|max:2048',
|
|
'slug' => 'nullable|alpha_dash|max:50|unique:links,slug',
|
|
'title' => 'nullable|max:200',
|
|
];
|
|
|
|
public function generateSlug(): void
|
|
{
|
|
if (empty($this->slug)) {
|
|
$this->slug = Str::random(6);
|
|
}
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$this->validate();
|
|
$workspace = app('current_workspace');
|
|
|
|
if (empty($this->slug)) {
|
|
$this->slug = Str::random(6);
|
|
}
|
|
|
|
Link::create([
|
|
'workspace_id' => $workspace->id,
|
|
'target_url' => $this->targetUrl,
|
|
'slug' => $this->slug,
|
|
'title' => $this->title ?: null,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$this->dispatch('linkCreated');
|
|
$this->dispatch('toast', message: 'Link erstellt', type: 'success');
|
|
$this->closeModal();
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.modals.create-link');
|
|
}
|
|
}
|