feat: add Posts Livewire CRUD page
Adds the Posts index page (list/create/edit/delete) following the existing Sites/SocialAccounts pattern, and extends the shared nav layout with a Posts link. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
f746c5d6a0
commit
be8e6e997f
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Posts;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Site;
|
||||
use App\Models\SocialAccount;
|
||||
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 $siteId = '';
|
||||
|
||||
public string $socialAccountId = '';
|
||||
|
||||
public string $type = '';
|
||||
|
||||
public string $title = '';
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public string $status = 'draft';
|
||||
|
||||
public string $scheduledAt = '';
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.posts.index', [
|
||||
'posts' => Post::with(['site', 'socialAccount'])->orderByDesc('created_at')->get(),
|
||||
'sites' => Site::orderBy('name')->get(),
|
||||
'accounts' => SocialAccount::orderBy('username')->get(),
|
||||
'types' => Post::TYPES,
|
||||
'statuses' => Post::STATUSES,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$this->resetForm();
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function edit(int $id): void
|
||||
{
|
||||
$post = Post::findOrFail($id);
|
||||
$this->editingId = $post->id;
|
||||
$this->siteId = (string) $post->site_id;
|
||||
$this->socialAccountId = $post->social_account_id ? (string) $post->social_account_id : '';
|
||||
$this->type = $post->type;
|
||||
$this->title = (string) $post->title;
|
||||
$this->body = $post->body;
|
||||
$this->status = $post->status;
|
||||
$this->scheduledAt = $post->scheduled_at?->format('Y-m-d\TH:i') ?? '';
|
||||
$this->showForm = true;
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$validated = $this->validate([
|
||||
'siteId' => ['required', 'integer', 'exists:sites,id'],
|
||||
'socialAccountId' => ['nullable', 'integer', 'exists:social_accounts,id'],
|
||||
'type' => ['required', 'string', 'in:' . implode(',', Post::TYPES)],
|
||||
'title' => ['nullable', 'string', 'max:255'],
|
||||
'body' => ['required', 'string'],
|
||||
'status' => ['required', 'string', 'in:' . implode(',', Post::STATUSES)],
|
||||
'scheduledAt' => ['nullable', 'date'],
|
||||
]);
|
||||
|
||||
Post::updateOrCreate(['id' => $this->editingId], [
|
||||
'site_id' => $validated['siteId'],
|
||||
'social_account_id' => $validated['socialAccountId'] ?: null,
|
||||
'type' => $validated['type'],
|
||||
'title' => $validated['title'] ?: null,
|
||||
'body' => $validated['body'],
|
||||
'status' => $validated['status'],
|
||||
'scheduled_at' => $validated['scheduledAt'] ?: null,
|
||||
]);
|
||||
|
||||
$this->resetForm();
|
||||
$this->showForm = false;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
Post::findOrFail($id)->delete();
|
||||
}
|
||||
|
||||
public function resetForm(): void
|
||||
{
|
||||
$this->editingId = null;
|
||||
$this->siteId = '';
|
||||
$this->socialAccountId = '';
|
||||
$this->type = '';
|
||||
$this->title = '';
|
||||
$this->body = '';
|
||||
$this->status = 'draft';
|
||||
$this->scheduledAt = '';
|
||||
$this->resetValidation();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user