Add Post Templates feature
- Create templates table with name, body, type, title, target, placeholders - Add Template model with applyToPost method for placeholder replacement - Add Livewire Templates/Index component for CRUD operations - Add templates index view with form and table - Add /templates route - Update Posts/Index to support template selection when creating posts - Templates can be applied to pre-fill post form fields Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Templates;
|
||||
|
||||
use App\Models\Template;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Layout('components.layouts.app')]
|
||||
class Index extends Component
|
||||
{
|
||||
public bool $showForm = false;
|
||||
|
||||
public ?int $editingId = null;
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public ?string $type = null;
|
||||
|
||||
public ?string $title = null;
|
||||
|
||||
public ?string $target = null;
|
||||
|
||||
public string $placeholders = '';
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.templates.index', [
|
||||
'templates' => Template::orderBy('name')->get(),
|
||||
'types' => Template::TYPES,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$this->resetForm();
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function edit(int $id): void
|
||||
{
|
||||
$template = Template::findOrFail($id);
|
||||
$this->editingId = $template->id;
|
||||
$this->name = $template->name;
|
||||
$this->body = $template->body;
|
||||
$this->type = $template->type;
|
||||
$this->title = $template->title;
|
||||
$this->target = $template->target;
|
||||
$this->placeholders = $template->placeholders ? implode(', ', $template->placeholders) : '';
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$validated = $this->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'body' => ['required', 'string'],
|
||||
'type' => ['nullable', 'string', 'in:' . implode(',', Template::TYPES)],
|
||||
'title' => ['nullable', 'string', 'max:255'],
|
||||
'target' => ['nullable', 'string', 'max:255'],
|
||||
'placeholders' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
// Parse placeholders from comma-separated string
|
||||
$placeholderArray = null;
|
||||
if ($validated['placeholders']) {
|
||||
$placeholderArray = array_map('trim', explode(',', $validated['placeholders']));
|
||||
$placeholderArray = array_filter($placeholderArray);
|
||||
}
|
||||
|
||||
Template::updateOrCreate(['id' => $this->editingId], [
|
||||
'name' => $validated['name'],
|
||||
'body' => $validated['body'],
|
||||
'type' => $validated['type'],
|
||||
'title' => $validated['title'],
|
||||
'target' => $validated['target'],
|
||||
'placeholders' => $placeholderArray,
|
||||
]);
|
||||
|
||||
$this->resetForm();
|
||||
$this->showForm = false;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
Template::findOrFail($id)->delete();
|
||||
}
|
||||
|
||||
public function resetForm(): void
|
||||
{
|
||||
$this->editingId = null;
|
||||
$this->name = '';
|
||||
$this->body = '';
|
||||
$this->type = null;
|
||||
$this->title = null;
|
||||
$this->target = null;
|
||||
$this->placeholders = '';
|
||||
$this->resetValidation();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user