Skip to content

Commit

Permalink
feat: auto save editing post
Browse files Browse the repository at this point in the history
  • Loading branch information
yilanboy committed Nov 6, 2024
1 parent c9249ec commit 33b052a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app/Livewire/Forms/PostForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

class PostForm extends Form
{
#[Locked]
public ?int $user_id = null;
#[Validate('required')]
public int $user_id;

public int $category_id = 1;

Expand Down Expand Up @@ -79,6 +79,7 @@ public function autoSave(string $key): void
json_encode([
'category_id' => $this->category_id,
'is_private' => $this->is_private,
'preview_url' => $this->preview_url,
'title' => $this->title,
'tags' => $this->tags,
'body' => $this->body,
Expand All @@ -94,6 +95,7 @@ public function setDataFromAutoSave(string $key): bool

$this->category_id = $autoSavePostData['category_id'];
$this->is_private = $autoSavePostData['is_private'];
$this->preview_url = $autoSavePostData['preview_url'];
$this->title = $autoSavePostData['title'];
$this->tags = $autoSavePostData['tags'];
$this->body = $autoSavePostData['body'];
Expand Down Expand Up @@ -142,13 +144,12 @@ public function uploadPreviewImage(): void

public function setPost(Post $post): void
{
$this->user_id = $post->user_id;
$this->category_id = $post->category_id;
$this->is_private = $post->is_private;
$this->preview_url = $post->preview_url;
$this->title = $post->title;
$this->body = $post->body;
$this->tags = $post->tags_json;
$this->body = $post->body;
}

public function createPost(): Post
Expand Down
15 changes: 15 additions & 0 deletions app/Livewire/Pages/Posts/EditPostPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class EditPostPage extends Component

public Post $post;

public string $autoSaveKey;

public function mount(int $id): void
{
$this->post = Post::findOrFail($id);
Expand All @@ -32,9 +34,22 @@ public function mount(int $id): void

$this->categories = Category::all(['id', 'name']);

$this->autoSaveKey = 'auto_save_user_'.auth()->id().'_edit_'.$this->post->id.'_post';

$this->form->user_id = auth()->id();

if ($this->form->setDataFromAutoSave($this->autoSaveKey)) {
return;
}

$this->form->setPost($this->post);
}

public function updated(): void
{
$this->form->autoSave($this->autoSaveKey);
}

/**
* @throws RandomException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class="flex w-full items-center justify-start rounded-xl bg-gradient-to-r from-w

{{-- save button --}}
<button
class="group mt-4 inline-flex h-14 w-14 items-center justify-center rounded-xl border border-transparent bg-blue-600 text-gray-50 ring-blue-300 transition duration-150 ease-in-out focus:border-blue-700 focus:outline-none focus:ring active:bg-blue-700"
class="group mt-4 inline-flex h-14 w-14 items-center justify-center rounded-xl border border-transparent bg-blue-600 text-gray-50 ring-blue-300 transition duration-150 ease-in-out focus:border-blue-700 focus:outline-none focus:ring active:bg-blue-700 disabled:bg-slate-600"
form="{{ $formId }}"
type="submit"
wire:loading.attr="disabled"
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Posts/CreatePostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
->toBe([
'category_id' => $categoryId,
'is_private' => false, // default value
'preview_url' => '',
'title' => $title,
'tags' => $tags,
'body' => $body,
Expand Down Expand Up @@ -277,6 +278,7 @@
json_encode([
'category_id' => $categoryId,
'is_private' => false,
'preview_url' => '',
'title' => $title,
'tags' => $tags,
'body' => $body,
Expand Down

0 comments on commit 33b052a

Please sign in to comment.