diff --git a/app/DataTransferObjects/CommentCardData.php b/app/DataTransferObjects/CommentCardData.php new file mode 100644 index 00000000..fcf86790 --- /dev/null +++ b/app/DataTransferObjects/CommentCardData.php @@ -0,0 +1,50 @@ +user = $user; + } + } + + public function __construct( + public int $id, + public ?int $userId, + public string $body, + public string $convertedBody, + public string $createdAt, + public string $updatedAt, + public int $childrenCount = 0, + ?array $user = null + ) { + $this->user = $user; + } + + public function toArray(): array + { + return [ + 'id' => $this->id, + 'user_id' => $this->userId, + 'body' => $this->body, + 'converted_body' => $this->convertedBody, + 'created_at' => $this->createdAt, + 'updated_at' => $this->updatedAt, + 'children_count' => $this->childrenCount, + 'user' => $this->user, + ]; + } +} diff --git a/app/Livewire/Shared/Comments/CommentGroup.php b/app/Livewire/Shared/Comments/CommentGroup.php index 4048cb69..db427d93 100644 --- a/app/Livewire/Shared/Comments/CommentGroup.php +++ b/app/Livewire/Shared/Comments/CommentGroup.php @@ -4,11 +4,9 @@ use App\Models\Comment; use App\Models\Post; -use App\Traits\MarkdownConverter; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Support\Facades\DB; use Illuminate\View\View; -use League\CommonMark\Exception\CommonMarkException; use Livewire\Attributes\Locked; use Livewire\Attributes\On; use Livewire\Component; @@ -16,7 +14,6 @@ class CommentGroup extends Component { use AuthorizesRequests; - use MarkdownConverter; #[Locked] public int $postId; @@ -69,14 +66,11 @@ public function createComment(array $comment): void $this->comments = [$comment['id'] => $comment] + $this->comments; } - /** - * @throws CommonMarkException - */ #[On('update-comment-in-{commentGroupName}')] - public function updateComment(int $id, string $body, string $updatedAt): void + public function updateComment(int $id, string $body, string $convertedBody, string $updatedAt): void { $this->comments[$id]['body'] = $body; - $this->comments[$id]['converted_body'] = $this->convertToHtml($body); + $this->comments[$id]['converted_body'] = $convertedBody; $this->comments[$id]['updated_at'] = $updatedAt; } diff --git a/app/Livewire/Shared/Comments/CommentList.php b/app/Livewire/Shared/Comments/CommentList.php index 171469ea..9aed9f42 100644 --- a/app/Livewire/Shared/Comments/CommentList.php +++ b/app/Livewire/Shared/Comments/CommentList.php @@ -7,7 +7,6 @@ use App\Traits\MarkdownConverter; use Illuminate\Database\Eloquent\Builder; use Illuminate\View\View; -use League\CommonMark\Exception\CommonMarkException; use Livewire\Attributes\Locked; use Livewire\Attributes\On; use Livewire\Attributes\Renderless; @@ -71,9 +70,6 @@ class CommentList extends Component */ public array $newCommentIds = []; - /** - * @throws CommonMarkException - */ public function mount(): void { $this->showMoreComments(); @@ -86,9 +82,6 @@ public function appendNewIdToNewCommentIds(int $id): void $this->newCommentIds[] = $id; } - /** - * @throws CommonMarkException - */ private function getComments(int $skip): array { $comments = Comment::query() @@ -120,7 +113,7 @@ private function getComments(int $skip): array ->keyBy('id') ->toArray(); - return array_map(function ($comment): array { + $callback = function (array $comment): array { $comment['converted_body'] = $this->convertToHtml($comment['body']); if (! is_null($comment['user'])) { @@ -129,7 +122,9 @@ private function getComments(int $skip): array } return $comment; - }, $comments); + }; + + return array_map($callback, $comments); } private function updateCommentsList(array $comments): void @@ -146,9 +141,6 @@ private function updateShowMoreButtonStatus(array $comments): void } } - /** - * @throws CommonMarkException - */ public function showMoreComments(int $skip = 0): void { $comments = $this->getComments($skip); diff --git a/app/Livewire/Shared/Comments/Comments.php b/app/Livewire/Shared/Comments/Comments.php index d21eb6b4..bad168ec 100644 --- a/app/Livewire/Shared/Comments/Comments.php +++ b/app/Livewire/Shared/Comments/Comments.php @@ -26,7 +26,6 @@ class Comments extends Component #[Locked] public CommentOrder $order = CommentOrder::POPULAR; - // update comment count in post show page #[On('update-comment-counts')] public function updateCommentCounts(): void { diff --git a/app/Livewire/Shared/Comments/CreateCommentModal.php b/app/Livewire/Shared/Comments/CreateCommentModal.php index 63c52251..057b0d0c 100644 --- a/app/Livewire/Shared/Comments/CreateCommentModal.php +++ b/app/Livewire/Shared/Comments/CreateCommentModal.php @@ -2,6 +2,7 @@ namespace App\Livewire\Shared\Comments; +use App\DataTransferObjects\CommentCardData; use App\Models\Comment; use App\Models\Post; use App\Models\User; @@ -12,7 +13,6 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\View\View; -use League\CommonMark\Exception\CommonMarkException; use Livewire\Attributes\Computed; use Livewire\Attributes\Locked; use Livewire\Component; @@ -52,9 +52,6 @@ protected function messages(): array ]; } - /** - * @throws CommonMarkException - */ #[Computed] public function convertedBody(): string { @@ -117,9 +114,23 @@ public function store(?int $parentId = null): void // Notify the article author of new comments. $post->user->notifyNewComment(new NewComment($comment)); + $commentCard = new CommentCardData( + id: $comment->id, + userId: auth()->id(), + body: $comment->body, + convertedBody: $this->convertToHtml($comment->body), + createdAt: $comment->created_at->toDateTimeString(), + updatedAt: $comment->updated_at->toDateTimeString(), + user: auth()->check() ? [ + 'id' => auth()->id(), + 'name' => auth()->user()->name, + 'gravatar_url' => get_gravatar(auth()->user()->email), + ] : null, + ); + $this->dispatch( event: 'create-new-comment-to-'.($parentId ?? 'root').'-new-comment-group', - comment: $this->prepareCommentCardArray($comment, auth()->user()) + comment: $commentCard->toArray(), ); $this->dispatch(event: 'append-new-id-to-'.($parentId ?? 'root').'-comment-list', id: $comment->id); @@ -137,29 +148,4 @@ public function render(): View { return view('livewire.shared.comments.create-comment-modal'); } - - /** - * @throws CommonMarkException - */ - private function prepareCommentCardArray(Comment $comment, ?User $user): array - { - $comment = $comment->toArray(); - - $comment['converted_body'] = $this->convertToHtml($comment['body']); - $comment['children_count'] = 0; - - if (is_null($user)) { - $comment['user'] = null; - - return $comment; - } - - $comment['user'] = [ - 'id' => $user->id, - 'name' => $user->name, - 'gravatar_url' => get_gravatar($user->email), - ]; - - return $comment; - } } diff --git a/app/Livewire/Shared/Comments/EditCommentModal.php b/app/Livewire/Shared/Comments/EditCommentModal.php index 99ca7835..32df470c 100644 --- a/app/Livewire/Shared/Comments/EditCommentModal.php +++ b/app/Livewire/Shared/Comments/EditCommentModal.php @@ -7,7 +7,6 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\View\View; -use League\CommonMark\Exception\CommonMarkException; use Livewire\Attributes\Computed; use Livewire\Component; @@ -39,9 +38,6 @@ protected function messages(): array ]; } - /** - * @throws CommonMarkException - */ #[Computed] public function convertedBody(): string { @@ -67,6 +63,7 @@ public function update(Comment $comment, string $groupName): void event: 'update-comment-in-'.$groupName, id: $comment->id, body: $comment->body, + convertedBody: $this->convertToHtml($comment->body), updatedAt: $comment->updated_at, ); } diff --git a/app/Traits/MarkdownConverter.php b/app/Traits/MarkdownConverter.php index 63c98f44..b24c199d 100644 --- a/app/Traits/MarkdownConverter.php +++ b/app/Traits/MarkdownConverter.php @@ -3,16 +3,12 @@ namespace App\Traits; use League\CommonMark\Environment\Environment; -use League\CommonMark\Exception\CommonMarkException; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension; use League\CommonMark\MarkdownConverter as CommonMarkdownConverter; trait MarkdownConverter { - /** - * @throws CommonMarkException - */ public function convertToHtml(string $body): string { $config = [