Skip to content

Commit

Permalink
refactor: create comment card data transfer object
Browse files Browse the repository at this point in the history
  • Loading branch information
yilanboy committed Nov 26, 2024
1 parent 5098b94 commit f856d10
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 59 deletions.
50 changes: 50 additions & 0 deletions app/DataTransferObjects/CommentCardData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\DataTransferObjects;

use InvalidArgumentException;

class CommentCardData
{
/**
* @var array{'id': int, 'name': string, 'gravatar_url': string}|null
*/
public ?array $user {

Check failure on line 12 in app/DataTransferObjects/CommentCardData.php

View workflow job for this annotation

GitHub Actions / Test in php 8.4 version

Syntax error, unexpected '{', expecting ';' on line 12
set (array|null $user) {
if ($user !== null) {
if (! isset($user['id'], $user['name'], $user['gravatar_url'])) {
throw new InvalidArgumentException('user must have id, name and gravatar_url');
}
}

$this->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,
];
}
}
10 changes: 2 additions & 8 deletions app/Livewire/Shared/Comments/CommentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@

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;

class CommentGroup extends Component
{
use AuthorizesRequests;
use MarkdownConverter;

#[Locked]
public int $postId;
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 4 additions & 12 deletions app/Livewire/Shared/Comments/CommentList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,9 +70,6 @@ class CommentList extends Component
*/
public array $newCommentIds = [];

/**
* @throws CommonMarkException
*/
public function mount(): void
{
$this->showMoreComments();
Expand All @@ -86,9 +82,6 @@ public function appendNewIdToNewCommentIds(int $id): void
$this->newCommentIds[] = $id;
}

/**
* @throws CommonMarkException
*/
private function getComments(int $skip): array
{
$comments = Comment::query()
Expand Down Expand Up @@ -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'])) {
Expand All @@ -129,7 +122,9 @@ private function getComments(int $skip): array
}

return $comment;
}, $comments);
};

return array_map($callback, $comments);
}

private function updateCommentsList(array $comments): void
Expand All @@ -146,9 +141,6 @@ private function updateShowMoreButtonStatus(array $comments): void
}
}

/**
* @throws CommonMarkException
*/
public function showMoreComments(int $skip = 0): void
{
$comments = $this->getComments($skip);
Expand Down
1 change: 0 additions & 1 deletion app/Livewire/Shared/Comments/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
46 changes: 16 additions & 30 deletions app/Livewire/Shared/Comments/CreateCommentModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -52,9 +52,6 @@ protected function messages(): array
];
}

/**
* @throws CommonMarkException
*/
#[Computed]
public function convertedBody(): string
{
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
5 changes: 1 addition & 4 deletions app/Livewire/Shared/Comments/EditCommentModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -39,9 +38,6 @@ protected function messages(): array
];
}

/**
* @throws CommonMarkException
*/
#[Computed]
public function convertedBody(): string
{
Expand All @@ -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,
);
}
Expand Down
4 changes: 0 additions & 4 deletions app/Traits/MarkdownConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit f856d10

Please sign in to comment.