forked from eatvio/eatvio-infra
102 lines
2.4 KiB
PHP
102 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\BlogPost;
|
|
|
|
use App\Models\BlogPost;
|
|
use App\Models\Tag;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class Create extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public UploadedFile|null $image = null;
|
|
|
|
public string $state = 'archived';
|
|
|
|
public string $title = '';
|
|
|
|
public string $description = '';
|
|
|
|
public Collection|array $tags = [];
|
|
|
|
public string $videoUrl = '';
|
|
|
|
// protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved'];
|
|
|
|
protected function getListeners(): array
|
|
{
|
|
return ['parentComponentTagAdded', 'parentComponentTagRemoved'];
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.blog-post.create');
|
|
}
|
|
|
|
public function parentComponentTagAdded(Tag $tag): void
|
|
{
|
|
$tagIndex = $tag->id;
|
|
$this->tags[$tagIndex] = $tag;
|
|
}
|
|
|
|
public function parentComponentTagRemoved(Tag $tag): void
|
|
{
|
|
$this->tags = collect($this->tags)
|
|
->filter(fn ($curTag) => $curTag['name'] !== $tag)
|
|
->all();
|
|
}
|
|
|
|
public function draft(): void
|
|
{
|
|
$this->state = 'archived';
|
|
$this->publish();
|
|
}
|
|
|
|
public function preview(): void
|
|
{
|
|
$this->state = 'archived';
|
|
$this->publish();
|
|
}
|
|
|
|
public function publish(): void
|
|
{
|
|
$this->validate();
|
|
|
|
$post = new BlogPost();
|
|
// $post->image = $this->image->store('blog_posts', 'public');
|
|
$post->image = $this->image->store('blog_posts');
|
|
$post->state = $this->state;
|
|
$post->title = $this->title;
|
|
$post->description = $this->description;
|
|
$post->youtube_url = $this->videoUrl;
|
|
$post->save();
|
|
|
|
$tagsIds = collect($this->tags)->map(fn ($curTag) => $curTag['id']);
|
|
$post->tags()->attach($tagsIds);
|
|
|
|
$user = Auth::user();
|
|
$user->author_of_blog_post()->attach($post->id);
|
|
|
|
|
|
|
|
$this->redirectRoute('blog_post.show', $post);
|
|
// return redirect()->route('blog_post.show', $post->id);
|
|
}
|
|
|
|
protected function rules(): array
|
|
{
|
|
return [
|
|
'image' => 'required|image|max:2048',
|
|
'state' => 'required|in:active,archived',
|
|
'description' => 'required|min:3',
|
|
'videoUrl' => 'nullable|url|max:2048',
|
|
];
|
|
}
|
|
}
|