Skip to content

Commit

Permalink
Blog with this title exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Aug 9, 2024
1 parent 994e61b commit 817a0d8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
30 changes: 28 additions & 2 deletions app/Livewire/BlogMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,47 @@ class BlogMaker extends Component
public $content = '';
public $contentHtml;

public function saveBlog(): RedirectResponse
public function mount(): void
{
//check if session has blog data
if (session()->has('blog')) {
$blog = session()->get('blog');
$this->title = $blog['title'];
$this->content = $blog['content'];
//alert
session()->flash('message', 'Blog data restored from session');
}

}

public function saveBlog(): mixed
{
$this->validate([
'title' => 'required|min:6',
'content' => 'required|min:6',
]);

//save blog to session
session()->put('blog', [
'title' => $this->title,
'content' => $this->content,
]);

$blogData = [
'title' => $this->title,
'content' => $this->content, // Storing HTML content directly
'slug' => Str::slug($this->title),
'user_id' => Auth::id(),
];

$blog = Blog::create($blogData);
try {
$blog = Blog::create($blogData);
} catch (\Exception $e) {
if (Str::contains($e->getMessage(), 'blogs_slug_unique')) {
return redirect()->route('blog.create')->with('error', 'Blog with this title already exists');
}
return redirect()->route('blog.create' )->with('error', 'Failed to create blog' . $e->getMessage());
}

return redirect()->route('blogs.show', $blog->slug);
}
Expand Down
16 changes: 11 additions & 5 deletions resources/views/blogs/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div>
<div>
<input wire:model="title" type="text" placeholder="Enter title">
@error('title') <span>{{ $message }}</span> @enderror
<div class="rounded-lg p-4 flex flex-col gap-4" data-theme="light">
<div class="flex flex-col">
<input wire:model="title" class="input input-ghost in" type="text" placeholder="Enter title">
@error('title') <span class="text-xs text-red-500">{{ $message }}</span> @enderror
</div>

<div>
<div class="flex flex-col">
<div id="editor"></div>
@error('content') <span>{{ $message }}</span> @enderror
</div>
Expand All @@ -13,6 +13,7 @@

@script
<script>
const textArea = document.querySelector('#content');
const saveButton = document.querySelector('#save_button');
Expand Down Expand Up @@ -43,6 +44,11 @@
}
});
//when the livewire component is mounted
document.addEventListener('livewire:load', function () {
quill.root.innerHTML = @this.content;
});
saveButton.addEventListener('click', () => {
@this.
set('content', quill.root.innerHTML);
Expand Down
1 change: 1 addition & 0 deletions resources/views/blogs/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</div>
<livewire:alerts/>
<div class="flex relative z-40 gap-2 p-2">
<livewire:sidebar/>
<!-- Page Content -->
<main class="w-full bg-gray-100 p-4 text-black/80 overflow-clip rounded-[16px]">
{{ $slot }}
Expand Down

0 comments on commit 817a0d8

Please sign in to comment.