diff --git a/app/Livewire/CreatePost.php b/app/Livewire/CreatePost.php index 4fb3e4e..1fe2ad3 100644 --- a/app/Livewire/CreatePost.php +++ b/app/Livewire/CreatePost.php @@ -2,37 +2,27 @@ namespace App\Livewire; -use App\Models\Post; -use Livewire\Attributes\Validate; use Livewire\Component; +use App\Livewire\Forms\PostForm; class CreatePost extends Component { - #[Validate('required|min:5')] - public string $title = ''; - - #[Validate('required|min:5')] - public string $body = ''; + public PostForm $form; public bool $success = false; - public function render() - { - return view('livewire.create-post'); - } - public function save(): void { $this->validate(); - Post::create([ - 'title' => $this->title, - 'body' => $this->body, - ]); + $this->form->save(); $this->success = true; + } - $this->reset('title', 'body'); + public function render() + { + return view('livewire.create-post'); } } diff --git a/app/Livewire/EditPost.php b/app/Livewire/EditPost.php new file mode 100644 index 0000000..bdb738c --- /dev/null +++ b/app/Livewire/EditPost.php @@ -0,0 +1,33 @@ +form->setPost($post); + } + + public function update(): void + { + $this->validate(); + + $this->form->update(); + + $this->success = true; + } + + public function render() + { + return view('livewire.edit-post'); + } +} \ No newline at end of file diff --git a/app/Livewire/Forms/PostForm.php b/app/Livewire/Forms/PostForm.php new file mode 100644 index 0000000..f49abdb --- /dev/null +++ b/app/Livewire/Forms/PostForm.php @@ -0,0 +1,39 @@ +post = $post; + + $this->title = $post->title; + + $this->body = $post->body; + } + + public function save(): void + { + Post::create($this->all()); + + $this->reset('title', 'body'); + } + + public function update(): void + { + $this->post->update($this->all()); + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7f93bd8..a94aa16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "project2", + "name": "project", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/resources/views/livewire/create-post.blade.php b/resources/views/livewire/create-post.blade.php index 2749386..e9d3c54 100644 --- a/resources/views/livewire/create-post.blade.php +++ b/resources/views/livewire/create-post.blade.php @@ -3,19 +3,19 @@ Post saved successfully. @endif -